setting variables in outer functions

Tommy Nordgren tommy.nordgren at comhem.se
Mon Oct 29 22:18:20 CET 2007


On 29 okt 2007, at 21.59, brad wrote:

> Tommy Nordgren wrote:
>> Given the following:
>> def outer(arg)
>>      avar = ''
>>      def inner1(arg2)
>>           # How can I set 'avar' here ?
>
> Try this... works for me... maybe not for you?
>
> def outer(avar=False):
>      print avar
>      if avar == True:
>          return
>
>      def inner(avar=True):
>          print avar
>          return avar
>
>      outer(inner())
>
> outer()
> --  
> http://mail.python.org/mailman/listinfo/python-list
	This is not a general solution to this problem.
What works, though, (I just thought of it) is to do the following:
def outer(arg):
     adict = {}
     def inner1(arg):
          adict['avar'] = 'something'
     #now the value set by inner1 is available to other nested functions
------
What is a woman that you forsake her, and the hearth fire and the  
home acre,
to go with the old grey Widow Maker.  --Kipling, harp song of the  
Dane women
Tommy Nordgren
tommy.nordgren at comhem.se






More information about the Python-list mailing list