python class properties

前端 未结 2 1285
情话喂你
情话喂你 2021-02-14 11:46

I\'m trying to find the best way to extend a class variable. Hopefully an example of the method I\'ve come up with so far will make this clear.

class A(object):         


        
2条回答
  •  北海茫月
    2021-02-14 12:17

    I definitively would go for instance-properties. (if I got it right, they are not bound to be static for your case?!)

    >>> class A:
    ...     @property
    ...     def foo(self):
    ...         return ['thin', 'another thing']
    ...
    >>> class B(A):
    ...     @property
    ...     def foo(self):
    ...         return super().foo + ['stuff', 'thing 3']
    ...
    >>> B().foo
    ['thin', 'another thing', 'stuff', 'thing 3']
    

提交回复
热议问题