cpython: 8e5e04a1497f (original) (raw)

Mercurial > cpython

changeset 91356:8e5e04a1497f 3.4

Issue #11974: Add tutorial section on class and instance variables (Based on a patch from Renee Chu.) [#11974]

Raymond Hettinger python@rcn.com
date Mon, 23 Jun 2014 18:08:01 -0700
parents cc0f5d6ccb70
children a941bb617c2a
files Doc/tutorial/classes.rst Misc/ACKS
diffstat 2 files changed, 72 insertions(+), 0 deletions(-)[+] [-] Doc/tutorial/classes.rst 71 Misc/ACKS 1

line wrap: on

line diff

--- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -387,6 +387,77 @@ object and the argument list, and the fu argument list. +.. _tut-class-and-instance-variables: + +Class and Instance Variables +---------------------------- + +Generally speaking, instance variables are for data unique to each instance +and class variables are for attributes and methods shared by all instances +of the class:: +

+

+

+

+ +As discussed in :ref:tut-object, shared data can have possibly surprising +effects with involving :term:mutable objects such as lists and dictionaries. +For example, the tricks list in the following code should not be used as a +class variable because just a single list would be shared by all Dog +instances:: +

+

+

+

+

+ +Correct design of the class should use an instance variable instead:: +

+

+

+

+ + .. _tut-remarks: Random Remarks

--- a/Misc/ACKS +++ b/Misc/ACKS @@ -238,6 +238,7 @@ Matt Chisholm Lita Cho Anders Chrigström Tom Christiansen +Renee Chu Vadim Chugunov Mauro Cicognini David Cinege