Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
bc95e90d
Commit
bc95e90d
authored
Jun 24, 2014
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Plain Diff
merge
parents
78774142
9944c971
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
0 deletions
+72
-0
Doc/tutorial/classes.rst
Doc/tutorial/classes.rst
+71
-0
Misc/ACKS
Misc/ACKS
+1
-0
No files found.
Doc/tutorial/classes.rst
View file @
bc95e90d
...
@@ -387,6 +387,77 @@ object and the argument list, and the function object is called with this new
...
@@ -387,6 +387,77 @@ object and the argument list, and the function object is called with this new
argument list.
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::
class Dog:
kind = 'canine' # class variable shared by all instances
def __init__(self, name):
self.name = name # instance variable unique to each instance
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind # shared by all dogs
'canine'
>>> e.kind # shared by all dogs
'canine'
>>> d.name # unique to d
'Fido'
>>> e.name # unique to e
'Buddy'
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::
class Dog:
tricks = [] # mistaken use of a class variable
def __init__(self, name):
self.name = name
def add_trick(self, trick):
self.tricks.append(trick)
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks # unexpectedly shared by all dogs
['roll over', 'play dead']
Correct design of the class should use an instance variable instead::
class Dog:
def __init__(self, name):
self.name = name
self.tricks = [] # creates a new empty list for each dog
def add_trick(self, trick):
self.tricks.append(trick)
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks
['roll over']
>>> e.tricks
['play dead']
.. _tut-remarks:
.. _tut-remarks:
Random Remarks
Random Remarks
...
...
Misc/ACKS
View file @
bc95e90d
...
@@ -238,6 +238,7 @@ Matt Chisholm
...
@@ -238,6 +238,7 @@ Matt Chisholm
Lita Cho
Lita Cho
Anders Chrigström
Anders Chrigström
Tom Christiansen
Tom Christiansen
Renee Chu
Vadim Chugunov
Vadim Chugunov
Mauro Cicognini
Mauro Cicognini
David Cinege
David Cinege
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment