Global variables, assignment, ( & eval )

Steven D. Majewski (sdm7g@aemsun.med.Virginia.EDU)
Fri, 6 Dec 91 13:46:55 EST

Re: input()

You are correct.

>>> a = 1
>>> b = input( '?:' )
?: a
>>> b
1

works correctly.

I must have had some other syntax errors and did not
transcribe it correctly. ( In the longer examples I
usually cut & past from the screen so I get it verbatim. )
Sorry.

I'm also sorry because I thought that your reply pointed to
a problem with *my* executable of python, and I thought it
might explain some other behaviour that perplexes me.

I've been having some trouble with global assignment.

./misc/CLASSES :
"A special quirk of Python is that assignments always go into the
innermost scope. Assignments do not copy data -- they just
bind names to objects. The same is true for deletions: the statement
"del x" removes the binding of x from the name space referenced by the
local scope. In fact, all operations that introduce new names use the
local scope: in particularly, import statements and function
definitions bind the module or function name in the local scope."

Does this mean that global variables in a module are READONLY.
That didn't seem to be explicitly stated in the docs. ( Maybe I
missed it. I looked through some of the demo's and most of the
globals I saw WERE being used READONLY )

I guess a quick experiment answers my own question:

>>> N = 1000
>>> def decN():
... print N
... N = N - 1
... print N
... return N
...
>>> N
1000
>>> decN()
1000
999
999
>>> print N
1000

... but I would suggest you stress this a bit more in the tutorial.

Do I have to create a class instance to have a writeable non local variable ?

( I mentioned a bug in my du routine - I can't set MAXDEPTH correctly.
This seems to be it. )

The following is actually the perplexing part:
( The above is not confusing once I did the experiment, but I'm
not sure I understand the cause of the errors below. Is the
compiler/interpreter anticipating that since I do an assignment,
that the references in incK() are to a different K than the
references in access(), and so the print statement before the
assignment give me an error ?
Could you perhaps give us a bit more on binding & scope of
variables, etc. ? )

# module test
#
N = 99
S = 'This is a test: '
K = 1

def access():
print N, S, K
return ( N, S, K )

def newK(x):
K = x
return K

def decN():
print N
N = N - 1
return N

def incK():
print K
K = K + 1
return K

>>> import test
>>> test.K
1
>>> test.access()
99 This is a test: 1
(99, 'This is a test: ', 1)
>>> test.newK( 33 )
33
>>> test.access()
99 This is a test: 1
(99, 'This is a test: ', 1)
>>> test.K
1
>>> test.incK()
Unhandled exception: undefined name: K
Stack backtrace (innermost last):
File "<stdin>", line 1
File "./test.py", line 21
print K
>>> K = 3 # just to check that it is not GLOBAL/global
>>> test.incK()
Unhandled exception: undefined name: K
Stack backtrace (innermost last):
File "<stdin>", line 1
File "./test.py", line 21
print K

======== "If you have a hammer, find a nail" - George Bush,'91 =========
Steven D. Majewski University of Virginia Physiology Dept.
sdm7g@Virginia.EDU Box 449 Health Sciences Center
Voice: (804)-982-0831 1600 Jefferson Park Avenue
FAX: (804)-982-1616 Charlottesville, VA 22908