Understanding the pythonic way: why a.x = 1 is better than a.setX(1) ?

George Sakkis george.sakkis at gmail.com
Thu Sep 4 12:53:21 EDT 2008


On Sep 4, 7:09 am, "Marco Bizzarri" <marco.bizza... at gmail.com> wrote:
> Sorry... pressed enter but really didn't want to.
>
> As I said, let's say I have a class
>
> class A:
>     def __init__(self):
>          self.x = None
>
> Python makes the decision to allow the developers to directly access
> the attribute "x",  so that they can directly write: "a.x = 1", or
> whatever; this has for me the unfortunate side effect that if I write,
> for example "a.y = 1", when I really wanted to write "a.x = 1" no one
> cares about it, and I'm unable to spot this error until later.
>
> Of course, I know that while I'm fresh, I've a good knowledge of the
> code, and anything else, I will be able to avoid such stupid errors;
> however, I'm afraid of the times when I'm tired, when I have to put my
> hands on the code of someone else, and so on.

So what happens in Java (or any language for that matter) if there are
indeed two attributes x and y with the same type and you mistype the
one for the other ? Or if you meant to write x-y instead of y-x ?

When coding tired or on someone's else code, stupid errors are the
ones you should worry the least about.

George



More information about the Python-list mailing list