Re: [Tutor] Mutable Properties
"Chris King" wrote I noticed that when you make a property to represent a mutable value *class Obj(object): prop = property(get, set) test.prop = ['Hello'] Let's be clear, here you are assigning a list object to your property. and then try and use one of its methods. *test.prop.append('World') And here you use a method of that list object. The property is just a name that refers to the list object. You are not using a method of the property you are using a method of the object to which the property refers. *It will treat it as a get, not a set. Because it is a get. Your line of code is a shorthand way of writing theList = test.prop theList.append('World') *Even thou you are basically changing its value. You are not changing the property's value, it is still the same list object. The property has done exactly what you asked it to do - get you the list object so that you can perform an append operation on it. *I know this happens because I'm not technically setting it to something else, just getting one of its properties. You are getting the object you assigned because that's what you asked it to do. Python is not psychic, it cannot guess what you will do with the property value. I was wondering how to make it fire off set to for certain methods. The property mechanism only works for access to the property value. It has no way of knowing what you will do with that value once you get it. What you need to do is create your own kind of list that prints get/set when you access the list. It is the list you are manipulating not the test class. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mutable Properties
On 2:59 PM, Chris King wrote: On 9/30/2010 8:43 PM, Steven D'Aprano wrote: What problem are you trying to solve? There is likely another way to solve it. (Not necessarily an easy way, but we'll see.) So I'll have to create my own object which modifies each method to fire off the set method if the old form and the new form are unequal, there is no easy way? Try rephrasing that in English. Or just answer Chris's question. What problem are you trying to solve? The system will call the get() method if you're accessing the existing object, whether to look at it or modify it. It'll call the set() method if you're binding a new object to the attribute. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mutable Properties
On Fri, 1 Oct 2010 11:24:16 am Chris King wrote: > > What problem are you trying to solve? There is likely another way > > to solve it. (Not necessarily an easy way, but we'll see.) > > So I'll have to create my own object which modifies each method to > fire off the set method if the old form and the new form are unequal, > there is no easy way? I repeat... what problem are you trying to solve? Calling the set method for something that *doesn't* set a property doesn't sound like a solution to a problem to me. It sounds like a problem itself. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mutable Properties
On 9/30/2010 8:43 PM, Steven D'Aprano wrote: On Fri, 1 Oct 2010 10:24:50 am Chris King wrote: 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. That's because it is a get. You get the property, and then you call one of its methods. How could it be any different? The object stored in the property could be anything. How can the class know which methods modify it in place, and which ones don't? test.prop.sort() Did this modify the list or not? *Output: Get *Even thou you are basically changing its value. But changing a mutable value means modifying it in place, not re-assigning it. This is no different from: mylist = test.prop # this is a get mylist.append("something") # change it in place Since there is no assignment to prop, there is no set. *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. You can't. What problem are you trying to solve? There is likely another way to solve it. (Not necessarily an easy way, but we'll see.) So I'll have to create my own object which modifies each method to fire off the set method if the old form and the new form are unequal, there is no easy way? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mutable Properties
On Fri, 1 Oct 2010 10:24:50 am Chris King wrote: > 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. That's because it is a get. You get the property, and then you call one of its methods. How could it be any different? The object stored in the property could be anything. How can the class know which methods modify it in place, and which ones don't? test.prop.sort() Did this modify the list or not? > *Output: Get > > *Even thou you are basically changing its value. But changing a mutable value means modifying it in place, not re-assigning it. This is no different from: mylist = test.prop # this is a get mylist.append("something") # change it in place Since there is no assignment to prop, there is no set. > *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. You can't. What problem are you trying to solve? There is likely another way to solve it. (Not necessarily an easy way, but we'll see.) -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Mutable Properties
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mutable Properties
sorry, accidentally hit reply instead of reply to all On Thu, Sep 9, 2010 at 8:42 AM, Steven D'Aprano wrote: > Please don't reply privately to me unless you mean to ask me something > private or personal. > > If you send your reply to the tutor list, I'll respond there. > > > Regards, > > > > -- > Steven D'Aprano > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mutable Properties
On Thu, 9 Sep 2010 06:59:57 am Chris King wrote: > Dear Tutors, > I noticed that when you use a property to represent a mutable > value, I you try to use its methods, it will directly change the > value returned. I know this happens because I'm not really assigning > it to something new, but changing whats already there, which won't > fire off the set method. I was wondering if there was a way around > this. You're going to need to give an example of: (1) what you do; (2) what you want to happen; and (3) what actually happens. The simplest example I can think of is: class K(object): def __init__(self): self._private = [] def _getter(self): return self._private def _setter(self, value): self._private = list(value) seq = property(_getter, _setter) And in use: >>> k = K() >>> k.seq [] >>> k.seq.append(1) >>> k.seq [1] Works fine. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Mutable Properties
Dear Tutors, I noticed that when you use a property to represent a mutable value, I you try to use its methods, it will directly change the value returned. I know this happens because I'm not really assigning it to something new, but changing whats already there, which won't fire off the set method. I was wondering if there was a way around this. Sincerely, Me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor