Scope question

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

    Scope question

    Hi,

    How can I make counter variable in function foo reference to global counter
    variable in the following code. Generally C programmers tend to write code like
    that I am looking for simple way to do it in python.

    --- BEGIN ---
    counter = 0

    def foo():
    if counter < 10:
    print "count = ", counter
    counter += 1

    foo()
    --- END ---

    regards,
    Subhash
    --
    Remove "nospam_" for direct reply
  • Gandalf

    #2
    Re: Scope question

    Subhash Chandra wrote:
    [color=blue]
    > Hi,
    >
    > How can I make counter variable in function foo reference to global
    > counter variable in the following code. Generally C programmers tend
    > to write code like that I am looking for simple way to do it in python.
    >
    > --- BEGIN ---
    > counter = 0
    >
    > def foo():
    > if counter < 10:
    > print "count = ", counter
    > counter += 1
    >
    > foo()
    > --- END ---[/color]

    You need to tell explicitly that the name 'counter' is global. It is
    because used this statment in your function:

    counter += 1

    which is a rebind of the name 'counter'. So python thinks it must be a
    local name.
    Here is what you want:

    counter = 0

    def foo():
    global counter
    if counter < 10:
    print "count = ", counter
    counter += 1

    foo()



    Comment

    • Jacek Generowicz

      #3
      Re: Scope question

      Subhash Chandra <nospam_yschand ra@yahoo.com> writes:
      [color=blue]
      > How can I make counter variable in function foo reference to global
      > counter variable in the following code.[/color]

      add the line " global counter" just after "def foo():"
      [color=blue]
      > Generally C programmers tend to write code like that I am looking
      > for simple way to do it in python.[/color]

      Generally C programmers do horrible things. Please consider whether
      you really, really should be emulating C style in Python.
      [color=blue]
      > --- BEGIN ---
      > counter = 0
      >
      > def foo():
      > if counter < 10:
      > print "count = ", counter
      > counter += 1
      >
      > foo()
      > --- END ---[/color]

      Comment

      • Miki Tebeka

        #4
        Re: Scope question

        Hello Subhash,
        [color=blue]
        > --- BEGIN ---
        > counter = 0
        >
        > def foo():
        > if counter < 10:
        > print "count = ", counter
        > counter += 1
        >
        > foo()
        > --- END ---[/color]
        Today we have generators:[color=blue][color=green][color=darkred]
        >>> def foo():[/color][/color][/color]
        for count in range(10):
        print "count = ", count
        yield count
        count += 1[color=blue][color=green][color=darkred]
        >>> gen = foo()
        >>> gen.next()[/color][/color][/color]
        count = 0
        0[color=blue][color=green][color=darkred]
        >>> gen.next()[/color][/color][/color]
        count = 1
        1[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        HTH.
        Miki

        Comment

        Working...