Accessing variable from a function within a function

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

  1. 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
     
    Nathan Harmston, Jun 24, 2007
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Nathan Harmston

    7stud Guest

    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?
    >
    > Many Thanks in advance,
    >
    > Nathan


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

    return inner


    f = outer()
    f()

    --output:--
    10
     
    7stud, Jun 24, 2007
    #3
  4. 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
    #4
  5. Nathan Harmston

    James Stroud Guest

    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
     
    James Stroud, Jun 24, 2007
    #5
  6. Nathan Harmston

    James Stroud Guest

    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
    #6
  7. Nathan Harmston

    Marcin Ciura Guest

    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
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mohanasundaram
    Replies:
    44
    Views:
    1,071
    Keith Thompson
    Aug 24, 2004
  2. mfglinux
    Replies:
    11
    Views:
    707
    Roberto Bonvallet
    Sep 12, 2007
  3. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
  4. Krishna Komoravolu

    newbie ?: expand variable within a variable

    Krishna Komoravolu, Oct 31, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    124
    Gunnar Hjalmarsson
    Oct 31, 2003
  5. Replies:
    2
    Views:
    78
Loading...

Share This Page