Help a C++ escapee!

Jason tenax.raccoon at gmail.com
Thu Jun 7 11:55:24 EDT 2007


On Jun 7, 1:59 am, "Simon Pickles" <sipick... at hotmail.com> wrote:
> Hi,
>
> Can someone help me leave the murky c++ world and enter shiny pythonland?
>
> I have a problem with importing and global variables, here's my code:
[snip!]
> When run, I come unstuck here:
>
>             self.clientSocket, self.clientAddress = network.accept()
>
> I get a nameError on 'network', yet it is one in the global namespace,
> defined in server.py before CServerThread.Listen() is called.
>
> In c++, I understood how to declare variables, here I have a problem. Its
> like I need an extern statement from c++.
>
> Can anyone clarify this for me?

The others have covered your problem.  I'd just like to add a little
detail.

As the others have mentioned, global variables reside in their module
namespace.  There is no truly "global" namespace, and that hasn't
really shown up as a problem in Python.  There are two tricks to this:

  1)  Modules are only executed once, at the first import.  Every
additional import merely gets the module object, but does not execute
the code again.  Try adding a print statement outside all class and
function statements in a module, then import it multiple times.
You'll only see the results of the print statement once.

You can also start up python with the "-v" flag.  Python will then
tell you whenever it executes a module.

This means that you can have a "global" module.  Create a Python
module with all the variables that you want, and just import that
module in all places that need those variables.  If you find yourself
stuffing a huge number of variables into your global module, you
probably need to rethink your program design.

Otherwise, put the global data in the various modules that create and
update it.  Usually, your other code will only need to access that
data under more rare conditions.

If you absolutely need a module to be re-executed, the "reload"
function will do what you want.

  2)  Modules do not have to be imported at the start of the file.  A
module import statement can occur any time code is executed.  By
convention, we place the import statement at the beginning of the
block where we use it.

Remember that Python isn't C++.  If you place an import statement in a
function, Python doesn't try to import that statement until the
function is called.  This is very different from C/C++'s #include
preprocessor statements.

If you're having problems with circular references, the import
statement can usually
be moved inside of functions or methods that need them, or the files
can be refactored to  get rid of the circular reference.

Here's something that shows both points.  I started python by typing
in "python -v".  There's a large number of imported modules that
Python automatically loads.  After that finishes:
>>> def spam(x):
...     x = 5 * x
...     import math   # Math won't be imported until this function is
called
...     return math.pow(x, 2)
...
>>> spam(1) # This should import the math module, executing its contents
import math # builtin
25.0
>>> spam(2) # spam() will now use the previously import math module.  (This is fast)
100.0

Hope this helps!

  --Jason




More information about the Python-list mailing list