[Tutor] @property for old style classes vs new style classes

monikajg at netzero.net monikajg at netzero.net
Thu Sep 15 00:40:22 EDT 2016


Hi:
Im studying @property, @var.setter and @var.deleter.
I understand how they work in new style classes. Even though I do not use old style classes it would be interesting to understand what is going on behind the scenes. I can try to make assumptions but I do not want to because they might be incorrect. 
Could somebody please explain what is going on for old style classes for the below code:
class GetSet():

    def __init__(self, value):
        self.attrval = value

    @property
    def var(self):
        print "getting the var attribute"
        return self.attrval
    @var.setter 
    def var(self,value):
        print "setting the var attribute"
        self.attrval = value

    @var.deleter
    def var(self):
        print "deleting the var attribute"
        self.attrval = None

me = GetSet(5)
me.var = 1000
print me.var
del me.var
print me.var

Output:
1000
getting the var attribute
5
>>>


Same code but with new style classes (this one I understand):
class GetSet(object):

    def __init__(self, value):
        self.attrval = value

    @property
    def var(self):
        print ("getting the var attribute")
        return self.attrval
    @var.setter
    def var(self,value):
        print ("setting the var attribute")
        self.attrval = value

    @var.deleter
    def var(self):
        print ("deleting the var attribute")
        self.attrval = None

me = GetSet(5)
me.var = 1000
print (me.var)
del me.var
print (me.var)

Output:
setting the var attribute
getting the var attribute
1000
deleting the var attribute
getting the var attribute
None

Thank you very much
Monika
____________________________________________________________
Unlock Visions (Sponsored by Content.Ad)
1 Odd Method 'Restores' Your 20/20 Vision. Try This
http://thirdpartyoffers.netzero.net/TGL3241/57da2673b610726731820st03duc


More information about the Tutor mailing list