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

Marco Bizzarri marco.bizzarri at gmail.com
Fri Sep 5 07:17:29 CEST 2008

On Thu, Sep 4, 2008 at 8:59 PM, Carl Banks <pavlovevidence at gmail.com> wrote:
> You can write code to guard against this if you want:
> class A:
>    legal = set(["x"])
>    def __setattr__(self,attr,val):
>        if attr not in self.legal:
>            raise AttributeError("A object has no attribute '%s'" %
> attr)
>        self.__dict__[attr] = val
>    def __init__(self,x):
>        self.y = x
> I suspect most people who go into Python doing something like this
> soon abandon it when they see how rarely it actually catches anything.
> Carl Banks
> --
> http://mail.python.org/mailman/listinfo/python-list


Carl, I think I did not explained what I was asking the right way.


I'm not asking: "how can I do this sort of checks in Python": as I
stated before, if I want them, I will go for Java, or some other
language like that.

I understand that Python is a balance between different forces (like
any software object around the world) and I'm simply asking some
pointers to the discussion leading to this balance.

That's all.

Marco Bizzarri

More information about the Python-list mailing list