Visibility of Python Variables

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Visibility of Python Variables

Georg Maubach
Hi All,

I have a general question on Python programming.

In a function variables need to be declared as global if I wanna use a
variable in the function that was defined outside the function. But if I
use it as a parameter to a function this is not necessary.

I include a simple script code explaining the question in more detail.

--------------------- cut --------------------

# Author: Georg Maubach
# Date  : 16-Aug-2006
# File  : VisibilityOfVariables.py

from turtle import *

width(10)

step = 100 # a global variable is defined
color("blue")

# Section 1: Code works
def triangle():
    forward(step) # The global variable step which should not be visible
to the
    left(120)     # interpreter does not cause an error during program
run
    forward(step) # although it should be addressable by the interpreter
    left(120)     # cause the global variable "step" was not introduced
to the
    forward(step) # function "triangle" by using "global step".
    left(120)     # Why does this not raise an error?


triangle()

# Section 2: Code does not work
def rectangle():
    forward(step)
    left(90)
    forward(step)
    left(90)
    forward(step)
    left(90)
    forward(step)
    left(90)
    step = step + change # Why does this not work? The variable was
                         # defined as before. Now it seems to be not
                         # visible. Why? What is the difference between
                         # Section 1 and 2?
    left(60) # next rectangle


rectangle()

# eof #

--------------------- cut --------------------

The question is why do I have not to declare a global variable to be
used as function parameter whereas I have to declare this variable as
global if I want to assign another value?

Question to List-Owner:
My question is more general. Is it off-topic to ask these questions on
this list?

I am looking forward to hearing from you.

Best regards

Georg Maubach
Market Analyst
Reply | Threaded
Open this post in threaded view
|

Re: Visibility of Python Variables

Peck, Jon
Your step variable has module scope.  That means that it is visible to any code in the module where it is declared except that if you define a function within that module and use the same name as the parameter for the function, e.g.,
def f(step): ...

the step variable within the function will be interpreted as the parameter step rather than the module step.

But, assuming that your function is not defined using a parameter name that masks the module level variable,
def f(x): ...

then if you reference step within the function, you are referring to the module variable.  If you assign to that variable, however, you are referring to a local variable with that name.

"If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) "
and
"If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block"

By this logic, then, when you first refer to the value of the variable, it is undefined, because by the later assignment, it is the local variable that is meant, and that does not have a value yet.

So
step = 100
def f(x):
        print step
        step = 200

If you just use a module value in f but don't assign to it, the variable is a module variable.

HTH,
Jon Peck
SPSS

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Georg Maubach
Sent: viernes, 18 de agosto de 2006 10:54
To: [hidden email]
Subject: [SPSSX-L] Visibility of Python Variables

Hi All,

I have a general question on Python programming.

In a function variables need to be declared as global if I wanna use a variable in the function that was defined outside the function. But if I use it as a parameter to a function this is not necessary.

I include a simple script code explaining the question in more detail.

--------------------- cut --------------------

# Author: Georg Maubach
# Date  : 16-Aug-2006
# File  : VisibilityOfVariables.py

from turtle import *

width(10)

step = 100 # a global variable is defined
color("blue")

# Section 1: Code works
def triangle():
    forward(step) # The global variable step which should not be visible to the
    left(120)     # interpreter does not cause an error during program run
    forward(step) # although it should be addressable by the interpreter
    left(120)     # cause the global variable "step" was not introduced to the
    forward(step) # function "triangle" by using "global step".
    left(120)     # Why does this not raise an error?


triangle()

# Section 2: Code does not work
def rectangle():
    forward(step)
    left(90)
    forward(step)
    left(90)
    forward(step)
    left(90)
    forward(step)
    left(90)
    step = step + change # Why does this not work? The variable was
                         # defined as before. Now it seems to be not
                         # visible. Why? What is the difference between
                         # Section 1 and 2?
    left(60) # next rectangle


rectangle()

# eof #

--------------------- cut --------------------

The question is why do I have not to declare a global variable to be used as function parameter whereas I have to declare this variable as global if I want to assign another value?

Question to List-Owner:
My question is more general. Is it off-topic to ask these questions on this list?

I am looking forward to hearing from you.

Best regards

Georg Maubach
Market Analyst