[issue18352] collections.Counter with added attributes are not deepcopied properly.
Changes by Jesús Cea Avión j...@jcea.es: -- nosy: +jcea ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue18352 ___ ___ Python-bugs-list mailing list Unsubscribe: https://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 rep...@bugs.python.org 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.
Changes by Raymond Hettinger raymond.hettin...@gmail.com: -- assignee: - rhettinger priority: normal - low ___ Python tracker rep...@bugs.python.org 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.
Raymond Hettinger added the comment: I don't want the current behavior to change. The copy() method does not guarantee that it will copy added attributes. -- resolution: - rejected status: open - closed ___ Python tracker rep...@bugs.python.org 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.
Serhiy Storchaka added the comment: What about pickling? OrderedDict.__reduce__() saves added attributes, but Counter.__reduce__() does not. One of them should be changed for consistency. -- ___ Python tracker rep...@bugs.python.org 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 rep...@bugs.python.org 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.
Raymond Hettinger added the comment: Do you guys have any actual motivating use cases or real code that won't work because of the present design? Consistency arguments are somewhat weak and don't necessarily warrant an API change. AFAICT, there is no use for counters going out of their way to save attributes. Like many classes and types in Python, a subclasser is responsible for adding extra behavior if needed. -- ___ Python tracker rep...@bugs.python.org 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.
Serhiy Storchaka added the comment: Here is an updated patch. It includes a fix for OrderedDict.copy(). Counter's copy() and __reduce__() are simplified. Added tests for OrderedDict which checks that OrderedDict copying and pickling preserves dynamic attributes. -- Added file: http://bugs.python.org/file30887/counter_copy_attrs_2.patch ___ Python tracker rep...@bugs.python.org 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.
Vajrasky Kok added the comment: The question is whether we should give the freedom to user to add dynamic attribute to Counter object. Is this freedom has any practicality? Why do we want to add dynamic attributes to Counter object? Decimal object does not have this freedom. from decimal import Decimal z = Decimal('1.0') z.foo = 'a' Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'decimal.Decimal' object has no attribute 'foo' Actually I am not really sure about this. Maybe someone knows the answer. -- nosy: +vajrasky ___ Python tracker rep...@bugs.python.org 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 stdin, line 1, in module AttributeError: 'myCounter' object has no attribute 'foo' The reduction function should still copy every dynamic attribute of the object. -- ___ Python tracker rep...@bugs.python.org 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.
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 rep...@bugs.python.org 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.
Changes by Madison May madison@students.olin.edu: -- nosy: +madison.may ___ Python tracker rep...@bugs.python.org 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.
Serhiy Storchaka added the comment: Here is a preliminary patch. It changes copying and pickling to preserve instance attributes. Actually I'm not sure which attributes should be copied. I doubt -- should this be considered as a fix or as a new feature? Perhaps OrderedDict should be changed in same way. -- keywords: +patch nosy: +rhettinger, serhiy.storchaka stage: - patch review versions: +Python 3.4 -Python 3.3 Added file: http://bugs.python.org/file30759/counter_copy_attrs.patch ___ Python tracker rep...@bugs.python.org 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.
Eric Snow added the comment: OrderedDict already copies the instance dict in __reduce__(). Are you talking about something more than that? -- nosy: +eric.snow ___ Python tracker rep...@bugs.python.org 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.
Serhiy Storchaka added the comment: I mean OrderedDict.copy(). -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue18352 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com