osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Extending property using a Subclass - single method - why Super(Baz, Baz).name.__set__ ?


class Foo(object):
    @property
    def name(self):
        if hasattr(self, '_name'):
            print('Foo name', self._name)
            return self._name
        else: 
            return 'default'

    @name.setter
    def name(self, value):
        print('Foo', self)
        self._name = value
        print(self._name)
        
    @name.deleter
    def name(self):
        print('del')
        self._name = None
    
    print('Foo', name)

class Baz(Foo):
    @property
    def name(self):
        print('Baz wrapper around getter')
        return super().name
        
    @Foo.name.setter
    def name(self, value):
        print('Baz wrapper around setter')
        print(self)
        print(super(Baz,Baz).name, value)
        return super(Baz, Baz).name.__set__(self, value)
    
b = Baz()
print('print', b.name)
b.name = 'v'
print(b.name)

Why do we user super(Baz, Baz) - are we setting a class variable called 
Baz.name which would trigger Baz._name = value?

We are essentially doing:
Foo.name.__set__(Baz, value) ?

How come 'self' is not used.. like in the traditional property way where 
we pass an instance reference instead of a class?