On 5/9/2013 12:37, jsri...@gmail.com wrote: > I am going through the tutorials on docs.python.org, and I came across this > excerpt from http://docs.python.org/3/tutorial/controlflow.html: > > "The execution of a function introduces a new symbol table used for the local > variables of the function. More precisely, all variable assignments in a > function store the value in the local symbol table; whereas variable > references first look in the local symbol table, then in the local symbol > tables of enclosing functions, then in the global symbol table, and finally > in the table of built-in names. Thus, global variables cannot be directly > assigned a value within a function (unless named in a global statement), > although they may be referenced.
That new symbol table is per call, thus it's equivalent to a stack frame. A new stack frame is created when the function is called. The names in that stack frame are the function parameter names, plus all the names that are assigned to within that function, minus any names that were declared as global or as nonlocal. The list of such names is determined at compile time,not during the run of the function. Any reference within that function to names NOT in that local symbol table will be resolved at run time in a particular order; first in any enclosing functions (nested function definitions, which is NOT the same as nested calls), then in the global namespace, then in builtins. If the name cannot be found in any of those places, you'll get a runtime exception, something like NameError: global name 'b' is not defined > > "The actual parameters (arguments) to a function call are introduced in the > local > symbol table of the called function when it is called; thus, arguments are > passed > using call by value (where the value is always an object reference, not the > value > of the object). [1] When a function calls another function, a new local symbol > table is created for that call." > > Even as a professional programmer, I'm not really able to follow this. It > seems self-contradictory, amgiguous, and incomplete. The problem with > looking for this information elsewhere is that it's not going to be all in > one spot like this half the time, and it's not going to be readily searchable > on Google without more knowledge of what it's referring to. However this > looks like something that's too important to overlook. > > I can tell it's referring to things like scope, pass-by-value, references, > probably the call stack, etc., but it is written extremely poorly. > Translation please? Thanks! Python doesn't do pass-by-value nor pass-by-reference. The expression in the calling argument is some object. During a call to a function, that object is bound to the parameter name, which is a local variable in the called function. The object is NOT copied, it is effectively shared. if the object is immutable, the distinction is unimportant, but if the object is changed, it's changed for all names that are bound to the same object. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list