I'm having trouble with instance variables being inherited in Python

What?

It's really like "What?"

verification code

Code in question

# -*- coding: utf-8 -*-


class Hoge():
    def __init__(self, data=[]):
        self.data = data

    def add_data(self):
        self.data.append(u"ʅ(´◔౪◔)ʃ")

if __name__ == "__main__":
    hoge = Hoge()
    hoge.add_data()
    hoge2 = Hoge()

    print hoge2.data[0]

When executed, ʅ (´◔౪◔) ʃ will be displayed.

???????

If you do the following, data will not be inherited by hoge2

# -*- coding: utf-8 -*-


class Hoge():
    def __init__(self):
        self.data = []

    def add_data(self):
        self.data.append(u"ʅ(´◔౪◔)ʃ")

if __name__ == "__main__":
    hoge = Hoge()
    hoge.add_data()
    hoge2 = Hoge()

    print hoge2.data


Furthermore, if you put a string instead of a list, it will not be inherited even if you put an argument in the constructor

# -*- coding: utf-8 -*-


class Hoge():
    def __init__(self, data=''):
        self.data = data

    def add_data(self):
        self.data = u"ʅ(´◔౪◔)ʃ"

if __name__ == "__main__":
    hoge = Hoge()
    hoge.add_data()
    hoge2 = Hoge()

    print hoge2.data

Why... Why inherits instance variables when you put the default value in the argument of the constructor?

I don't know the cause, but be careful. If you know the cause, I would appreciate it if you could comment.

Recommended Posts

I'm having trouble with instance variables being inherited in Python
I'm having trouble with Django's Twitter authentication
Trouble with Python pseudo-private variables and class inheritance
Python: Class and instance variables
Scraping with selenium in Python
For those who are having trouble drawing graphs in python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Handle environment variables in Python
Working with sounds in Python
Scraping with Selenium in Python
Reference order of class variables and instance variables in "self. Class variables" in Python
Scraping with Tor in Python
Tweet with image in Python
Python class variables and instance variables
Combined with permutations in Python
If you are having trouble with timeouts when implementing Slack's SlashCommand in Python on AWS Lambda
I was addicted to confusing class variables and instance variables in Python
Number recognition in images with Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Landmines hidden in Python class variables
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Using global variables in python functions
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Are you having trouble with "pipenv.exceptions.ResolutionFailure"?
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
[Python] Inherit a class with class variables
Get started with Python in Blender
Working with DICOM images in Python
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
[Python3] Dynamically define global variables in functions
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
How to access environment variables in Python
Device monitoring with On-box Python in IOS-XE
How to dynamically define variables in Python
Put TensorFlow in P2 instance with pip3
Try working with binary data in Python
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Page cache in Python + Flask with Flask-Caching
Post Test 3 (Working with PosgreSQL in Python)
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Dealing with "years and months" in Python