Functions

Discussion in 'Python' started by Duncan Booth, Aug 18, 2003.

  1. Duncan Booth

    Duncan Booth Guest

    Thor <> wrote in
    news:bhqhm1$1vcq3$-berlin.de:

    > In this hypothetical case:
    >
    > def f1:
    > f3:
    > def f2:
    > def f3:
    > pass
    > f1:
    > def f4:
    > def f3:
    > pass
    > f1:
    >
    > would the function f1 execute the right f3 depending on from which
    > functions is it called?


    Why don't you load up the interactive interpreter and try running it?
    You'll find a lot of mistakes in the hypothetical code you entered,
    including the absence of argument lists after the function names, and the
    spurious colons and lack of parenthese on the function calls.

    I'll assume you actually meant something like:

    def f1():
    f3()
    def f2():
    def f3():
    pass
    f1()
    def f4():
    def f3():
    pass
    f1()

    If the code above is indeed what you intended then calling either f2() or
    f4() will result in a 'NameError' exception because there is no name 'f3'
    in scope from inside f1(). There are local variables 'f3' inside both f2()
    and f4(), but local variables are never visible nor accessible from outside
    the functions in which they are defined. (They are visible from inside
    nested functions, but that is not the situation here.)

    To get code something like this to work, you should pass f3 as a parameter
    to f1:

    def f1(f3):
    f3()
    def f2():
    def f3():
    pass
    f1(f3)
    def f4():
    def f3():
    pass
    f1(f3)

    Remember, Python functions are just objects like any other and may be
    assigned to variables or passed in and out of other functions.

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
    Duncan Booth, Aug 18, 2003
    #1
    1. Advertising

  2. Thor wrote:

    > In this hypothetical case:
    >
    > def f1:


    I assume you mean def f1(): here and in similar cases (the
    parentheses are syntactically mandatory).

    > f3:


    I assume you mean f3() here and in similar cases (the colon
    would be a syntax error, the parentheses indicate a call is
    being performed).

    > def f2:
    > def f3:
    > pass
    > f1:
    > def f4:
    > def f3:
    > pass
    > f1:
    >
    > would the function f1 execute the right f3 depending on from which
    > functions is it called?


    No. There is no "dynamic scoping" of names (and the rules are
    exactly the same whether you're thinking of names of functions
    or names of any other type of object). f1 would look up name
    f3 in its LEXICAL scope, not find it, and therefore produce an
    error. If you added
    global f3
    as the first statement of both f2 and f4, right before the
    "def f3():" in each of them, then -- as it happens -- you would
    get the behavior you're after, in this particular simple case
    (both f2 and f4 would, with the 'global', clobber global name
    f3 with their own version of function f3 on each execution --
    and global names of this module which all functions share ARE
    parts of the lexical scope searched for name resolution within
    function f1).


    Alex
    Alex Martelli, Aug 18, 2003
    #2
    1. Advertising

  3. Duncan Booth

    Thor Guest

    In this hypothetical case:

    def f1:
    f3:
    def f2:
    def f3:
    pass
    f1:
    def f4:
    def f3:
    pass
    f1:

    would the function f1 execute the right f3 depending on from which functions
    is it called?
    --
    Thor -- Stockholm -- Sverige
    Thor, Aug 18, 2003
    #3
  4. Duncan Booth

    Thor Guest

    Thanks to both. Of ourse the parentheses thin was worng, I was just trying
    to make the most symplified code. I tt was just one possibility that arose
    from the code I was doing.

    --
    Thor -- Stockholm -- Sverige
    Thor, Aug 19, 2003
    #4
    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. tshad
    Replies:
    11
    Views:
    809
    tshad
    May 27, 2005
  2. Timothy Wong
    Replies:
    3
    Views:
    402
    Timothy Wong
    May 20, 2004
  3. ILLOGIC
    Replies:
    1
    Views:
    349
    Rob Williscroft
    Jun 1, 2004
  4. Xiangliang Meng
    Replies:
    1
    Views:
    1,580
    Victor Bazarov
    Jun 21, 2004
  5. Steven T. Hatton

    Passing member functions to C functions?

    Steven T. Hatton, Oct 4, 2004, in forum: C++
    Replies:
    7
    Views:
    1,304
    David Hilsee
    Oct 7, 2004
Loading...

Share This Page