import parent

Discussion in 'Python' started by Greg Hoover, Feb 27, 2007.

  1. Greg Hoover

    Greg Hoover Guest

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

    Thanks.
    Greg Hoover, Feb 27, 2007
    #1
    1. Advertising

  2. Greg Hoover wrote:

    > 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

    --
    BOFH excuse #407:

    Route flapping at the NAP.
    Bjoern Schliessmann, Feb 27, 2007
    #2
    1. Advertising

  3. Greg Hoover

    John Machin Guest

    On Feb 28, 8:01 am, Greg Hoover wrote:
    > 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
    John Machin, Feb 27, 2007
    #3
  4. Greg Hoover

    Ben Finney Guest

    , writes:

    > 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.

    --
    \ "Laurie got offended that I used the word 'puke.' But to me, |
    `\ that's what her dinner tasted like." -- Jack Handey |
    _o__) |
    Ben Finney
    Ben Finney, Feb 27, 2007
    #4
  5. Greg Hoover

    Larry Bates Guest

    Greg Hoover wrote:
    > 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
    Larry Bates, Feb 27, 2007
    #5
  6. Greg Hoover

    Gary Herron Guest

    wrote:
    > 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
    Gary Herron, Feb 28, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy
    Replies:
    2
    Views:
    3,100
  2. mark
    Replies:
    1
    Views:
    558
  3. per9000
    Replies:
    7
    Views:
    396
    Magnus Lycka
    Feb 27, 2006
  4. metal
    Replies:
    8
    Views:
    490
    Bruno Desthuilliers
    Oct 30, 2009
  5. Bill S.
    Replies:
    2
    Views:
    198
    Bill S.
    May 8, 2004
Loading...

Share This Page