Python - extending properties like you'd extend a function

前端 未结 4 1588
甜味超标
甜味超标 2021-01-11 17:42

Question

How can you extend a python property?

A subclass can extend a super class\'s function by calling it in the overloaded version, a

4条回答
  •  逝去的感伤
    2021-01-11 18:40

    The problem is that you're missing a self going into the parent class. If your parent is a singleton then a @staticmethod should work.

    class X():
        x=1
        @staticmethod
        def getx():
            return X.x
    
    class Y(X):
        y=2
        def getyx(self):
            return X.getx()+self.y
    
    wx = Y()
    wx.getyx()
    3
    

提交回复
热议问题