Re: 2 questions about scope

From: Andrew Dalke (adalke_at_mindspring.com)
Date: 10/25/04


Date: Mon, 25 Oct 2004 19:25:41 GMT

Neal D. Becker wrote:
> Actually, these don't introduce a block in c++ either. The braces do.

In C++

for (int i=0; i<100; spam(i), ++i)
    ;

the 'i' is in the for statement's scope even though there
are no braces.

To respond to the OP's question, one big difference between
C++ and Python's scoping systems is that Python doesn't have
a distinct variable declaration. In C++ you could say

{
   char i[] = "something";
   ...
   for (int i=0; i<100; i++) {
     ...
   }
}

and the compiler knows you want a new variable 'i'.

In Python, the equivalent might be

   i = "something"
   for i in range(100):
     ...

There's nothing to tell Python that the 'i' used
in the for statement is different than the i in the
outer scope, so it assumes you meant the same variable.

The only scopes created in Python are for modules,
classes, and functions. As I pointed out a couple
weeks ago, you could fake scope with a class statement,
as in

   i = "something"
   class scope:
     for i in range(100):
       ...

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.

                                Andrew
                                dalke@dalkescientific.com



Relevant Pages

  • Re: Class Variable Inheritance
    ... from most specific to least specific scope. ... If you actually assigned c1.mastervar, rather than modifying the dict, ... I strongly recommend a read of the book "Learning Python" if you want to ... classes, inheritance, and namespaces work. ...
    (comp.lang.python)
  • Re: [Python-Dev] The baby and the bathwater (Re: Scoping, augmented assignment, fast locals &#
    ... but on a *space* of programs of identical function, as of manner of probing recent features and modules of Python that I manage to apply to it. ... parent lexical scope, be performed on the object bound to a parent scope ... I guess not in the manner of someone who started programming when programs had to fit in a couple dozen kbytes. ... intersection of the file tagged "arrogant would-be mind-readers", and of the file tagged "you are late on the normal trajectory if you did not reproduce my own oh-so-significant mistakes", both well-populated already, although I forgot who I put in them. ...
    (comp.lang.python)
  • Re: Why we will use obj$func() often
    ... To me a scope is just a namespace defined by ... that is structurally a criticism. ... > final Prothon before you criticize my wanton destruction of the Python ... You can't afford to indulge my tastes in programming languages, ...
    (comp.lang.python)
  • Re: Code block literals
    ... >> a code block (Python's lambda is too puny to count as such), ... Python syntactic context allows -- it really feels alien. ... the same scope, use of new variables isn't (creates a new ...
    (comp.lang.python)
  • Re: python newbie
    ... global vars - python sets scope to the block a var is ... to global vars in a function, what I'm not clear on is does ... If x is referenced, but not assigned to, then Python does not ... global statement to declare your intation. ...
    (comp.lang.python)