SoFunction
Updated on 2024-11-15

Python beginners should not use expressions indiscriminately

Messing with expressions as default values in function arguments

Python allows you to set a default value for a parameter of a function in order to make that parameter optional. While this is a great feature of the language, it can be a bit tricky when the default value is mutable. For example, look at the following Python function definition:

>>> def foo(bar=[]):    
# bar is optional and defaults to [] if not specified.
...  ("baz")  
# But it's a questionable business, we'll see... #
...  return bar

A common mistake people make is to assume that every time the function is called without assigning a value to the optional parameter, it will always be given the value of the default expression. For example, in the code above, a programmer might assume that calling the function foo() repeatedly (without passing the parameter bar to the function) will always return 'baz', because it is assumed that the parameter bar will be set to [] (i.e., an empty list) each time foo() is called (without passing bar).

So let's look at what really happens when you do that:

>>> foo()
["baz"]
>>> foo()
["baz", "baz"]
>>> foo()
["baz", "baz", "baz"]

Hmmm? Why does this function always add our default value "baz" to an existing list every time foo() is called, instead of creating a new list every time?

The answer is that the default value of a function parameter is assigned only once, when the function is defined. Thus, the default value of the argument bar is initialized to its default value (i.e., an empty list) only when the function foo() is defined for the first time. When foo() is called (without the argument bar), it continues to use the same list as when bar was first initialized.

From this, the following solutions are possible:

>>> def foo(bar=None):
...  if bar is None:   
# Or if not bar.
...    bar = []
...  ("baz")
...  return bar
...
>>> foo()
["baz"]
>>> foo()
["baz"]
>>> foo()
["baz"]

Content additions:

regular character an explanation of the meaning of words or phrases give an example
+ The preceding element appears at least once ab+: ab, abbbb, etc.
* The preceding element appears 0 or more times ab*: a, ab, abb, etc.
? Match the previous one or zero times Ab?: A, Ab, etc.
^ serve as a start marker ^a: abc, aaaaaa, etc.
$ serve as a closing marker c$: abc, cccc, etc.
\d digital (electronics etc) 3, 4, 9, etc.
regular character an explanation of the meaning of words or phrases give an example
+ The preceding element appears at least once ab+: ab, abbbb, etc.
* The preceding element appears 0 or more times ab*: a, ab, abb, etc.
? Match the previous one or zero times Ab?: A, Ab, etc.
^ serve as a start marker ^a: abc, aaaaaa, etc.
$ serve as a closing marker c$: abc, cccc, etc.
\d digital (electronics etc) 3, 4, 9, etc.
\D non-numeric A, a, -, etc.
[a-z] Any letter between A and z a, p, m, etc.
[0-9] Any number between 0 and 9 0, 2, 9, etc.
\D non-numeric A, a, -, etc.
[a-z] Any letter between A and z a, p, m, etc.
[0-9] Any number between 0 and 9 0, 2, 9, etc.

to this article on python white avoid messing with the expression of the article is introduced to this, more related python mess with the expression of the content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!