a dummy python question

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

    #1

    a dummy python question

    A example in learning Python by Mark Lutz and David Ascher

    about function scope

    example like this:
    [color=blue][color=green]
    >>def outer(x):[/color][/color]
    def inner(i):
    print i,
    if i: inner(i-1)
    inner(x)[color=blue][color=green]
    >>outer(3)[/color][/color]

    Here supposely, it should report error, because the function inner
    cannot see itself since inner is only in local namespace of outer.

    but I typed in this in python interface. It works!
    it print out:
    3 2 1 0


    If you turn this into a module file and run this
    it print out
    3 2 1 0 none

    Can anyone explain to me what's going on?

    Thanks

    BTW: I am using Python 2.3

  • infidel

    #2
    Re: a dummy python question


    Learning Python wrote:[color=blue]
    > A example in learning Python by Mark Lutz and David Ascher
    >
    > about function scope
    >
    > example like this:
    >[color=green][color=darkred]
    > >>def outer(x):[/color][/color]
    > def inner(i):
    > print i,
    > if i: inner(i-1)
    > inner(x)[color=green][color=darkred]
    > >>outer(3)[/color][/color]
    >
    > Here supposely, it should report error, because the function inner
    > cannot see itself since inner is only in local namespace of outer.[/color]

    If that were so, Pythonistas could never write a recursive function!

    Comment

    • cipherpunk@gmail.com

      #3
      Re: a dummy python question

      This is not reproducible under either Python 2.3.4 (UNIX), Python 2.4.1
      (UNIX) or Python 2.4.1 (Windows). If you still need help, we need to
      know precisely what you're doing.

      ===== scope_test.py =====
      #!/usr/bin/env python
      #
      # (insert his code, verbatim...)
      #
      if __name__=='__ma in__':
      outer(3)

      ===== end scope_test.py =====

      [rjhansen@serv16 ~]$ ./scope_test.py
      3 2 1 0

      [rjhansen@serv16 ~]$ python
      Python 2.3.4 (#1, Feb 2 2005, 11:44:13)
      [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
      >>> from scope_test import outer
      >>> outer(3)[/color][/color][/color]
      3 2 1 0

      Comment

      • rafi

        #4
        Re: a dummy python question

        Learning Python wrote:
        [color=blue][color=green][color=darkred]
        >>>def outer(x):[/color][/color]
        >
        > def inner(i):
        > print i,
        > if i: inner(i-1)
        > inner(x)
        >[color=green][color=darkred]
        >>>outer(3)[/color][/color]
        >
        > Here supposely, it should report error, because the function inner
        > cannot see itself since inner is only in local namespace of outer.[/color]

        There is no error. the function inner is defined recursively: It calls
        itself with a different value than the one it has been called with. When
        defining a recursive function, there are case when it calls itself and
        other when it does not (otherwise the recursion is infinite and the
        program crashes after all the memory is used). Here it does not call
        itself when the value given as parameter is 0 (the if fails).

        one can always see itself (even at definition time)
        [color=blue]
        > but I typed in this in python interface. It works!
        > it print out:
        > 3 2 1 0
        >
        > If you turn this into a module file and run this
        > it print out
        > 3 2 1 0 none[/color]

        I suppose you wrote this down (instead of cut and paste) as the none is
        not capitalized. There must be something else in your module that writes
        the none as your code presented above should really not "as is".

        --
        rafi

        "Imaginatio n is more important than knowledge."
        (Albert Einstein)

        Comment

        • Robert Kern

          #5
          Re: a dummy python question

          infidel wrote:[color=blue]
          > Learning Python wrote:
          >[color=green]
          >>A example in learning Python by Mark Lutz and David Ascher
          >>
          >>about function scope
          >>
          >>example like this:
          >>
          >>[color=darkred]
          >>>>def outer(x):[/color]
          >>
          >> def inner(i):
          >> print i,
          >> if i: inner(i-1)
          >> inner(x)
          >>[color=darkred]
          >>>>outer(3)[/color]
          >>
          >>Here supposely, it should report error, because the function inner
          >>cannot see itself since inner is only in local namespace of outer.[/color]
          >
          > If that were so, Pythonistas could never write a recursive function![/color]

          No, presumably at the writing of the edition of _Learning Python_ that
          he is reading, Python did not have nested scopes in the language, yet.
          One could always write a recursive function provided it was at the
          top-level of the module. One could not write a recursive function inside
          another function because inside inner(), it could only access two
          namespaces, the one local to inner() and the module's namespace, not the
          namespace of outer() where inner() is defined.

          For the original poster: Your book is old. You will want to catch up on
          recent additions to the language by reading the "What's New in Python
          2.x" portions of the documentation for each major revision. Specifically:







          --
          Robert Kern
          rkern@ucsd.edu

          "In the fields of hell where the grass grows high
          Are the graves of dreams allowed to die."
          -- Richard Harter

          Comment

          • Learning Python

            #6
            Re: a dummy python question

            Thanks all for replying.
            I finally know what's going on.

            Comment

            • infidel

              #7
              Re: a dummy python question

              > > If that were so, Pythonistas could never write a recursive function![color=blue]
              >
              > No, presumably at the writing of the edition of _Learning Python_ that
              > he is reading, Python did not have nested scopes in the language, yet.
              > One could always write a recursive function provided it was at the
              > top-level of the module. One could not write a recursive function inside
              > another function because inside inner(), it could only access two
              > namespaces, the one local to inner() and the module's namespace, not the
              > namespace of outer() where inner() is defined.[/color]

              Ah, that makes sense. Thanks for the clarification.

              Comment

              Working...