Tracing variable scope (local vs. global)

Discussion in 'Python' started by Manuel Graune, Dec 1, 2009.

  1. Hello,

    consider the following piece of code:

    a=1
    b=2

    def foo(c):
    b=3
    return a + b + c

    In this case, when calling "foo", "a" will take the global value,
    "b" will take the local value and "c" will take the value assigned
    when calling the function.

    Since I consider this behaviour a possible source of bugs due to
    personal sloppiness (e. g. forgetting to put "a=4" inside the
    function-body):

    Is there any way to automatically check that all variables in a
    function are either local or passed in as arguments?

    Regards,

    Manuel Graune

    --
    A hundred men did the rational thing. The sum of those rational choices was
    called panic. Neal Stephenson -- System of the world
    http://www.graune.org/GnuPG_pubkey.asc
    Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
    Manuel Graune, Dec 1, 2009
    #1
    1. Advertising

  2. Manuel Graune

    Lie Ryan Guest

    On 12/2/2009 9:02 AM, Manuel Graune wrote:
    >
    > Hello,
    >
    > consider the following piece of code:
    >
    > a=1
    > b=2
    >
    > def foo(c):
    > b=3
    > return a + b + c
    >
    > In this case, when calling "foo", "a" will take the global value,
    > "b" will take the local value and "c" will take the value assigned
    > when calling the function.
    >
    > Since I consider this behaviour a possible source of bugs due to
    > personal sloppiness (e. g. forgetting to put "a=4" inside the
    > function-body):
    >
    > Is there any way to automatically check that all variables in a
    > function are either local or passed in as arguments?


    If you really mean what you're saying, you won't be able to use builtin
    functions.

    Anyway... the answer to your problem is a strong naming convention and
    to not store variables in the global namespace; put everything in a
    class and use a main() function so your initialization code doesn't
    clutter up the global namespace. As such, there shouldn't be a
    lowercased name in the global namespace (class use CamelCase and
    constants UPPERCASED); and all lowercased names are local (or builtins,
    use pylint to check for the case of shadowing builtins).
    Lie Ryan, Dec 1, 2009
    #2
    1. Advertising

  3. Manuel Graune schrieb:
    > Hello,
    >
    > consider the following piece of code:
    >
    > a=1
    > b=2
    >
    > def foo(c):
    > b=3
    > return a + b + c
    >
    > In this case, when calling "foo", "a" will take the global value,
    > "b" will take the local value and "c" will take the value assigned
    > when calling the function.
    >
    > Since I consider this behaviour a possible source of bugs due to
    > personal sloppiness (e. g. forgetting to put "a=4" inside the
    > function-body):
    >
    > Is there any way to automatically check that all variables in a
    > function are either local or passed in as arguments?


    No. And as long as you don't have the habit of using global variables to
    a non-reasonable extent, and follow some simple conventions such as
    spelling them in all uppercase and with more meaningful names such as
    the ones above - it's not an actual problem.

    Diez
    Diez B. Roggisch, Dec 1, 2009
    #3
  4. Manuel Graune

    Terry Reedy Guest

    Manuel Graune wrote:
    > Hello,
    >
    > consider the following piece of code:
    >
    > a=1
    > b=2
    >
    > def foo(c):
    > b=3
    > return a + b + c
    >
    > In this case, when calling "foo", "a" will take the global value,
    > "b" will take the local value and "c" will take the value assigned
    > when calling the function.
    >
    > Since I consider this behaviour a possible source of bugs due to
    > personal sloppiness (e. g. forgetting to put "a=4" inside the
    > function-body):
    >
    > Is there any way to automatically check that all variables in a
    > function are either local or passed in as arguments?


    There are at least 3 code checking programs: pychecker, pylint,
    pyflakes. Perhaps one of them has an option to check for non-builtin
    globals. Would not be too difficult to add, I suspect.
    Terry Reedy, Dec 2, 2009
    #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. Mohanasundaram
    Replies:
    44
    Views:
    1,037
    Keith Thompson
    Aug 24, 2004
  2. sairam
    Replies:
    2
    Views:
    354
    Steve Holden
    Apr 5, 2007
  3. David Filmer
    Replies:
    19
    Views:
    223
    Kevin Collins
    May 21, 2004
  4. Erwin Moller

    local/global scope confusion

    Erwin Moller, Nov 12, 2008, in forum: Javascript
    Replies:
    30
    Views:
    251
    Dr J R Stockton
    Nov 19, 2008
  5. Andrew Falanga
    Replies:
    2
    Views:
    191
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page