On Mar 23, 2010, at 9:58 AM, Ricardo Aráoz wrote:
> I'm confused here. I've googled and got even more confused.
> I always thought attribute and property were synonyms for a variable
> bound to a class and that a method was a procedure bound to a class.
> But it would seem I am wrong. Any help here?
You are correct about methods and attributes. Properties, in Python,
are a much richer concept: you access them like attributes, but under the hood
they implement getter/setter/deller methods that allow you to customize the
value of that property. Example:
class TestClass(object):
def _getMyProp(self):
try:
return self._propval
except AttributeError:
# prop hasn't been set yet
self._propval = "Ricardo"
def _setMyProp(self, val):
self._propval = str(val).title()
MyProp = property(_getMyProp, _setMyProp, None,
"Docstring for this property")
obj = TestClass()
print obj.MyProp
# -> 'Ricardo'
obj.MyProp = "DABO RULES, DUDE!"
print obj.MyProp
# -> 'Dabo Rules, Dude!'
This is a trivial example, but properties are what allow us to 'wrap'
the behaviors of wxPython by creating a simple interface that does an awful lot
of stuff for you.
-- Ed Leafe
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message:
http://leafe.com/archives/byMID/[email protected]