[Tutor] Mutable Properties

Chris King g.nius.ck at gmail.com
Fri Oct 1 02:24:50 CEST 2010


  Dear Tutors,
     I noticed that when you make a property to represent a mutable value

*class Obj(object):
         def get(self):
                 print 'Get'
                 return self.prop
         def set(self, new):
                 print 'Set'
                 self.prop = new
     prop = property(get, set)

test = Obj()
test.prop = ['Hello']
*
and then try and use one of its methods.

*test.prop.append('World')

*It will treat it as a get, not a set.

*Output: Get

*Even thou you are basically changing its value.

*Before: ['Hello']
After: ['Hello', 'World']

*I know this happens because I'm not technically setting it to something 
else, just getting one of its properties.
I was wondering how to make it fire off set to for certain methods.

Sincerely,
     Me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100930/8f1a1186/attachment.html>


More information about the Tutor mailing list