Question on class member in python
Peter Otten
__peter__ at web.de
Mon Oct 17 04:36:34 EDT 2005
Johnny Lee wrote:
> Class A:
> def __init__(self):
> self.member = 1
>
> def getMember(self):
> return self.member
>
> a = A()
>
> So, is there any difference between a.member and a.getMember? thanks
> for your help. :)
Yes. accessor methods for simple attributes are a Javaism that should be
avoided in Python. You can always turn an attribute into a property if the
need arises to do some calculations behind the scene
>>> class A(object):
... def getMember(self):
... return self.a * self.b
... member = property(getMember)
... def __init__(self):
... self.a = self.b = 42
...
>>> A().member
1764
I. e. you are not trapped once you expose a simple attribute.
Peter
More information about the Python-list
mailing list