variable refences for nested functions

thephoenix235 at gmx.net thephoenix235 at gmx.net
Mon Oct 21 04:56:43 CEST 2002


Hi,

As I understand it, a nested function can access the local variables of the
enclosing function.  The following code works. 

def foo ():
  a=1
  def bar():
    print a
  bar()

foo()


But this does not.


def foo ():
  a=1
  def bar():
    print a
    a=1
  bar()

foo()


foo() is actually a state machine which takes a file as a argument. there
are several bar()'s, each corresponding to a different state. 'a' is a line of
the file. I could make foo into a class but i'd then have to prefix 'self.'
to every value. (besides, it does not look like a function call anymore.) 

i am using python 2.2.1.

anyone have a better workaround?

ps: please include my address in the reply as i am not subscribed to the
list.

Chirayu.

-- 
+++ GMX - Mail, Messaging & more  http://www.gmx.net +++
NEU: Mit GMX ins Internet. Rund um die Uhr für 1 ct/ Min. surfen!





More information about the Python-list mailing list