Re: Imports awareness in Imported modules problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mohamed Yousef

    Re: Imports awareness in Imported modules problem

    On Sun, Aug 24, 2008 at 9:59 AM, Fredrik Lundh <fredrik@python ware.comwrote:
    Mohamed Yousef wrote:
    >
    >why am i doing this in the first place
    >I'm in the process of a medium project where imports of modules start
    >to make a jungle and i wanted all needed imports to be in a single
    >file (namely __init__.py)
    >and then all imports are made once and other modules feel it
    >
    Python doesn't use a global namespace -- importing a given module into one
    module doesn't make it visible everywhere else (and trust me, this is a very
    good thing).
    why isn't it a good thing (even if optional)
    consider the sitution in which a utility module is used every where
    else - other modules -
    you may say import it in them all , what i changed it's name ? go back
    change all imports... this doesn't seem good
    and what about package wide varailbles ?
    (it uses a global module cache, though, so it's only the first import that
    actually loads the module)
    >
    >my goal is basically making W() aware of the re module when called
    >from A
    >
    to do that, add "import re" to the top of the C module.
    and sys ,string.. etc add them all twice or four / n times ?
    remove one and then forget to remove one of them in a file and start
    debugging ... this really doesn't look to be a good practice
    see this page for a little more on Python's import mechanism:
    >

    >
    Regards,
    Mohamed Yousef
  • alex23

    #2
    Re: Imports awareness in Imported modules problem

    On Aug 24, 8:34 pm, "Mohamed Yousef" <harrr...@gmail .comwrote:
    and sys  ,string.. etc add them all twice or four / n times ?
    remove one and then forget to remove one of them in a file and start
    debugging ... this really doesn't look to be a good practice
    No, having each module contain all of the necessary imports for itself
    -is- good practice.

    While it might seem more convenient to define all of your imports in
    one place, what you're really doing is injecting -all- references to -
    all- imported items into -every- module. Having a global soup of
    imported items like such is much, much harder to control for
    unexpected name conflicts.

    Ideally, you want each module's namespace to -only- contain references
    to the external entities it's actually using. This not only makes each
    module self-contained and more open to independent re-use, it makes
    them -incredibly- easier to debug.

    Please don't confuse temporary convenience with good practice.

    Comment

    • Mohamed Yousef

      #3
      Re: Imports awareness in Imported modules problem

      On Sun, Aug 24, 2008 at 2:27 PM, alex23 <wuwei23@gmail. comwrote:
      On Aug 24, 8:34 pm, "Mohamed Yousef" <harrr...@gmail .comwrote:
      >and sys ,string.. etc add them all twice or four / n times ?
      >remove one and then forget to remove one of them in a file and start
      >debugging ... this really doesn't look to be a good practice
      >
      No, having each module contain all of the necessary imports for itself
      -is- good practice.
      >
      While it might seem more convenient to define all of your imports in
      one place, what you're really doing is injecting -all- references to -
      all- imported items into -every- module. Having a global soup of
      imported items like such is much, much harder to control for
      unexpected name conflicts.
      >
      Ideally, you want each module's namespace to -only- contain references
      to the external entities it's actually using. This not only makes each
      module self-contained and more open to independent re-use, it makes
      them -incredibly- easier to debug.
      >
      Please don't confuse temporary convenience with good practice.
      mm , this seems to be logically or another good behind logic . as PHP
      already uses that convention -from which i thought python also does -

      but what about module-wide variables what is the approach to them ,
      will i have to store them in a memory table (SQL) , or keep passing
      them between objects - which i really hate -

      Comment

      • alex23

        #4
        Re: Imports awareness in Imported modules problem

        On Aug 24, 9:43 pm, "Mohamed Yousef" <harrr...@gmail .comwrote:
        but what about module-wide variables what is the approach to them ,
        will i have to store them in a memory table (SQL) , or keep passing
        them between objects - which i really hate -
        If you have a set of global variables that you want to share among
        modules, put them all into a module.

        config.py:
        var1 = 'a'

        a.py:
        import config
        config.var1 = 'z'

        b.py:
        import config
        print config.var1
        >>import a
        >>import b
        z

        A module is only really imported once; each subsequent import is given
        a reference to that imported module, so any changes to the module
        values are shared amongst each importing module.

        Comment

        • Fredrik Lundh

          #5
          Re: Imports awareness in Imported modules problem

          Mohamed Yousef wrote:
          mm , this seems to be logically or another good behind logic . as PHP
          already uses that convention -from which i thought python also does -
          I'm not sure arguing with anyone who tries to help you is the best way
          to learn a new programming language.

          </F>

          Comment

          Working...