About the use of **args
jacek.generowicz at cern.ch
Wed Dec 10 14:14:44 CET 2003
Zunbeltz Izaola <zunbeltz at wm.lc.ehu.es.XXX> writes:
> I'm starting a new proyect and i'm in doubt about diferent interfaces
> for my clases. My clases will have a lot of attributes and i'm want to
> know what aproach could be the best
> 1) Define one SetAttribute/GetAttribute pair of method for each
> 2) Define one SetAttribute/GetAttribute which argument is a key=value
> Any advaice?
3) Not have any setters or getters at all.
Ask yourself: "what is the point of getters and setters?".
The most common answers are:
a) Data hiding, privacy, etc.
b) To protect myself against breaking the interface when I change the
implementation of my object.
c) Give you and your users typing exercise.
In Python there is no enforced data hiding or privacy: you use a
leading underscore in the identifier name to indicate to your users
that something is _not_ part of the interface ... so (a) is irrelevant.
As for (b), Python provides properties, which allow you to replace an
attribute with a setter and/or a getter method, while maintaining the
illusion that there is only an attribute. Properties also allow you to
implement read-only (or write-only! (or delete-only!!)) attributes.
(I won't comment on (c) :-)
If you are a fan of Bertrand Meyer, you might also answer
d) Attribute access and method calls should look the same.
I'm not aware of a Python mechanism to make that irrelevant.
More information about the Python-list