On Fri, Jul 28, 2017 at 10:09 AM, Mike Miller <[email protected]> wrote:
> I've never liked that error message either:
>
> >>> object().foo = 'bar'
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'object' object has no attribute 'foo'
>
>
> Should say the "object is immutable," not writable, or something of the
> sort.
As Ivan said, this is to do with __slots__. It's nothing to do with
immutability:
>>> class Demo:
... __slots__ = 'spam'
...
>>> x = Demo()
>>> x.spam = 1
>>> x.ham = 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Demo' object has no attribute 'ham'
If the object has a __dict__, any unknown attributes go into there:
>>> class Demo2(Demo): pass
...
>>> y = Demo2()
>>> y.ham = 2
>>> y.spam = 3
>>> y.__dict__
{'ham': 2}
which prevents that "has no attribute" error.
ChrisA
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/