Python's scope rules

Brian Elmegaard be at
Wed Dec 20 10:51:55 CET 2000


I am not a computer scientist, just an engineer knowing how to program in
Fortran, Pascal,... So I after reading some of the python material on the web, I
decided to give it a try and is now using the language some, finding it fun and

On usenet I have now learned from skilled scientists, that Python has some
deficiencies regarding scope rules, and I am not capable of telling them why it
has not (or that it has). 

In Aaron Watters 'The wwww of python' I have read that Python uses lexical
scoping with a convenient modularity. But, other people tell me it does not.
Some say it uses dynamic scoping, some say it uses it own special
'local-global-builtin' scope. What is right?

The above mainly is a theoretical question. A more practical example which I
agree seems a bit odd is:

Python 1.5.2 (#1, Mar  9 2000, 17:40:34)  [GCC 2.95.2 19991024 (release)] on hp-
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> x=24
>>> def foo():
...     y=2
...     print x+y
>>> def bar():
...     x=11
...     foo()
>>> bar()

Well, I can live with it. In Fortran I would always put all the variables to be
passed in the parameter list. Other people say that Fortran is not easy, and
this example shows how stupid python is. It does?

Hopefully not, but probably, this is a faq, but I have not been able to find
anything on the matter.

Brian Elmegaard (be at 
Dept. of Energy Engineering, Technical University of Denmark, 
Phone +45 4525 4169  Fax +45 4593 5215

More information about the Python-list mailing list