Python: Class and instance variables

A class variable is a variable common to the class. Instance variables are variables that are used only by that instance. This is a memorandum of understanding for beginners.

Example

class enemy:
    #Class variables
    #Variables shared between instance objects
    ENEMY_MAX = 100  #Maximum number of enemies
    CHARACTER = 'O' #Enemy display character

    def __init__(self,x,y):
    #Instance variables
    #Variables not shared between instance objects
        self.alive = True #Enemy alive flag
        self.x = x        #Enemy x coordinate
        self.y = y        #Enemy y coordinate

To ensure access to class variables, use "class.variable name" both inside and outside the class.

Recommended Posts

Python: Class and instance variables
Python class variables and instance variables
[Python] class, instance
Python class, instance
Python class definitions and instance handling
About Class and Instance
Reference order of class variables and instance variables in "self. Class variables" in Python
I was addicted to confusing class variables and instance variables in Python
[python] Difference between variables and self. Variables in class
Trouble with Python pseudo-private variables and class inheritance
About Python variables and objects
Globalization of class instance variables
Python variables and object IDs
Class variables
[Python] Variables
Class variables
Use instance method and class method properly
perl objects and python class part 2.
Landmines hidden in Python class variables
perl objects and python class part 1.
[Python] Inherit a class with class variables
Python classes and instances, instance methods
[Python] How to play with class variables with decorator and metaclass
"Kanrika" python class
[Python] Use and and or when creating variables
Example of using class variables and class methods
About python, class
Python basic operation 3rd: Object-oriented and class
[Python] Difference between class method and static method
[python] Get a list of instance variables
Python is instance
I thought a Python class variable was an instance variable and died
#Python basics (class)
Talking about Python class attributes and metaclasses
Python variables and data types learned in chemoinformatics
I wrote a class in Python3 and Java
Python: Prepare a serializer for the class instance:
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
Practice applying functions and global variables in Python
[Python] How to sort instances by instance variables
[Python] Class type and usage of datetime module
[python] Compress and decompress
python syslog wrapper class
Python memorandum numbering variables
Python class (Python learning memo ⑦)
[Python] Get environment variables
Global and local variables 2
[Python] pip and wheel
case class in python
Batch design and python
Python iterators and generators
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
[Python] Class inheritance, override
python input and output
Python and Ruby split
python subprocess wrapper class