I just attempted to make some stylistic edits on What are the consequences of nesting classes?.
OP has not been seen on Stack Overflow in 7 years, so I think the question can safely be considered abandoned in that sense. Originally, the question asked if it "is good practice" to nest classes, which of course is opinion-based and got the question closed on that basis.
I think the question can be made objective by talking about specifically what happens when classes are nested. I want to rehabilitate the question because the existing answer is high quality; because I think there is an important underlying idea about how nesting classes works in Python; because I could contribute my own answer; and because old questions that were closed as "opinion-based" are somewhat vulnerable to deletion even if they're popular.
So, I tried to edit to improve the wording (also trying to ask more clearly, and improving the code formatting since it didn't cause an error) and the title.
In this process, I couldn't help but notice that the code makes very little sense for demonstrating the problem (some subject matter expertise required here):
It's much more complex than necessary. Separate
b1
andb2
attributes aren't really necessary to demonstrate any issues with communicating between the two classes, and neither are separatea1
anda2
attributes. Having theB
class store an instance count doesn't appear to have a point in the part of the code that can be seen;listb[A.B.count]
seems intended to access theA.B
instance that was just created, and it's not clear why we would care about the previous instance in the list.It's not a MRE at all. We can't see
pyfunc1
,pyfunc2
orpyfunc3
definitions, not that we even know what types should be passed to the constructors in the first place.The setup needlessly introduces the potential for bugs that aren't related to the underlying task of making nested classes communicate. In particular, the
A
instances each store a separate list ofB
instances, but will index it using an overall count that is stored in theB
class. (If the goal was simply to get the last value of the instance's own list, no count is necessary for that anyway).OP has some misconceptions and oversights in describing the code. In particular, the text discusses a
bb
"instance variable" (this seems intended to mean instance attribute) ofB
instances, which doesn't exist (bb
only appears as a parameter). OP also seems to expectA.a1 = pyfunc1(bb)
to modify some particularA
instance, rather than the class itself, although that wasn't quite clear to me. (I'm pretty sure OP properly understood the distinction between classes and their instances, but not 100%.)
I suspect that it's important to preserve the bit about modifying A.a1
in some form, because OP may have been expecting an instance of the "outer" class to be automatically "associated with" each B instance in some form (it works like this in some other programming languages, but not Python; Python's nested classes are roughly the equivalent of only static
inner classes in Java, if that helps anyone).
Also, note that the existing answer doesn't reference the current code, but the revision 1 code; OP edited to fix several places where the code said B
and needed to say A.B
(because the class name would be looked up in the global scope; Python's class
statement does create a local scope, but only while the class is being created).
Do you see any issues with the edit I made so far? Should I do something with the code? Any specific suggestions for how to make a better code example, and/or to rephrase the question around that example? Should the code reflect the original namespacing issue?