On Thu, 05 Sep 2013 09:37:57 -0700, jsrig88 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 above sounds like it was written by an assembly language programmer, rather than a Python coder. Anyway, here's a translation: When you call a function, the function creates an internal table of variable names. (Each name is a symbol. Hence a table of names is a symbol table.) Assignments inside the function (e.g. "x = 1") creates an entry in that function's table of variable names. On the other hand, merely referring to a variable name *without* assigning to it (e.g. "print x", or "x.attribute") searches for that variable by checking the local table of local variables, then the local variables of any enclosing functions, then the global variables, and finally the built-ins. The consequence of this is that all assignments like: x = 1 automatically force "x" to be a local variable. To make "x" a global variable, and hence assign to the global table of variables instead of the local table, you need to declare it as a global first using "global x" somewhere in the body of the function (conventionally at the top of the function, although anywhere will do). > "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). Every time somebody uses "call by value" in that way, God kills a kitten. Translation: Arguments to the function are treated as local variables. Arguments are not passed using call by value, although Java programmers will describe it that way. Nor are they passed as call by reference, although Ruby programmers will describe it that way. Both are guilty of misusing a simple term in a very unhelpful way, since call by value and call by reference have long standing meanings that are nothing like what Python (or Java, or Ruby) do. See here for further explanation: http://mail.python.org/pipermail/tutor/2010-December/080505.html The site appears to be down at the moment, but you can try the google cache instead: http://webcache.googleusercontent.com/search?q=cache:tuQDYATR8HAJ:http://mail.python.org/pipermail/tutor/2010-December/080505.html > [1] When a function > calls another function, a new local symbol table is created for that > call." Every call to a function has its own independent set of local variables. > Even as a professional programmer, I'm not really able to follow this. > It seems self-contradictory, amgiguous, and incomplete. It's not really any of those things. I'd call it pretentious, unnecessarily jargon-filled for a tutorial, and (in the case of the call- by-value comment) not just unhelpful but actively harmful. -- Steven -- https://mail.python.org/mailman/listinfo/python-list