Joseph Mack NA3T | 11 May 19:16

scope in nested function calls


I've just moved a piece of code out of a long function into another (lower 
nested) function and I'd like the lower nested function to still access (write 
to) variables in the calling function. I'm doing this for readability and so 
that I can test a couple of different versions of the lower nested function by 
commenting out calls in the calling function.

Here's an example piece of code
-------------------------------
#! /usr/bin/python

def foo():
         print "foo: bar_value  %d" %bar_value

def bar():
         bar_value=1
         print "bar: bar_value  %d" %bar_value
         foo()

#main()
bar()

---------

the output is

# ./test_scope.py
bar: bar_value  1
Traceback (most recent call last):
   File "./test_scope.py", line 12, in ?
(Continue reading)

Philip Semanchuk | 11 May 19:54

Re: scope in nested function calls


On May 11, 2008, at 1:20 PM, Joseph Mack NA3T wrote:

>
> I've just moved a piece of code out of a long function into another  
> (lower nested) function and I'd like the lower nested function to  
> still access (write to) variables in the calling function. I'm  
> doing this for readability and so that I can test a couple of  
> different versions of the lower nested function by commenting out  
> calls in the calling function.
>
> Here's an example piece of code
> -------------------------------
> #! /usr/bin/python
>
> def foo():
>         print "foo: bar_value  %d" %bar_value
>
> def bar():
>         bar_value=1
>         print "bar: bar_value  %d" %bar_value
>         foo()
>
> #main()
> bar()
>
> ---------
>
> the output is
>
(Continue reading)

Joseph Mack NA3T | 12 May 00:21

Re: scope in nested function calls

On Sun, 11 May 2008, Philip Semanchuk wrote:

& thanks Bob too

It seems I have to declare the variable global at least at 
the module level

> #! /usr/bin/python
>
> # The line below will be executed once when the module is initialized.
> bar_value=42

> This should print (untested):
>
> foo: bar_value  42
> bar: bar_value  1
> foo: bar_value  1

seems reasonable

As for the term "nesting" - I was using it in the sense that 
fnA() calls fnB(), then anything declared in fnA() should be 
visible to fnB(). At least that was my understanding from a 
long time ago. However looking in google for "wiki" and 
"nesting", I find the term nesting is used only for 
recursive calls by a fn() to itself. I have no idea whether 
I missunderstood nesting when I first heard it, or the 
meaning has changed over 40yrs.

Writing a fn() inside another fn() apparently is called 
(Continue reading)

bob gailer | 11 May 23:24

Re: scope in nested function calls

Joseph Mack NA3T wrote:
>
> I've just moved a piece of code out of a long function into another 
> (lower nested) function and I'd like the lower nested function to 
> still access (write to) variables in the calling function. I'm doing 
> this for readability and so that I can test a couple of different 
> versions of the lower nested function by commenting out calls in the 
> calling function.
>
> Here's an example piece of code
> -------------------------------
> #! /usr/bin/python
>
> def foo():
>         print "foo: bar_value  %d" %bar_value
>
> def bar():
>         bar_value=1
>         print "bar: bar_value  %d" %bar_value
>         foo()
>
> #main()
> bar()
>
> ---------
>
> the output is
>
> # ./test_scope.py
> bar: bar_value  1
(Continue reading)


Gmane