[issue18352] collections.Counter with added attributes are not deepcopied properly.
New submission from Olivier Gagnon: The following code shows that the Counter is not deepcopied properly. The same code with an user defined class or a dict is copied with the "b" attribute. import collections import copy count = collections.Counter() count.b = 3 print(count.b) # prints 3 count_copy = copy.deepcopy(count) print(count_copy.b) # raise AttributeError: 'Counter' object has no attribute 'b' -- components: Library (Lib) messages: 192239 nosy: Olivier.Gagnon priority: normal severity: normal status: open title: collections.Counter with added attributes are not deepcopied properly. type: behavior versions: Python 3.3 ___ Python tracker <http://bugs.python.org/issue18352> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18352] collections.Counter with added attributes are not deepcopied properly.
Olivier Gagnon added the comment: The dictionary and the set do not give the freedom to add dynamic attributes to them. I agree that the Counter should have the same behaviour. However, this will raise the same bug when we inherit from a Counter object. >>> class mylist(list): pass ... >>> l = mylist() >>> l.foo = "bar" >>> c = copy.deepcopy(l) >>> print(c.foo) # prints bar >>> class myCounter(Counter): pass ... >>> original = myCounter() >>> original.foo = "bar" >>> c = copy.deepcopy(original) >>> c.foo Traceback (most recent call last): File "", line 1, in AttributeError: 'myCounter' object has no attribute 'foo' The reduction function should still copy every dynamic attribute of the object. -- ___ Python tracker <http://bugs.python.org/issue18352> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18352] collections.Counter with added attributes are not deepcopied properly.
Olivier Gagnon added the comment: I can understand that the current behaviour can be correct in regard with the added attributes of the object. However, should I open a new issue for the following inheritance behaviour which the reduce function affects also. class myCounter(Counter): def __init__(self, bar, *args): self.foo = bar super().__init__(*args) class myDict(dict): def __init__(self, bar, *args): self.foo = bar super().__init__(*args) c = myCounter("bar") l = myDict("bar") print(c.foo) # prints bar print(l.foo) # prints bar cc = copy.copy(c) ll = copy.copy(l) print(cc.foo) # prints {} print(ll.foo) # prints bar -- ___ Python tracker <http://bugs.python.org/issue18352> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue18352] collections.Counter with added attributes are not deepcopied properly.
Olivier Gagnon added the comment: Yes I do have code that break because of this behaviour. I'm doing evolutionary algorithm using a framework called DEAP. This framework creates a type called individual at the runtime by subclassing a container and adding it a fitness attribute. Those individual are copied as not to modify every indivual when we work on a single one. AFAIK the only container that can't be used right now is the counter because the fitness is not copied. I'm sure I can come up with a hack to have this behaviour, but it does clash with other standard container type and there is no mention anywhere that the Counter should be different than every other container type in the python standard library. -- ___ Python tracker <http://bugs.python.org/issue18352> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com