By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

How two modules call functions defined in each other?

P: n/a
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?
What about the global varibals? is there anything like "extern" keyword
in C?
or python has some other solutions?

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 19 Mar 2005 18:07:31 -0800, rumours say that "Tian" <wa*********@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...
Jul 18 '05 #2

P: n/a
Tian wrote:
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?
What about the global varibals? is there anything like "extern" keyword
in C?
or python has some other solutions?


Yes. Either define plus and add in module1 or define each one in a
separate module.

Reinhold
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.