Be careful when specifying the default argument value in Python3 series

Memo from P115 of http://www.amazon.co.jp/dp/4873117380

When defining a function in python, you can set a default value for the argument, but the value of the default value is calculated when the function is defined </ b>, not when the function is executed. ..

Therefore, if you define the function as follows, the list is not empty, and the value at the time of the previous call remains after the second time.

In [32]: def buggy(arg, result=[]):
   ....:     result.append(arg)
   ....:     print(result)
   ....:

In [33]: buggy('a')
['a']

In [34]: buggy('b')
['a', 'b']

As much as possible, it is a point to prevent bugs by specifying immutable default values.

There are various ways to avoid this, but for example, make it explicit that it is the first call as follows.

In [35]: def nonbuggy(arg, result=None):
   ....:     if result is None:
   ....:         result = []
   ....:     result.append(arg)
   ....:     print(result)
   ....:
In [36]: nonbuggy('a')
['a']

In [37]: nonbuggy('b')
['b']

Recommended Posts

Be careful when specifying the default argument value in Python3 series
Get the value while specifying the default value from dict in Python
The timing when the value of the default argument is evaluated is different between Ruby and Python.
When will the default arguments be bound in python? When variables are bound in closure lazy evaluation.
Master the type in Python? (When should type check be done)
[Python] Be careful when using print
What to do when the value type is ambiguous in Python?
When specifying multiple keys in python sort
⚠️ Be careful with Python's default argument values ⚠️
Make the default value of the argument immutable
Find the divisor of the value entered in python
Isn't there a default value in the dictionary?
[pandas] When specifying the default Index label in the at method, "" is not required
Python note: When the pip command cannot be used
How to retrieve the nth largest value in Python
Python default argument trap
Initial settings when using the foursquare API in python
Be careful when differentiating the eigenvectors of a matrix
Python default argument behavior
Make the default value of the argument immutable (article explanation)
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
Get the value selected in Selenium Python VBA pull-down
The value of meta when specifying a function with no return value in Dask dataframe apply
Check the argument type annotation when executing a function in Python and make an error
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
Effective Python Note Item 20 Use None and the documentation string when specifying dynamic default arguments
Code that sets the default value in case of AttributeError
Solution when the image cannot be displayed with tkinter [python]
[Python3] Call by dynamically specifying the keyword argument of the function
Be careful when assigning Series as a column to pandas.DataFrame
How to get the last (last) value in a list in Python
Solve the Japanese problem when using the CSV module in Python.
Scripts that can be used when using bottle in Python
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
Download the file in Python
Find the difference in Python
Shapley value calculation in Python
Attention when os.mkdir in Python
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
Things to watch out for when using default arguments in Python
Be careful of LANG for UnicodeEncodeError when printing Japanese with Python 3
I got an AttributeError when mocking the open method in python
Precautions when using a list or dictionary as the default argument
Mode line when you open the appropriate Python code in Vim
[Python] Execution time when a function is entered in a dictionary value
[Python] The value written in Openpyxl is displayed in exponential notation (E).
Build Python environment on Ubuntu (when pip is not the default)
Jupyter Notebook 6.0.2 cannot be installed in the Python 2.7 environment created in Anaconda
Solve the smallest value in Python (equivalent to paiza rank D)
Precautions when giving default values to arguments in Python function definitions
[Python] When the priority is the same in Priority Queue, it can be acquired in the order in which it was added to the queue.
Getting the arXiv API in Python
Precautions when using pit in Python
Check the behavior when assigning Python
Function argument type definition in python
[Python] Find the second smallest value.
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Behavior when listing in Python heapq
Get the desktop path in Python