How two modules call functions defined in each other?

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Sun Mar 20 05:20:16 EST 2005


On 19 Mar 2005 18:07:31 -0800, rumours say that "Tian" <wangtianthu at gmail.com>
might have written:

>I am python beginner, I have a question about the interdependence of
>modules.

>For example, when I have two modules:
>
>module1.py
>-------------
>def plus(x):
>  return add(x,1)
>
>
>module2.py
>-------------
>def add(x,y):
>  return x+y
>
>def plus2(x):
>  return plus(x)+1

>How should I write "import" in both files?

In module1, import module2 and vice versa.  From moduleX, you access any
attribute (function or "variable") of moduleY by using moduleY.attribute .

>What about the global varibals? is there anything like "extern" keyword
>in C?

There are no "global variables" in Python, only module-level attributes.  If by
global you mean the main program's (which is also a module) attributes, in your
other modules do a:

import __main__

and then access its attributes as __main__.attribute .  It's not generally a
good idea in Python, though, so you might like to explain what you need to do so
that we suggest alternate approaches.

>or python has some other solutions?

Cheers!
-- 
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...



More information about the Python-list mailing list