Issue 11974: Class definition gotcha.. should this be documented somewhere? (original) (raw)
Issue11974
Created on 2011-05-01 18:34 by sleepycal, last changed 2022-04-11 14:57 by admin. This issue is now closed.
Files | |||
---|---|---|---|
File name | Uploaded | Description | Edit |
obj.py | sleepycal,2011-05-01 18:34 | ||
issue11974.patch | reneighbor,2014-06-07 22:45 | Explanation and example code of scope precedence for mutable class attributes. | review |
class_instance_variables.diff | rhettinger,2014-06-21 22:10 | Tweaked explanation to also cover class variables versus instance variables |
| Repositories containing patches | | | | | ------------------------------------------------------------------------------------ | | | | | http://hg.python.org/cpython | | | |
Messages (9) | ||
---|---|---|
msg134919 - (view) | Author: Cal Leeming (sleepycal) | Date: 2011-05-01 18:34 |
So, when you create a class like this: class Obj: children = [] The 'children' object essentially becomes shared across all instances of Obj. To get around this, you have to use: class Obj: children = None def __init__(self): children = [] I have attached proof of concept code which can trigger this bug. Although I have almost 8 years of experience with Python, this is the first time I have actually noticed this, however, I am sure that similar things have happened in the past, and I just didn't investigate it enough to realise what was going on. Although this isn't a bug, it is likely that other advanced developers may also not know about, or have been caught out by this little gotcha. So, perhaps it might be worth documenting it somewhere? Let me know your thoughts. Cal | ||
msg134928 - (view) | Author: Georg Brandl (georg.brandl) * ![]() |
Date: 2011-05-01 20:40 |
Reclassifying as documentation issue. I was certain we had something about this in the tutorial, but couldn't find it with a quick look. It is in the FAQ ("how do I create static class data"), but that's not an obvious place to look. | ||
msg134951 - (view) | Author: Ezio Melotti (ezio.melotti) * ![]() |
Date: 2011-05-02 05:26 |
A good place where to add it would be http://docs.python.org/tutorial/classes.html. It might even get a small "Differences between class and instance attribute" section. | ||
msg219992 - (view) | Author: Renee Chu (reneighbor) | Date: 2014-06-07 22:45 |
Submitting a patch for documentation. | ||
msg221205 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2014-06-21 21:59 |
Renee, thanks for the patch. It looks pretty good. I've reworked a bit to accomplish the following: * Added a short introductory a short discussion about class variables versus instance variables. This addresses other questions that tend to arise irrespective of mutability. * Referenced the related discussion in"A Word About Names and Objects". * Referenced the term "mutable" in the glossary. * change "dangerous" to "unexpected" in accordance with the style guide and following Guido's own wording on the subject. * Change the class names from "A" and "B" to a "Dog" class because readers tend to find that the more abstract "A" and "B" examples are a little harder to follow. | ||
msg221388 - (view) | Author: Roundup Robot (python-dev) ![]() |
Date: 2014-06-24 01:03 |
New changeset 8e0b7393e921 by Raymond Hettinger in branch '2.7': Issue #11974: Add tutorial section on class and instance variables http://hg.python.org/cpython/rev/8e0b7393e921 | ||
msg221390 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2014-06-24 01:12 |
I'll load this into 3.4 and 3.5 shortly. | ||
msg221486 - (view) | Author: Roundup Robot (python-dev) ![]() |
Date: 2014-06-24 20:04 |
New changeset 8e5e04a1497f by Raymond Hettinger in branch '3.4': Issue #11974: Add tutorial section on class and instance variables http://hg.python.org/cpython/rev/8e5e04a1497f | ||
msg221487 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2014-06-24 20:05 |
Thanks for the patch. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:57:16 | admin | set | github: 56183 |
2014-06-24 20:05:10 | rhettinger | set | status: open -> closedresolution: fixedmessages: + |
2014-06-24 20:04:00 | python-dev | set | messages: + |
2014-06-24 01:12:41 | rhettinger | set | messages: + |
2014-06-24 01:11:48 | rhettinger | set | versions: + Python 3.5, - Python 3.2, Python 3.3 |
2014-06-24 01:03:30 | python-dev | set | nosy: + python-devmessages: + |
2014-06-22 18:24:59 | berker.peksag | set | nosy: + berker.peksag |
2014-06-21 22:10:05 | rhettinger | set | files: + class_instance_variables.diff |
2014-06-21 22:09:49 | rhettinger | set | files: - class_instance_variables.diff |
2014-06-21 21:59:10 | rhettinger | set | files: + class_instance_variables.diffmessages: + |
2014-06-21 03:24:45 | rhettinger | set | assignee: docs@python -> rhettingernosy: + rhettinger |
2014-06-07 22:45:36 | reneighbor | set | files: + issue11974.patchnosy: + reneighbormessages: + hgrepos: + hgrepo255keywords: + patch |
2012-11-08 08:34:04 | ezio.melotti | set | keywords: + easystage: needs patchversions: + Python 3.2, Python 3.3, Python 3.4 |
2011-05-02 05:26:37 | ezio.melotti | set | nosy: + ezio.melottimessages: + |
2011-05-01 20:40:40 | georg.brandl | set | nosy: + georg.brandl, docs@pythonmessages: + assignee: docs@pythoncomponents: + Documentation, - Interpreter Core |
2011-05-01 18:34:48 | sleepycal | create |