import parent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Hoover

    #1

    import parent

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

    Thanks.

  • Bjoern Schliessmann

    #2
    Re: import parent

    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.

    Comment

    • John Machin

      #3
      Re: import parent

      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_functi on 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 "interestin g". 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

      Comment

      • Ben Finney

        #4
        Re: import parent

        Greg@bag.python .org, Hoover@bag.pyth on.org 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

        Comment

        • Larry Bates

          #5
          Re: import parent

          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=par ent

          def somemethod(self ):
          print self.parent.som elist


          class foo:
          def __init__(self):
          self.somelist=['a','b','c']
          self.bars=[]
          self.bars.appen d(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

          Comment

          • Gary Herron

            #6
            Re: import parent

            Greg@bag.python .org 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




            Comment

            Working...