Accessing variable from a function within a function

7stud bbxx789_05ss at yahoo.com
Sun Jun 24 20:42:27 CEST 2007


On Jun 24, 11:55 am, "Nathan Harmston" <ratchetg... at 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




More information about the Python-list mailing list