[Python-ideas] namedtuple nit...
rosuav at gmail.com
Thu Jul 27 21:02:48 EDT 2017
On Fri, Jul 28, 2017 at 10:09 AM, Mike Miller <python-ideas at mgmiller.net> 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
As Ivan said, this is to do with __slots__. It's nothing to do with
>>> 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
which prevents that "has no attribute" error.
More information about the Python-ideas