Getters and setters in python, common practise

Peter Hansen peter at engcorp.com
Thu Apr 15 06:11:02 EDT 2004


Petter Holmström wrote:

> Having a Delphi/Object Pascal-background, I'm very used to using getters 
> and setters when designing my classes. What is the common practise in 
> the Python world? Should one use them or not?

Max described the common practice (don't use them until you actually
need them) but forgot to mention that in recent versions of Python
you can use "properties", in pretty much the same manner as Delphi
uses them.

Again though it's common, unless you are still tied to experience
in another language (such as Java), not to use even properties
until you actually have a case where it's really necessary.  Most
Python code just directly accesses the attributes and winds up
much cleaner as a result.

Even historically you could implement the equivalent of get/set
under the covers after the fact, using __getattr__ and its ilk,
but that's now been made mostly obsolete with properties.

-Peter



More information about the Python-list mailing list