What was surprising about Python classes

What surprised me

Imagine a code like this:

sample.py


class Sample:

    sample_list = []

    def __init__(self, args1):
        self.name = args1

    def add_args(self, args2):
        self.sample_list.append(args2)

When I run this code, the sample_list is shared between a and b as shown below.

>>> a = Sample('a')
>>> b = Sample('b')
>>> a.add_args('hoge')
>>> b.add_args('huga')
>>> a.sample_list
['hoge', 'huga']

Solution

You can prevent this problem by designing the class with instance variables as follows.

sample.py


class Sample:

    def __init__(self, args1):
        self.name = args1
        self.sample_list = []

    def add_args(self, args2):
        self.sample_list.append(args2)

Execution result

>>> a = Sample('a')
>>> b = Sample('b')
>>> a.add_args('hoge')
>>> b.add_args('huga')
>>> a.sample_list
['hoge']
>>> b.sample_list
['huga']

reference

Classes-Python 3.8.5 documentation

Recommended Posts

What was surprising about Python classes
Python: A Note About Classes 1 "Abstract"
[Python] What is @? (About the decorator)
[Python] About Executor and Future classes
What I was addicted to Python autorun
Talking about old and new Python classes
About python slices
About python comprehension
About Python tqdm.
About python yield
About python, class
About python inheritance
About python, range ()
About python decorators
What is python
About python reference
About Python decorators
[Python] About multi-process
What is Python
Two things I was happy about with Python 3.9
What I'm careful about in Python coding: comments, type annotations, data classes, enums
What I learned about AI / machine learning using Python (1)
About function arguments (python)
[Python] What is Pipeline ...
[Python] Memo about functions
Python classes are slow
Summary about Python3 + OpenCV3
About Python, for ~ (range)
About Python3 character code
[Python] Memo about errors
About Python development environment
Python: About function arguments
What I learned about AI / machine learning using Python (3)
Python, about exception handling
Python basic course (13 classes)
About Python Pyramid traversal
About Python3 ... (Ellipsis object)
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
[Python] What is virtualenv
What I was addicted to when using Python tornado
What I learned about AI / machine learning using Python (2)
What I was careful about when implementing Airflow with docker-compose
What I learned about AI and machine learning using Python (4)
What I was addicted to when migrating Processing users to Python
What I was worried about when displaying images with matplotlib
[Python] Find out about pip
Love was born in Python 2
Think about architecture in python
[Python] if __name__ == What is'__main__' :?
About python beginner's memorandum function
About the ease of Python
About the enumerate function (python)
About various encodings of Python 3
What about stock trading today?
Python classes learned in chemoinformatics
About Python, len () and randint ()
About Perl, Python, PHP, Ruby
About Python datetime and timezone
A memorandum about correlation [Python]