Python overriding getter without setter

前端 未结 1 1995
长发绾君心
长发绾君心 2020-11-29 06:29
class human(object):
    def __init__(self, name=\'\'):
        self.name = name

    @property
    def name(self):
        return self._name

    @name.setter
    d         


        
相关标签:
1条回答
  • 2020-11-29 07:20

    Use just the .getter decorator of the original property:

    class superhuman(human):
        @human.name.getter
        def name(self):
            return 'super ' + self._name
    

    Note that you have to use the full name to reach the original property descriptor on the parent class.

    Demonstration:

    >>> class superhuman(human):
    ...     @human.name.getter
    ...     def name(self):
    ...         return 'super ' + self._name
    ... 
    >>> s = superhuman('john')
    >>> print s.name
    super john
    >>> s.name = 'jack'
    >>> print s.name
    super jack
    

    The property descriptor object is just one object, even though it can have multiple methods associated with it (the getter, setter and deleter). The .getter, .setter and .deleter decorator functions provided by an existing property descriptor return a copy of the descriptor itself, with that one specific method replaced.

    So in your human base class what happens is that you first create the descriptor with the @property decorator, then replace that descriptor with one that has both a getter and a setter with the @name.setter syntax. That works because python decorators replace the original decorated function with the same name, it basically executes name = name.setter(name). See How does the @property decorator work? for the details on how that all works.

    In your subclass you simply use that trick to create a new copy of the descriptor with just the getter replaced.

    0 讨论(0)
提交回复
热议问题