[New-bugs-announce] [issue4998] fractions are mutable
Somelauw
report at bugs.python.org
Mon Jan 19 13:41:22 CET 2009
New submission from Somelauw <Somelauw at yahoo.com>:
>>> 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.
----------
components: Library (Lib)
messages: 80161
nosy: Somelauw
severity: normal
status: open
title: fractions are mutable
versions: Python 3.0
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4998>
_______________________________________
More information about the New-bugs-announce
mailing list