Velocity Reviews > Accessing variable from a function within a function

# Accessing variable from a function within a function

Nathan Harmston
Guest
Posts: n/a

 06-24-2007
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

Michael Hoffman
Guest
Posts: n/a

 06-24-2007
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

7stud
Guest
Posts: n/a

 06-24-2007
On Jun 24, 11:55 am, "Nathan Harmston" <(E-Mail Removed)>
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

Stefan Bellon
Guest
Posts: n/a

 06-24-2007
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

James Stroud
Guest
Posts: n/a

 06-24-2007
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
Guest
Posts: n/a

 06-24-2007
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)

Marcin Ciura
Guest
Posts: n/a

 06-24-2007
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/...ware/expose.py
(you'll need the byteplay module by Noam Raphael to make it work).
Marcin