[Tutor] Property Question (Was: RE: Overloading assignment operator)
Barry.Carroll at psc.com
Fri Feb 9 03:07:40 CET 2007
> -----Original Message-----
> From: Tony Cappellini [mailto:cappy2112 at gmail.com]
> Sent: Thursday, February 08, 2007 5:41 PM
> To: Carroll, Barry
> Subject: re:Overloading assignment operator
> Hello Barry
> I'm trying to understand you post
> my question is, should this line
> result = property(get_result, set_result)
> actually be
> self.result = property(get_result, set_result)
> if result is an instance variable?
> Or did you intend it to be a class variable?
> Properties always confused me.
> Overloading assignment operator
> Carroll, Barry Barry.Carroll at psc.com
> Wed Jan 24 00:32:34 CET 2007
> Hello, Achim,
> * ...here is where
> * properties become useful. you can create a getter, setter, and even
> * deleter and doc string if you want. here's how you use it... add the
> * following to your class:
> * def get_result(self):
> * return self.__result
> * def set_result (self, expression):
> * self.__result = expression
> * result = property(get_result, set_ result, doc='result of
> * -----
> I have tested this using the admittedly simple-minded code snipped
> @BCARROLL[Python]|3> class Aclass:
> |.> def __init__(self):
> |.> __result = None
> |.> def get_result(self):
> |.> return self.__result
> |.> def set_result (self, result):
> |.> self.__result = result
> |.> result = property(get_result, set_result,
> doc='result of expression')
> @BCARROLL[Python]|5> a = Aclass()
> @BCARROLL[Python]|7> a.result = 2*3
> @BCARROLL[Python]|8> a.result
> <8> 6
> @BCARROLL[Python]|9> a.result = 25.0 * 5.25
> @BCARROLL[Python]|10> a.result
> <10> 131.25
First off, it's always a good idea to respond to the mailing list
instead of directly to an individual. Everyone benefits from the
information instead of just one person. Besides, if I make a mistake
(something I fo depressingly often) someone else on the list can correct
it so you get the right info.
Anyway, to your question: "result" vs "self.result". Look at the
interpreter session snippet above. The assignment to "result" is inside
the class definition, so it is an attribute of Aclass. At this point
"Aclass.result" is indeed a class variable. Or, perhaps more correctly,
a class property.
However, when "a" is set to "Aclass()", an instance of Aclass is created
and given the name "a". "a.result" is therefore an instance variable.
I never use the class variable "Aclass.result".
Does that help any?
barry.carroll at psc.com
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
More information about the Tutor