question about scope

Magnus Lycka lycka at carmen.se
Wed Feb 22 04:23:14 EST 2006


John Salerno wrote:
> But as far as ifs and loops, is there such a thing as scope in them? 

No.

Local scopes are introduced with "def" or "class", nothing
else (or did I forget something?). There is nothing in Python
that corresponds directly to the { } in C and C++. If you want
data to exist in some "other scope", put them in a dictionary
(there you have your { } ;^) and use that explicitly.

Each Python module/file is a global scope in Python.

Something to consider if you come from C, is that variables
and assignments in Python are conceptually different from C.

Your objects/values are never created in a local scope. They
are always created on the heap, as if you would use malloc in
C or new in C++. All variables are pointers/references, and
the objects/values are automatically garbage collected. From
a C perspective, the only kind of variables you have are void
pointers, but that's ok, because you can only point these
pointers to managed objects that are handled by the runtime
systems. The runtime system handles both memory allocation
and deallocation and type checks for you. In some particular
cases (e.g. numeric types) it will also perform casting when
you need it.



More information about the Python-list mailing list