Re: 2 questions about scope

From: Alex Martelli (aleaxit_at_yahoo.com)
Date: 10/25/04


Date: Mon, 25 Oct 2004 22:53:39 +0200

Andrew Dalke <adalke@mindspring.com> wrote:
   ...
> but you really shouldn't. Or use a function scope,
>
> i = "something"
> def scope():
> for i in range(100):
> ...
> scope()
>
> I mostly use the last when I want some non-trivial
> initialization in my module and don't want the various
> variables hanging around in the process space. I'll
> also follow it up with a "del scope" so that no one
> can reference the initialization code.

Execution in function scope is also generally quite a bit faster than
execution at module scope or in class scope. Any nontrivial code in
module scope is best scooped up into a function, which is then executed
and removed. I'd suggest naming the function '_init' or something like
that -- with a leading underscore to indicate to the reader of the
source that it's a private implementation detail (not _needed_, since
you're gonna delete it after running it once, but helpful to the reader
at no cost to the code's author).

Alex



Relevant Pages

  • class variables: scoping
    ... static int x; ... scope, but is reinitialized each time the method is called. ... Now, I would like to have a variable which has function scope, ... time I instantiate an instance of Foo a new such variable is created. ...
    (comp.lang.cpp)
  • Re: Function and variable declarations
    ... >> levels of indirection, three dimensions, and three levels of scope. ... >> In C these are global, file scope, and function scope. ... I'm still a bit 'old school' I'm afraid, and typically declare my variables ... things easier, namely, decent IDEs. ...
    (comp.lang.c)
  • Re: Confused by Python and nested scoping (2.4.3)
    ... There are only two scopes in Python -- global scope and function scope. ... def outer: ... doesn't need warning. ...
    (comp.lang.python)
  • Re: Function and variable declarations
    ... A human being can cope with three layers of nested parentheses, three levels of indirection, three dimensions, and three levels of scope. ... In C these are global, file scope, and function scope. ... If, by putting your variables in subblocks, you can reduce the number of outstanding variables that have to be kept in active memory at any given point, you've achieved something. ...
    (comp.lang.c)
  • Re: Switch() parsing insanity
    ... snip ... ... >> function scope. ... It can be used (in a goto statement) ... > variables with that scope. ...
    (comp.lang.c)