def changer(L):
L.append(4)
L = [1, 2, 3]
changer(L)
print(L)
[1, 2, 3, 4]
Python is neither passed-by-value or passed-by-reference.
It's passed-by-assignment.
When using mutable defaults, the same object is reused each call.
To solve this, simply move the assign into the function body to solve.
def func(a=[]):
a.append(1)
print(a)
func()
func() # Change the same object
[1] [1, 1]
func(*iterable) # Pass all objects in iterable as individual positional arguments
func(**dict) # Pass all key/value pairs in dict as individual keyword arguments
def func(*name) # Matches and collects remaining positional arguments in a tuple
def func(**name) # Matches and collects remaining keyword arguments in a dictionaty
def func(*other, name) # Arguments that must be passed by keyword only in calls (Python3)
def func(*, name=value) # Arguments that must be passed by keyword only in calls (Python3)
def f(*args):
print(args)
f(1)
(1,)
def f(**args):
print(args)
f(a=1, b=2)
{'a': 1, 'b': 2}
def f(a, b, c, d):
print(a, b, c, d)
L = [1, 2, 3, 4]
f(*L)
1 2 3 4
def f(a, b, c, d):
print(a, b, c, d)
D = {"a": 1, "b": 2, "c": 3, "d": 4}
f(**D)
1 2 3 4
def f(a, *b, c):
print(a, b, c)
f(1, 2, c=4)
1 (2,) 4
f(1, 2, 3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-6f2568884585> in <module>() ----> 1 f(1, 2, 3) TypeError: f() missing 1 required keyword-only argument: 'c'
a, *b, c = 1, 2, 3, 4
print(a, b, c)
1 [2, 3] 4
All arguments following the * must be passed as keywords.
Keyword-only arguments must be specified after a single star, not two.
def f(a, *, b, c):
print(a, b, c)
f(1, 2, 3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-677b925ab41c> in <module>() 2 print(a, b, c) 3 ----> 4 f(1, 2, 3) TypeError: f() takes 1 positional argument but 3 were given
f(1)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-90b61b657670> in <module>() ----> 1 f(1) TypeError: f() missing 2 required keyword-only arguments: 'b' and 'c'
def f(a, *, b=2, c=4):
print(a, b, c)
f(1, 5)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-26693cd50d53> in <module>() 2 print(a, b, c) 3 ----> 4 f(1, 5) TypeError: f() takes 1 positional argument but 2 were given
f(1)
1 2 4
def return_multiple(x, y):
x = 2
y = 3
return x, y
x = "a"
y = "b"
x, y = return_multiple(x, y)
print(x, y)
2 3
In fact, it simulates returning two value by returning one tuple and unpack.
Python doesn't support call-by-reference.
The above way can be used to simulate it.
Functions are another kind of object that can be passed into a function.
def minmax(func, *args):
result = args[0]
for arg in args[1:]:
if func(arg, result):
result = arg
return result
def less_than(x, y):
return x < y
def greater_than(x, y):
return x > y
print(minmax(less_than, 1, 3, 5, 9, 2, 5, 8))
print(minmax(greater_than, 1, 3, 5, 9, 2, 5, 8))
1 9
By doing this, it can generalize functions to enhance reusibility.