This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Class definition gotcha.. should this be documented somewhere?
Type: enhancement Stage: needs patch
Components: Documentation Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: berker.peksag, docs@python, ezio.melotti, georg.brandl, python-dev, reneighbor, rhettinger, sleepycal
Priority: normal Keywords: easy, patch

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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) (Python triager) 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) * (Python committer) Date: 2014-06-24 01:12
I'll load this into 3.4 and 3.5 shortly.
msg221486 - (view) Author: Roundup Robot (python-dev) (Python triager) 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) * (Python committer) Date: 2014-06-24 20:05
Thanks for the patch.
History
Date User Action Args
2022-04-11 14:57:16adminsetgithub: 56183
2014-06-24 20:05:10rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg221487
2014-06-24 20:04:00python-devsetmessages: + msg221486
2014-06-24 01:12:41rhettingersetmessages: + msg221390
2014-06-24 01:11:48rhettingersetversions: + Python 3.5, - Python 3.2, Python 3.3
2014-06-24 01:03:30python-devsetnosy: + python-dev
messages: + msg221388
2014-06-22 18:24:59berker.peksagsetnosy: + berker.peksag
2014-06-21 22:10:05rhettingersetfiles: + class_instance_variables.diff
2014-06-21 22:09:49rhettingersetfiles: - class_instance_variables.diff
2014-06-21 21:59:10rhettingersetfiles: + class_instance_variables.diff

messages: + msg221205
2014-06-21 03:24:45rhettingersetassignee: docs@python -> rhettinger

nosy: + rhettinger
2014-06-07 22:45:36reneighborsetfiles: + issue11974.patch

nosy: + reneighbor
messages: + msg219992

hgrepos: + hgrepo255
keywords: + patch
2012-11-08 08:34:04ezio.melottisetkeywords: + easy
stage: needs patch
versions: + Python 3.2, Python 3.3, Python 3.4
2011-05-02 05:26:37ezio.melottisetnosy: + ezio.melotti
messages: + msg134951
2011-05-01 20:40:40georg.brandlsetnosy: + georg.brandl, docs@python
messages: + msg134928

assignee: docs@python
components: + Documentation, - Interpreter Core
2011-05-01 18:34:48sleepycalcreate