Getters and setters in python, common practise

  • Thread starter =?ISO-8859-1?Q?Petter_Holmstr=F6m?=
  • Start date
?

=?ISO-8859-1?Q?Petter_Holmstr=F6m?=

Hello,

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?

Thanks in advance,

-Petter-
 
M

Max M

Petter said:
Hello,

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?

Start with simple object attributes, then convert to attribute functions
using get/set-ers when needed.

regards Max M
 
P

Peter Hansen

Petter said:
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
 
?

=?ISO-8859-1?Q?Petter_Holmstr=F6m?=

Peter said:
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.

I have not found any documentation on this topic. Could you please point
me to some?

-Petter-
 
J

Jack Diederich

Start with simple object attributes, then convert to attribute functions
using get/set-ers when needed.
Agreed, do a search on some keywords plus "considered (evil|harmful)"

I consider get/set harmful 99% of the time, so read the rest of this
post as opinion.

Getter/Setters seem object oriented, but they usually aren't.
You are hiding nothing, you aren't abstacting anything either.
Someone is still depending on accessing a very particular attribute of
your class. If the same amount of code has to be changed when you
alter a getset method as when you alter a member your getset
methods are just adding obscurity and call overhead with no reward.

The object you are calling should get its state explicitly passed in
during __init__, and if it needs something else it should be passed in
on a method call. get/set methods are typically an indication that an
instance is 'peeking' at something else to figure out what to do.

Make "grep counts" your mantra. Passing things only at the time they
are actually needed makes things obvious.

import amodule
def myfunc(self):
# obvious, only gets what it needs.
amodule.launch_debugger(use_globals=self.vars)

import amodule
def myfunc(self):
# has to get 'vars' from self, might silently grow to do evil later.
amodule.launch_debugger(self)

If you limit what methods can use to what they are explicitly given it
also makes writing unit tests much, much easier.

-jackdied
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top