1

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 and b2 attributes aren't really necessary to demonstrate any issues with communicating between the two classes, and neither are separate a1 and a2 attributes. Having the B 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 the A.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 or pyfunc3 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 of B instances, but will index it using an overall count that is stored in the B 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) of B instances, which doesn't exist (bb only appears as a parameter). OP also seems to expect A.a1 = pyfunc1(bb) to modify some particular A 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?

1 Answer 1

7

How I'm viewing this:

  • OP's original question may be valid but offers no measurement or demonstrable sentiment to why they think their code isn't performant
  • OP hasn't been around in 7 years, and they've elected not to edit their question to clarify their intent
  • The edit you provide attempts to fix some of this but you discover that their example doesn't illustrate the actual problem, and is more complex than what you think would be suitable

My opinion:

It'd be better to not try to polish this turd. If the OP isn't available to help us clear up what it is they meant, then I don't really see that much value in trying to make the question any better.

The provided answer (likely) gives the OP some clarity into what's going on, and (likely) helps with their initial confusion since they (likely) came from a Java background, where nesting classes is still done.

4
  • 1
    I'm a little confused. I don't think the question is about performance at all; I think it's about correctness, and specifically about how an aspect of the language works. Feb 11 at 22:24
  • @KarlKnechtel: It would sure be nice to have the OP clarify what it was they were asking about, so we didn't have to guess about it. But this only makes my point; the question itself isn't answerable but luckily got a coherent answer while it was still active. Editing it now isn't going to magically introduce that missing context or make it a question that's...better...than what it really is.
    – Makoto
    Feb 12 at 0:05
  • 1
    So I have to make a new QA pair to address the underlying question? If you compare and contrast this with the comment discussion I had at meta.stackoverflow.com/posts/comments/946920?noredirect=1, perhaps you can get a sense of my frustration. Stack Overflow is, to my understanding, supposed to function as a curated library, but there's never any clear place for the curators to put the best quality material. Feb 12 at 9:24
  • 1
    @KarlKnechtel: I mean yes, that's the idea. If you want to see a question like this answered then ideally you'd create a Q&A pair to go along with it, that's based on this one. Then you're in full control of the question domain and can provide enough context to anyone curious about this quirk of the language. The challenge that you highlight with the curated library is that the things that are kept on the shelves here have to be worthy of preservation. You stumbled across one that isn't...but is still around for Some Reason™.
    – Makoto
    Feb 12 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .