Could someone please paraphrase this statement about variables andfunctions in Python?

J

jsrig88

I am going through the tutorials on docs.python.org, and I came across thisexcerpt 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 afunction store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tablesof 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), althoughthey may be referenced.

"The actual parameters (arguments) to a function call are introduced in thelocal
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 thevalue
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!
 
D

Dave Angel

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.
 
N

Neil Cerutti

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.

Every function has its own scope, which is a list of its local
variables.

When you assign to a local name in a function, you are assigning
to the symbol table that stores those variables.

Not spelled out exactly is that every unqualified name assigned
to in a function is assumed to be a local variable. So this is an
error:
.... print(x)
.... x = 7
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'x' referenced before assignment

If the assignement to x is removed, then you can refer to the
global variable just fine.
.... print(x)
....5

In a function, you can allow assignment to global variables using
the global statement.
.... global x
.... print(x)
.... x = 7
....
7

"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."

When you call a function, it is as if an assignment of the
arguments to the function paramters takes place.

def foo(x, y):
print(x, y)

When I call foo(1, 2), the effect is:

1. The local variable x is assigned to the object returned by the
expression 1
2. The local y is assigned to the object returned by the expression 2
3. The body of function foo is run.

Assignments in Python are name-binding. Objects are passed into
functions by assignment. If you understand how assignment works
in Python, that's a good explanation.

Finally, every time you call a function a new symbol table is
built. So I can do the following without names conflicting:

def foo(x, y):
if x == 0:
return y
y *= 2
return foo(x-1, y)
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.

It's not ambiguous, self-contradictory or incomplete. But it's
very densely packed with information; perhaps it's too complete.
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!

I don't think that's a fair criticism, but it might be too
technical for a language tutorial.
 
T

Terry Reedy

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.
 
S

Steven D'Aprano

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.c...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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top