Reading Variables From Modules ?

Hans Nowak hnowak at cuci.nl
Tue Sep 11 06:06:39 EDT 2001


>===== Original Message From pmoscatt at bigpond.net.au =====
>I have a simple question - well to some I hope :-)
>
>If I was to have a module named MyModule.py which had a function like:
>
>        def TestNumber():
>                A = 10
>                return

As you already pointed out yourself, this doesn't fly. The reason is that "A = 
10" creates A locally; it is only visible within the function. You can get 
around this by using 'global':

  def TestNumber():
    global A
    A = 10

This is kind of yucky, but does work. :)  And then you can do:

>        import MyModule
>        if MyModule.A > 10 dosomething

If this is a value that doesn't change, maybe it's better to use a function 
instead, e.g.

  if MyModule.my_convoluted_function(42) > 10:
      do_something()

HTH,

--Hans Nowak





More information about the Python-list mailing list