Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Default argument
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Extent== Generally a default argument will behave identically to an argument passed by parameter or a local variable declared at the start of the function, and have the same [[scope (computer science)|scope]] and extent (lifetime) as a parameter or other local variable, namely an [[automatic variable]] which is deallocated on function termination. In other cases a default argument may instead be statically allocated. If the variable is mutable, it will then retain its value across function calls, as with a [[static variable]]. This behavior is found in Python for mutable types, such as lists. As with evaluation, in order to ensure the same extent as a local variable, one can use a sentinel value: <syntaxhighlight lang="python"> def eager(a=[]): return a x = eager() x += [1] assert eager() == [1] def lazy(a=None): if a is None: a = [] return a x = lazy() x += [1] assert lazy() == [] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)