getFoo1() vs. class.foo1

Steven Taschuk staschuk at
Sat Jun 14 03:32:15 CEST 2003

Quoth Manuel:
> class VerySimple:
>         foo1 = 1
>         foo2 = 2
>         foo3 = 3
> etc...
> Any professional developers tell me that
> I must use get and set (to private variable)
> method instead.

Java programmers often do this, for two reasons that I know of:

1. Java encourages you -- by design -- to hide your data from
other code, for fear that the other code will break you.  (Python
encourages a more trusting, friendly attitude.)

2. If you use separate get/set methods even for simple data, then
you don't have to worry about API compatibility when the day comes
that you want to make access to that data more complicated -- say,
computing it on the fly instead of storing it, or logging all
accesses, or whatever.  (In Python this is no longer a big
concern, since you can change a simple attribute into a property,
leaving your API unchanged.)

In short: don't do this in Python.  In Java it's okay.

Steven Taschuk                staschuk at
"I tried to be pleasant and accommodating, but my head
 began to hurt from his banality."   -- _Seven_ (1996)

More information about the Python-list mailing list