Python - mutable default arguments to functions

2024/10/15 23:19:57

I was going through https://docs.python.org/3.5/tutorial/controlflow.html#default-argument-values. I modified the example there a little bit as below:

x = [4,5]
def f(a, L=x):L.append(a)return Lx = [8,9]
print(f(1,[6,7]))
print(f(2))
print(f(3))

The output I got was:

[6, 7, 1]
[4, 5, 2]
[4, 5, 2, 3]

Now, as per what I understood from the Python docs:

  1. The default values are evaluated at the point of function definition in the defining scope. So, L = [4,5]

  2. After the 1st call to f(), L = [6,7,1]. This is fine.

  3. What don't understand is output after 2nd call to f(). If L's value is shared between the calls to f(), then the output after 2nd function call should be [6,7,1,2]. L's value was shared between 2nd and 3rd call to f() but not between 1st and 2nd call.

Answer

This is because each time you call f it performs L=x, as x is a default value for L on you argument list of f.

So every time it actually appends to x.

https://en.xdnf.cn/q/117774.html

Related Q&A

Python: remove duplicate items from a list while iterating

I have a list named results, I want to get rid of the duplicate items in the list, the expected result and my results does not match, I checked my code but cannot find what is the problem, what happene…

Python - SciPy Kernal Estimation Example - Density 1

Im currently working through this SciPy example on Kernal Estimation. In particular, the one labelled "Univariate estimation". As opposed to creating random data, I am using asset returns. …

PyQt QFileDialog custom proxy filter not working

This working code brings up a QFileDialog prompting the user to select a .csv file:def load(self,fileName=None):if not fileName:fileName=fileDialog.getOpenFileName(caption="Load Existing Radio Log…

If I have Pandas installed correctly, why wont my import statement recognize it?

Im working on a project to play around with a csv file, however, I cant get pandas to work. Everything I have researched so far has just told me to make sure that pandas is installed. Using pip I have …

Python Issues with a Class

I am having issues with my below class. I keep getting the below traceback, butI am not sure were I am going wrong. I am expecting to see a dictionary with photo tags. Any help would be great. Tracebac…

Dynamically populate drop down menu with selection from previous drop down menu

I have a cgi script written in Python. There are two drop down menus and then a submit button. Id like to be able to make a selection from the first menu, and based off that choice, have the second dro…

Web Scrape page with multiple sections

Pretty new to python... and Im trying to my hands at my first project.Been able to replicate few simple demo... but i think there are few extra complexities with what Im trying to do.Im trying to scrap…

Python recv Loop

I am try to display data that is sent over a socket via an iteration from a loop. The way I have it at the moment isnt working on the Admin client. What should I do to fix my loop? Thank youAdmin t…

gtk+ python entry color [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

converting a text corpus to a text document with vocabulary_id and respective tfidf score

I have a text corpus with say 5 documents, every document is separated with each other by /n. I want to provide an id to every word in the document and calculate its respective tfidf score. for example…