Best way to set/get an object property
wuwei23 at gmail.com
Mon Aug 25 10:37:26 CEST 2008
On Aug 25, 4:56 pm, Hussein B <hubaghd... at gmail.com> wrote:
> AFAIUY (understand you), what it is called a property in Java, it is
> called an attribute in Python?
> Why Python encourages direct access to object's attributes?
The simplest answer is "Because Python is not Java" :)
Speaking of which, have you read the blog post of the same name? It
might be useful given your Java background: http://dirtsimple.org/2004/12/python-is-not-java.html
> setters/getters considered vital in OOP (encapsulation)?
Not at all. They're definitely part of the mechanism that Java
provides for encapsulation, sure. However, because Python provides a
consistent interface for accessing attributes and properties, you
don't need to define a property unless your code requires it. If all
your getters & setters are doing is reading & writing to an attribute,
then why not just r&w directly to the attribute? If you later need to
add more complexity to that process, you can easily create a property
without having to change how any other piece of code refers to that
property, given it shares the same interface with attributes.
More information about the Python-list