0

I want to know if it is possible to do something like the following

class A:
    a = 1
    class B:
            a = 2
            class C(A):
                    pass
x = A.B.C()
print(x.a)

In this scenario I would want it to print 1.

I have seen other posts on here trying to inherit from parent classes but I am trying to keep class B from inheriting everything in the parent class. It is a matter of organization of the classes and it happens that for my needs it is more visually organized if I place the classes like this rather than creating non-nested classes. If more information is needed let me know.

I'm not really sure how to word it better so here is a better picture of what I am already working with.

class TopLevelClass:
    class MidLevelClass:
        someVar = 'SomeText'
    class SiblingToMidLevel:
        class Child1:
        class Child2(MidLevelClass):
            pass

Ultimately what I want to do is to be able to access the someVar variable from the Child2 class.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
alphamonkey
  • 17
  • 1
  • 7
  • 6
    That is *not* inheritance, nor valid Python. Please be clearer about what you are trying to achieve. – jonrsharpe Apr 10 '14 at 14:40
  • "I am trying to keep class B from inheriting everything in the parent class" - then `B` _shouldn't_ inherit from `A`. Either create a base class with the parts both `A` and `B` need in common, or if you're wanting `C` to inherit from both `A` and `B` use multiclass inheritance: `class C(A, B):`. In any case, you most likely don't want to be nesting classes like that. – Matthew Trevor Apr 10 '14 at 14:56
  • defining class inside class is not a way of inheriting it just a matter of scoping. which is less useful most of the time. – James Sapam Apr 10 '14 at 14:56
  • @MatthewTrevor Thanks for the suggestion. Its not my idea to nest the classes. I am working on something in a group and it is how the group wants to do it. – alphamonkey Apr 10 '14 at 15:00
  • 1
    @alphamonkey That's just godawful. Are those classes instantiated at all? About the only way I can think of doing it is adding to `Child2` something like `def someVar(self): return MidLevelClass.someVar` and decorating it as a property. Your group should probably read up on the concept of composition rather than using weird nesting. – Matthew Trevor Apr 10 '14 at 15:05
  • There are a couple of SO q/a's about nested classes that may help: http://stackoverflow.com/questions/14606559/python-nested-classes, http://stackoverflow.com/questions/1765677/python-nested-classes-scope – wwii Apr 10 '14 at 15:08
  • @alphamonkey as you have it now, each class is a class attribute of the enclosing class, and `a` is also a class attribute. `C` can't inherit from `A`, as you start defining `C` before you finish defining `A` (hence `NameError`). Seriously, ditch the nested classes; "visually organized" or not, they are not helping you at all. – jonrsharpe Apr 10 '14 at 15:08
  • And this: http://stackoverflow.com/questions/2278426/inner-classes-how-can-i-get-the-outer-class-object-at-construction-time - which I found here; http://www.momentaryfascinations.com/programming/bound.inner.classes.for.python.html – wwii Apr 10 '14 at 15:16

0 Answers0