3

I'm enjoying Python 3.5 type hints support, but facing a special concern.

I'm trying to specify the type of a constructor parameter, which should be the class itself (i.e. a self-referential relation). Here is my example:

class Section(object):    
    def __init__(self, path: str, parent: Section):
        pass

Python complains with a

NameError: name 'Section' is not defined

Is there a way to fix this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user650108
  • 1,009
  • 1
  • 9
  • 18
  • use an abstract base class to avoid self reference? this may also avoid circular referencing that could cause memory issues (or it may not, ymmv) – Aaron Apr 13 '17 at 21:10

0 Answers0