import parent

G

Greg Hoover

How does one get access to the class that imported a module. For example:
foo imports bar -- how does bar access foo?

Thanks.
 
B

Bjoern Schliessmann

Greg said:
How does one get access to the class that imported a module. For
example: foo imports bar -- how does bar access foo?

Directly? Not at all in sane programs, IMHO. That's the job of clear
interfaces.

Regards,


Björn
 
J

John Machin

How does one get access to the class that imported a module. For example:
foo imports bar -- how does bar access foo?

It shouldn't (in any language, not just Python). Callees should not in
general need to inspect their caller's data structures, and should not
uninvitedly alter their caller's data structures.

Given Foo is a class in module foo, in general it is preferred that
all operations on instances of Foo are carried out by methods of the
Foo class. Given oof is such an instance, and there is a documented
attribute zot, then you *may* pass oof as an arg to a
bar.some_function which may do things like
if oof_arg.zot > 42:
oof_arg.zot = 42
but this is not the preferred way.

Your subject "import parent" is "interesting". In what way is foo
considered to be the parent of bar?

foo imports bar which imports foo is called a "circular import".
Google in this newsgroup should dredge up some threads on this topic.
Bottom line: if what you want to do really would involve a circular
import, then you have a design mess -- foo+bar needs to be refactored
by moving functionality into different modules so that you don't get a
circular import. The result may be 1, 2 (different), or 3 modules.

If you tell us a bit more about what you are trying to do, we may be
able to help you more.

Cheers,
John
 
B

Ben Finney

How does one get access to the class that imported a module. For
example: foo imports bar -- how does bar access foo?

If bar needs to know something specific from foo, then bar should
expose an interface that asks explicitly for that information, so that
foo can explicitly provide it.
 
L

Larry Bates

Greg said:
How does one get access to the class that imported a module. For example:
foo imports bar -- how does bar access foo?

Thanks.
I think we are having a problem understanding what you are asking.
If you mean an instance of class foo contains an instance of
class bar how would bar reference attributes or other data in
foo then I think I'll tackle that one. The best way I've seen
is the way that wxWindows does it.

class bar:
def __init__(self, parent)
self.parent=parent

def somemethod(self):
print self.parent.somelist


class foo:
def __init__(self):
self.somelist=['a','b','c']
self.bars=[]
self.bars.append(bar(self))


At least that's the way I understand it and it seems to work. If
this isn't what you are asking just disregard.

-Larry
 
G

Gary Herron

How does one get access to the class that imported a module. For example:
foo imports bar -- how does bar access foo?

Thanks.

If bar wants to access foo, it just imports it. If foo imports bar as
well, then you have circular imports, but Python can handle this with no
problem.

There can be a problem with circular imports if you try to import
specific names from the module, and the circularity happens before those
names are defined. But just:
foo:
import bar
....

bar:
import foo
....
works fine.

However... as several others have pointed out, circular imports are
often a sign that your software design is not very well thought out.
It's usually better to have your thought process, your design and your
imports organized in a hierarchal fashion.

Gary Herron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top