# Accessing variable from a function within a function

Discussion in 'Python' started by Nathan Harmston, Jun 24, 2007.

1. ### Nathan HarmstonGuest

Hi,

I m playing around with extended euclids algorithm from Knuth. I m
trying to build a function with a function inside it.

def exteuclid(m,n):
a,a1,b,b1,c,d = 0,1,1,0,m,n
def euclid(c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t - q * b
a = t - q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)

Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
function. Is there a way to make these variables accessible to the
euclid function. Or is there a better way to design this function?

Nathan

Nathan Harmston, Jun 24, 2007

2. ### Michael HoffmanGuest

Nathan Harmston wrote:
> Hi,
>
> I m playing around with extended euclids algorithm from Knuth. I m
> trying to build a function with a function inside it.
>
> def exteuclid(m,n):
> a,a1,b,b1,c,d = 0,1,1,0,m,n
> def euclid(c,d):
> q = c /d
> r = c % d
> if r == 0:
> print a,b
> return d
> else:
> print a1,a,b1,b,c,d,q,r
> t = b1
> b = t - q * b
> a = t - q * a
> c,d,a1,b1 = d,r,a,b
> return euclid(c,d)
> return euclid(c,d)
>
> Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
> function. Is there a way to make these variables accessible to the
> euclid function. Or is there a better way to design this function?

Well, it would be simpler to pass through all the variables rather than
relying on variables in a wider scope.
--
Michael Hoffman

Michael Hoffman, Jun 24, 2007

3. ### 7studGuest

On Jun 24, 11:55 am, "Nathan Harmston" <>
wrote:
> Hi,
>
> I m playing around with extended euclids algorithm from Knuth. I m
> trying to build a function with a function inside it.
>
> def exteuclid(m,n):
> a,a1,b,b1,c,d = 0,1,1,0,m,n
> def euclid(c,d):
> q = c /d
> r = c % d
> if r == 0:
> print a,b
> return d
> else:
> print a1,a,b1,b,c,d,q,r
> t = b1
> b = t - q * b
> a = t - q * a
> c,d,a1,b1 = d,r,a,b
> return euclid(c,d)
> return euclid(c,d)
>
> Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
> function. Is there a way to make these variables accessible to the
> euclid function. Or is there a better way to design this function?
>
>
> Nathan

ef outer():
a = 10
def inner():
print a

return inner

f = outer()
f()

--output:--
10

7stud, Jun 24, 2007
4. ### Stefan BellonGuest

On Sun, 24 Jun, 7stud wrote:

> ef outer():
> a = 10
> def inner():
> print a
>
> return inner
>
>
> f = outer()
> f()
>
> --output:--
> 10
>

>>> def outer():

.... a = 10
.... def inner():
.... a = a + 1
.... print a
.... return inner
....
>>> f=outer()
>>> f()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in inner
UnboundLocalError: local variable 'a' referenced before assignment

--
Stefan Bellon

Stefan Bellon, Jun 24, 2007
5. ### James StroudGuest

Nathan Harmston wrote:
> Hi,
>
> I m playing around with extended euclids algorithm from Knuth. I m
> trying to build a function with a function inside it.
>
> def exteuclid(m,n):
> a,a1,b,b1,c,d = 0,1,1,0,m,n
> def euclid(c,d):
> q = c /d
> r = c % d
> if r == 0:
> print a,b
> return d
> else:
> print a1,a,b1,b,c,d,q,r
> t = b1
> b = t - q * b
> a = t - q * a
> c,d,a1,b1 = d,r,a,b
> return euclid(c,d)
> return euclid(c,d)
>
> Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
> function. Is there a way to make these variables accessible to the
> euclid function. Or is there a better way to design this function?
>
>
> Nathan

That last return statement does not match indentation of another block.
But this is probably what you mean:

def exteuclid(m,n):
x = 0,1,1,0,m,n
def euclid(c,d,x=x):
a,a1,b,b1,c,d = x
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t - q * b
a = t - q * a
c,d,a1,b1 = d,r,a,b
return euclid(c,d)
return euclid(c,d)

James

James Stroud, Jun 24, 2007
6. ### James StroudGuest

James Stroud wrote:
> Nathan Harmston wrote:
> def exteuclid(m,n):
> x = 0,1,1,0,m,n
> def euclid(c,d,x=x):
> a,a1,b,b1,c,d = x
> q = c /d
> r = c % d
> if r == 0:
> print a,b
> return d
> else:
> print a1,a,b1,b,c,d,q,r
> t = b1
> b = t - q * b
> a = t - q * a
> c,d,a1,b1 = d,r,a,b
> return euclid(c,d)
> return euclid(c,d)
>
> James

My answer above is wrong because c and d take the wrong default values.
Also, you have some ambiguity in your code. Are nested calls to euclid
supposed to have the original values of a, a1, b, & b1, or are they to
take the original values 0, 1, 1, 0? This type of ambiguity is one
reason why the interpreter does not like reference before assignment.
This is my best guess at what you want because I'm not familiar with how
euclid's algorithm works:

def exteuclid(m,n):
def euclid(a,a1,b,b1,c,d):
q = c /d
r = c % d
if r == 0:
print a,b
return d
else:
print a1,a,b1,b,c,d,q,r
t = b1
b = t - q * b
a = t - q * a
c,d,a1,b1 = d,r,a,b
return euclid(a,a1,b,b1,c,d)
return euclid(0,1,1,0,m,n)

James Stroud, Jun 24, 2007
7. ### Marcin CiuraGuest

Nathan Harmston wrote:
> Unfortunately this doesnt work since a,a1,b,b1 arent declared in the
> function. Is there a way to make these variables accessible to the
> euclid function. Or is there a better way to design this function?

The canonical recommendations are: use attributes of the inner
function or one-element lists as writable variables visible
in the outer function.

Another possibility is to modify the bytecode of the functions
by an appropriate decorator. See
http://www-zo.iinf.polsl.gliwice.pl/~mciura/software/expose.py
(you'll need the byteplay module by Noam Raphael to make it work).
Marcin

Marcin Ciura, Jun 24, 2007