Python class variables and instance variables

To define a class variable in Python, you are supposed to declare the variable under the class name. That's what the official documentation says.

https://docs.python.jp/3/tutorial/classes.html#class-and-instance-variables

sample.py


   class Sample:
     name = 'name'
   
   s  = Sample()
   s2 = Sample()
   
   print(s.name) # 'name'
   print(s2.name) # 'name'

Obviously, both are displayed as "name".

However, if you change the name like this, it will no longer be a class variable.

sample.py


   s  = Sample()
   s2 = Sample()
   
   s.name = 'aaaa' 
   
   print(s.name) # 'aaaa'
   print(s2.name) # 'name'

When you do this, s.name is displayed as aaaa and s2.name is displayed as name. If it is shared by all objects, s2.name should also change to ʻaaaa`.

The answer is that strings are immutable, so if you refer to them via an object, they will automatically create an instance variable.

But when I tried Sample.name, both changed to aaaa. It has characteristics as a class variable. Is it interpreted by reading the air depending on whether the reference source is a class or an object?

Arrays and dictionary types are mutable, so doing the following will propagate the changes and both prints will show [1,2,3,4]. It is natural because it points to the same thing.

sample.py


   class Sample:
     a_list = [1,2,3]
   
   s  = Sample()
   s2 = Sample()
   s.a_list.append(4)
   print(s.a_list)  # [1,2,3,4]
   print(s2.a_list) # [1,2,3,4]

Forcibly summarizing

I wonder if it's okay to say that immutables are automatically switched to instance variables even if they are declared under the class, and mutables are still class variables. In principle, I will share it. However, immutable data becomes an instance variable when accessed via an object.

I wonder if it has such specifications.

Recommended Posts

Python: Class and instance variables
Python class variables and instance variables
Python class, instance
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.
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
Example of using class variables and class methods
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] Get environment variables
Python and numpy tips
Global and local variables 2
[Python] pip and wheel
case class in 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
Metaclass and is instance
Class methods and static methods
Python3, venv and Ansible
Python asyncio and ContextVar
YOLO Python wrapper class
Class notation in Python