python - Why is a function argument default value (an empty list) static? -


this question has answer here:

code:

def func(a=[]):     a.append(1)     print(a)  func() func() func() 

output:

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

i thought default value, list, re-assigned every time func called, , answer be:

[1] [1] [1] 

you "thought default value, list, re-assigned everytime func called." thought wrong. if learning python should work through the official tutorial @ point. here what says default arguments:

the default value evaluated once. makes difference when default mutable object such list, dictionary, or instances of classes.

read tutorial details.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -