Nested Python class needs to access variable in enclosing class

前端 未结 6 2236
难免孤独
难免孤独 2020-12-20 13:02

I\'ve seen a few \"solutions\" to this, but the solution every time seems to be \"Don\'t use nested classes, define the classes outside and then use them normally\". I don\'

6条回答
  •  失恋的感觉
    2020-12-20 13:26

    You can access the parent class through its name:

    class ChildClass:
    
        name = None
    
        def __init__(self, stream):
            idx = stream.read_ui16()
            self.name = ParentClass.constant_pool[idx]
    

    Then again, I'm not sure I understand what you are trying to achieve.

提交回复
热议问题