python: lexical or dynamic scope?

G

globalrev

i cant figure outif python has lexical or general scope.

it seems functions have lexical scope but with some restrictions and
some non-function scopes are dynamic?
 
A

Arnaud Delobelle

globalrev said:
i cant figure outif python has lexical or general scope.

it seems functions have lexical scope but with some restrictions and
some non-function scopes are dynamic?

I can't think of any instance of dynamic scoping in Python. Can you
give an example of what you are thinking of?

AFAIK, Python has lexical scoping, with the restriction that
non-global non-local names cannot be rebound.
 
C

Carl Banks

i cant figure outif python has lexical or general scope.

it seems functions have lexical scope but with some restrictions and

Yes. (You can't modify a local variable from an inner scope--this
will change in Python 3.0.)
some non-function scopes are dynamic?

No: I presume you mean the class statement, which is the only thing
other than def that introduces a scope in Python, but it's neither
dynamic nor lexical. In fact it's closed: you can only access
variables in a class scope from that scope, and not from enclosed
scopes (e.g., method defintions) at all.


Carl Banks
 
M

Mark Wooding

Arnaud Delobelle said:
AFAIK, Python has lexical scoping, with the restriction that
non-global non-local names cannot be rebound.

I believe so.

It's possible to implement (shallow) dynamic binding as a Python context
manager, though it involves a little unpleasantness with sys._getframe
to get hold of the caller's variables. See

http://www.distorted.org.uk/~mdw/hacks/fluid.py

That Python can do this (and fairly simply) speaks well of its
flexibility and dynamic nature.

I don't know how to make it work properly with multi-threading,
unfortunately.

-- [mdw]
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top