The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python: Class vs. Instance variables - Understand the basics of classes and objects
By Guest on 30th October 2022 03:56:26 AM | Syntax: PYTHON | Views: 207



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1.  
  2. 9.3.5. Class and Instance Variables
  3. -----------------------------------
  4.  
  5. Generally speaking, instance variables are for data unique to each
  6. instance and class variables are for attributes and methods shared by
  7. all instances of the class:
  8.  
  9.    class Dog:
  10.  
  11.        kind = 'canine'         # class variable shared by all instances
  12.  
  13.        def __init__(self, name):
  14.            self.name = name    # instance variable unique to each instance
  15.  
  16.    >>> d = Dog('Fido')
  17.    >>> e = Dog('Buddy')
  18.    >>> d.kind                  # shared by all dogs
  19.    'canine'
  20.    >>> e.kind                  # shared by all dogs
  21.    'canine'
  22.    >>> d.name                  # unique to d
  23.    'Fido'
  24.    >>> e.name                  # unique to e
  25.    'Buddy'
  26.  
  27. As discussed in A Word About Names and Objects, shared data can have
  28. possibly surprising effects with involving *mutable* objects such as
  29. lists and dictionaries. For example, the *tricks* list in the
  30. following code should not be used as a class variable because just a
  31. single list would be shared by all *Dog* instances:
  32.  
  33.    class Dog:
  34.  
  35.        tricks = []             # mistaken use of a class variable
  36.  
  37.        def __init__(self, name):
  38.            self.name = name
  39.  
  40.        def add_trick(self, trick):
  41.            self.tricks.append(trick)
  42.  
  43.    >>> d = Dog('Fido')
  44.    >>> e = Dog('Buddy')
  45.    >>> d.add_trick('roll over')
  46.    >>> e.add_trick('play dead')
  47.    >>> d.tricks                # unexpectedly shared by all dogs
  48.    ['roll over', 'play dead']
  49.  
  50. Correct design of the class should use an instance variable instead:
  51.  
  52.    class Dog:
  53.  
  54.        def __init__(self, name):
  55.            self.name = name
  56.            self.tricks = []    # creates a new empty list for each dog
  57.  
  58.        def add_trick(self, trick):
  59.            self.tricks.append(trick)
  60.  
  61.    >>> d = Dog('Fido')
  62.    >>> e = Dog('Buddy')
  63.    >>> d.add_trick('roll over')
  64.    >>> e.add_trick('play dead')
  65.    >>> d.tricks
  66.    ['roll over']
  67.    >>> e.tricks
  68.    ['play dead']
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes