What are the consequences of nesting classes? [closed]

Nesting a class doesn’t reduce nor increase execution efficiency. It may alter maintenance and understanding efficiency.

The nested class becomes just another attribute on the parent class. You’ll have to reference it as A.B rather than B. That’s it, you deferred the lookup to a different namespace. In other words, your __init__ method will fail, because there is no global name B, only A.B and self.B exist (both referencing the same class object).

There is otherwise no special relationship between a nested class and their parent, as there is in Java.

Most Python developers do not nest classes, so when you do so you break convention and increase maintenance cost.

Leave a Comment