Functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thor

    Functions

    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
  • Duncan Booth

    #2
    Re: Functions

    Thor <thor__00@yahoo .com> wrote in
    news:bhqhm1$1vc q3$1@ID-108351.news.uni-berlin.de:
    [color=blue]
    > 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?[/color]

    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 duncan@rcp.co.u k
    int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
    "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

    Comment

    • Alex Martelli

      #3
      Re: Functions

      Thor wrote:
      [color=blue]
      > In this hypothetical case:
      >
      > def f1:[/color]

      I assume you mean def f1(): here and in similar cases (the
      parentheses are syntactically mandatory).
      [color=blue]
      > f3:[/color]

      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).
      [color=blue]
      > 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?[/color]

      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

      Comment

      • Thor

        #4
        Re: Functions

        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

        Comment

        Working...