Notes around Python3 assignments

Function definitions and assignments are new objects every time

Something that seems to have been seen somewhere.

In[1]


def a():
    print('a')
f = a
f()
def a():
    print('b')
f()

Out[1]


a
a

The same is true for λ.

In[2]


a = lambda: print('a')
f = a
f()
a = lambda: print('b')
f()

Out[2]


a
a

If it is not an assignment, it behaves "intentionally".

In[3]


a = [1]
f = a
print(f)
a.append(1)
print(f)
a = [2]
a.append(2)
print(f)

Out[3]


[1]
[1, 1]
[1, 1]

Details are as commented.

Hen that has or does not affect the global

Let's try what happens when we assign it in a function!

In[4]


l = 1
def a():
    l = 2
a()
l

Out[4]


1

I failed in sabotage. But using lists can be destructive! dangerous!

In[5]


l = [1]
def a():
    l[0] = 2
a()
l[0]

Out[5]


2

Dokan ... The point (?) Of this sabotage is that you can change the contents without passing it as an argument. This is certainly happening when you define variables in a function and use closures.

If you append to the default argument, it will be a big deal.

PyCharm gives a kind message like the following, so I tried it.

Default argument value is mutable

This inspection detects when a mutable value as list or dictionary is detected in a default value for an argument. Default argument values are evaluated only once at function definition time, which means that modifying the default value of the argument will affect all subsequent calls of the function.

In[6]


def a(x=[]):
    x.append(1)
    return x
a()
a()
a()

Out[6]


[1,1,1]

What! ?? Similarly, the following can be done.

In[7]


def a(x=[0]):
    x[0] += 1
    return x[0]
a()
a()
a()

Out[7]


3

Recommended Posts

Notes around Python3 assignments
Python scraping notes
Python study notes _000
Python learning notes
Python beginner notes
Python study notes_006
python C ++ notes
Python study notes _005
Python grammar notes
Python Library notes
python personal notes
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Python package development notes
python decorator usage notes
Python ipaddress package notes
[Personal notes] Python, Django
Python Pickle format notes
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Matlab => Python migration notes
Notes using Python subprocesses
Python try / except notes
Python framework bottle notes
Python notes using perl-ternary operator
O'Reilly python3 Primer Learning Notes
Web scraping notes in python3
Python standard unittest usage notes
python * args, ** kwargs Usage notes
Python notes using perl-special variables
Python Tkinter notes (for myself)
[Python] Notes on data analysis
Python data analysis learning notes
Notes on installing Python on Mac
Get Evernote notes in Python
Notes on installing Python on CentOS
Looking back on Python 2020 around me
Notes on Python and dictionary types
Minimum grammar notes for writing Python
Notes on using MeCab from Python
Assignments and changes in Python objects
Personal notes for python image processing
Python Pandas Data Preprocessing Personal Notes
Typing automation notes by Python beginners
Notes for me python csv graph
Notes on installing Python using PyEnv
Notes for Python file input / output
Notes on using rstrip with python.
Notes on accessing dashDB from python
(Personal notes) Python metaclasses and metaprogramming
Code tests around time in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
WEB scraping with Python (for personal notes)
Notes on PyQ machine learning python grammar
Blender 2.82 or later + python development environment notes
python notes: Modularization: __name__ == How to use'__main__'
Notes on nfc.ContactlessFrontend () for nfcpy in python
Learning notes from the beginning of Python 1
Notes on doing Japanese OCR with Python