2

This...

class A(object):
    class B(A):
        def __init__(self):
            pass

... throws "NameError: name 'A' is not defined".

Is there proper syntax to accomplish this, or must I use workarounds, like this?

class A(object):
    pass
class _B(A):
    pass
A.B = _B

The prior is strongly preferable. Thank you.

Bernhard
  • 2,084
  • 2
  • 20
  • 33
ca2longoria
  • 362
  • 1
  • 3
  • 12
  • 7
    1. That's an inner class, not a subclass. 2. No, an inner class can't inherit (not extend) its outer class because the outer class is not fully defined while defining the inner class. 3. Your workaround is not a work around, as it doesn't have an inner class. You are confusing subclasses and inner classes. – DeepSpace Aug 22 '16 at 10:51
  • Note that in Python there are very few good use cases for inner classes. – Daniel Roseman Aug 22 '16 at 10:53
  • Then I misnamed the query. So this is not possible with an inner class. Is it possible to rename this, or is the question invalid? – ca2longoria Aug 22 '16 at 10:54
  • @DeepSpace, phrase that in an answer and I'll mark it. I'll answer myself, otherwise. – ca2longoria Aug 22 '16 at 11:03

2 Answers2

2

You can not do this the normal way and probably should not do this.

For those who have a valid reason to attempt something similar, there is a workaround by dynamically changing the superclass of A.B after A is fully defined (see https://stackoverflow.com/a/9639512/5069869).

This is probably terrible code, a big hack and should not be done, but it works under certain conditions (see linked answer)

class T: pass
class A(object):
  def a(self):
    return "Hallo a"
  class B(T):
    def b(self):
      return "Hallo b"
A.B.__bases__ = (A,)
b=A.B()
assert isinstance(b, A)
assert b.a()=="Hallo a"

Now you can even do something weird like x = A.B.B.B.B()

Community
  • 1
  • 1
Bernhard
  • 2,084
  • 2
  • 20
  • 33
  • 2
    Oh good lord... Thanks for the working example, though. It clarifies just how backwards one must move to make this a reality. I'll approach the class structure differently. – ca2longoria Aug 22 '16 at 11:29
1

As per OPs request, posting as an answer.

  1. That's an inner class, not a subclass.

  2. No, an inner class can't inherit (not extend) its outer class because the outer class is not fully defined while defining the inner class.

  3. Your workaround is not a work around, as it doesn't have an inner class. You are confusing subclasses and inner classes

DeepSpace
  • 78,697
  • 11
  • 109
  • 154