How to write Smart Python programs?

Bruno Desthuilliers onurb at xiludom.gro
Wed Oct 11 13:28:32 CEST 2006


Antoine De Groote wrote:
>> Googling for "python is not java" may be a good start. Also, here are 2
>> common C-style smells:
> 
> Ok, the first Google result
> (http://dirtsimple.org/2004/12/python-is-not-java.html) says this
> somewhere:
> 
> "Getters and setters are evil. Evil, evil, I say! Python objects are not
> Java beans. Do not write getters and setters. This is what the
> 'property' built-in is for. And do not take that to mean that you should
> write getters and setters, and then wrap them in 'property'. (1) That
> means that until you prove that you need anything more than a simple
> attribute access, don't write getters and setters. They are a waste of
> CPU time, but more important, they are a waste of programmer time. Not
> just for the people writing the code and tests, but for the people who
> have to read and understand them as well.
> 
> In Java, you have to use getters and setters because using public fields
> gives you no opportunity to go back and change your mind later to using
> getters and setters. (2) So in Java, you might as well get the chore out
> of the way up front. In Python, this is silly, because you can start
> with a normal attribute and change your mind at any time, without
> affecting any clients of the class. (3) So, don't write getters and
> setters."
> 
> For the record, I'm coming from Java, but I have some Python experience
> already.
> 
> Now here's what I don't understand.
> 
> What exactly is meant by (1)? The python library reference includes this
> as an example:
> 
> class C(object):
>     def __init__(self): self.__x = None
>     def getx(self): return self._x
>     def setx(self, value): self._x = value
>     def delx(self): del self._x
>     x = property(getx, setx, delx, "I'm the 'x' property.")
> 
> To me this seems contradictory. Why would one not want to do something
> that is included in the documentation? Or am I just confused? Does
> anybody have an idea how to put me in the right direction?

Have you tried running the above snippet in a python shell ?

>>> c = C()
>>> c.x = 42
>>> c.x
42


> And what does property mean anyway? 

See above. A property is a computed attribute : you access it like a
'data' attribute, but it really uses getters/setters. The point here is
that client code doesn't know nor need to know if it's a plain attribute
or a computed one. This let you change implementation (turn a plain
attribute into a computed one) without breaking the API. So the real
thing is not "don't use getters and setters", but "don't bother with
getters and setters, you'll be able to add them transparently if and
when needed".

As a side node, the getters/setters of a property are often either
marked as implementation (the _leading_underscore convention) or deleted
from the class's dict once the property is created or 'masked' one way
or another so they don't pollute the class's namespace.

Also and FWIW, properties are just one possible application of the
descriptor protocol. If you want to grasp Python's object model, I'd
recommand that you read about descriptors and metaclasses.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list