I wanted to be careful about the behavior of Python's default arguments

The behavior of the default argument of python seems to be misunderstood and scary, so be careful.

➜  ~ python
Python 2.7.11 (default, Feb  2 2016, 21:44:54)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def func(arg={}):
...     return arg
...
>>> foo = func()
>>> foo['a'] = 'foo'
>>>
>>> bar = func()
>>> bar['b'] = 'bar'
>>> print(foo)
{'a': 'foo', 'b': 'bar'}
>>> print(bar)
{'a': 'foo', 'b': 'bar'}

The default arguments are shared between calls because they are evaluated only once when the module is loaded. I wonder why I made this specification ...

Recommended Posts

I wanted to be careful about the behavior of Python's default arguments
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
About the behavior of yield_per of SqlAlchemy
I wanted to challenge the classification of CIFAR-10 using Chainer's trainer
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
About the behavior of enable_backprop of Chainer v2
⚠️ Be careful with Python's default argument values ⚠️
About the arguments of the setup function of PyCaret
I tried to summarize the logical way of thinking about object orientation.
I tried to predict the behavior of the new coronavirus with the SEIR model.
About the behavior of copy, deepcopy and numpy.copy
I wanted to play with the Bezier curve
I tried to touch the API of ebay
I tried to correct the keystone of the image
About the behavior of Model.get_or_create () of peewee in Python
About the behavior of Queue during parallel processing
I want to customize the appearance of zabbix
I just wanted to understand Python's Pickle module
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
The and operator of Python's evaluation expression seems to be evaluated from the left side expression
I used Python to find out about the role choices of the 51 "Yachts" in the world.
I just wanted to extract the data of the desired date and time with Django
You have to be careful about the commands you use every day in the production environment.
I want to be notified of the connection environment when the Raspberry Pi connects to the network
I want to grep the execution result of strace
I wanted to use the Python library from MATLAB
I want to fully understand the basics of Bokeh
I checked the default OS and shell of docker-machine
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to classify the voices of voice actors
I want to increase the security of ssh connections
I tried to summarize the string operations of Python
I tried to dig deeper about safety while calculating the stochastic finality of Proof of Work
Summary of Python implementation know-how and tips that AI engineers want to be careful about
I tried to find the entropy of the image with python
I tried to find out the outline about Big Gorilla
[Horse Racing] I tried to quantify the strength of racehorses
A note on the default behavior of collate_fn in PyTorch
I wanted to solve the Panasonic Programming Contest 2020 with Python
I tried to find the average of the sequence with TensorFlow
Tank game made with python About the behavior of tanks
I want to use only the normalization process of SudachiPy
I want to get the operation information of yahoo route
I made a function to check the model of DCGAN
A memo about the behavior of bowtie2 during multiple hits
I wanted to worry about execution time and memory usage
[Python] I tried to visualize the follow relationship of Twitter
I want to judge the authenticity of the elements of numpy array
I tried a little bit of the behavior of the zip function
[Machine learning] I tried to summarize the theory of Adaboost
I want to know the features of Python and pip
I captured the Touhou Project with Deep Learning ... I wanted to.
I tried to fight the Local Minimum of Goldstein-Price Function
Keras I want to get the output of any layer !!
I want to know the legend of the IT technology world
I sent the data of Raspberry Pi to GCP (free)
About the ease of Python
About the components of Luigi
About the features of Python