471,337 Members | 838 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Accessing variable from a function within a function

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?

Many Thanks in advance,

Nathan
Jun 24 '07 #1
6 1318
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
Jun 24 '07 #2
On Jun 24, 11:55 am, "Nathan Harmston" <ratchetg...@googlemail.com>
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?

Many Thanks in advance,

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

return inner
f = outer()
f()

--output:--
10

Jun 24 '07 #3
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
Jun 24 '07 #4
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?

Many Thanks in advance,

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
Jun 24 '07 #5
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)
Jun 24 '07 #6
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
Jun 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by C Gillespie | last post: by
18 posts views Thread by Joe | last post: by
23 posts views Thread by Russ Chinoy | last post: by
14 posts views Thread by James Thiele | last post: by
12 posts views Thread by Steve Blinkhorn | last post: by
3 posts views Thread by BAnderton | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.