Could someone please paraphrase this statement about variables and functions in Python?

Terry Reedy tjreedy at udel.edu
Thu Sep 5 13:24:50 EDT 2013


On 9/5/2013 12:37 PM, jsrig88 at 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.
>
> "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.

The only incompleteness is that 'global statememt' should be 'global or 
nonlocal statement'.

> 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!

Without knowing your personal language, translating into a language you 
would understand is an impossible task. I would replace 'variable' with 
'name', 'symbol table' with 'nmespace', 'call by object reference value' 
with 'call by object', and 'global' with 'modular'. Maybe that helps 
you, maybe it confuses you more.

The importance thing you must understand is that Python is an 
object-based language and that calling a function (like assignment 
statement in general) does not copy the argument objects.

I suggest that you study the behavior of actual examples both in the 
tutorial and ones you make up. If you are actually baffled by some 
behavior, ask a spedific question.

PS. Try not to double post.

-- 
Terry Jan Reedy




More information about the Python-list mailing list