Message80161
>>> f = Fraction()
>>> f.a = 5
>>> f.__slots__
('_numerator', '_denominator')
>>> f.a
5
>>> f.__dict__
{}
When I create my own object, this doesn't happen.
>>> class Slots:
__slots__ = ("slot1", "slot2")
>>> a = Slots()
>>> a.slot3 = 6
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
a.slot3 = 6
AttributeError: 'Slots' object has no attribute 'slot3'
>>>
In python2 this only happens when __slots__ is a tuple. (When __slots__
is a list, it works correctly)
>>> class Slots:
__slots__ = ("slot1", "slot2")
>>> a = Slots()
>>> a.slot3 = 8
>>>
Here is a copy-paste from the python3 documentation:
Without a __dict__ variable, instances cannot be assigned new variables
not listed in the __slots__ definition. Attempts to assign to an
unlisted variable name raises AttributeError. If dynamic assignment of
new variables is desired, then add '__dict__' to the sequence of strings
in the __slots__ declaration.
Any non-string iterable may be assigned to __slots__. Mappings may also
be used; however, in the future, special meaning may be assigned to the
values corresponding to each key. |
|
Date |
User |
Action |
Args |
2009-01-19 12:41:23 | Somelauw | set | recipients:
+ Somelauw |
2009-01-19 12:41:22 | Somelauw | set | messageid: <1232368882.79.0.616892747667.issue4998@psf.upfronthosting.co.za> |
2009-01-19 12:41:21 | Somelauw | link | issue4998 messages |
2009-01-19 12:41:20 | Somelauw | create | |
|