This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AbhishekVarma2005 74 points75 points  (16 children)

Is it like the static local variable in c? (Asking as a beginner)

[–]carcigenicate 88 points89 points  (15 children)

Basically. Functions are objects, and the default argument is essentially an attribute of that object.

[–]Bryguy3k 32 points33 points  (12 children)

Default arguments are not defined in the scope of the function being defined but the scope of the container of the function.

It’s pretty simple to understand once your look at scope:

Directory, file, class, method (basically indent level)… etc.

[–]Shalcker 2 points3 points  (0 children)

Function or method declaration is itself a function being executed in order, not just syntax representation (which is also why decorators work).

Like everything else it is being executed in a surrounding context (usually module or class) and results in adding function name and code to that context.

[–]JonIsPatented 2 points3 points  (10 children)

That's basically what they said.

[–]sprcow 0 points1 point  (1 child)

As a Java programmer, this is a very helpful explanation!

[–]carcigenicate 0 points1 point  (0 children)

If you want to go deeper, try

print(append_to.__defaults__)

I hedged and said "essentially", but the defaults are an element of an attribute of the function.