Confused about namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Martelli

    #16
    Re: Confused about namespaces

    KvS <keesvanschaik@ gmail.com> wrote:
    [color=blue][color=green]
    > > There's no reason not to just "import wx" if you want that.[/color]
    >
    > Yes, that's clear. But if you would make some huge application that has
    > a large number of nested modules, each importing the former one, then
    > avoiding the use of "from ... import *" would mean that you have to use
    > long references like foo1.foo2.... to get to the lowest modules plus[/color]

    Not at all -- you may perfectly well "import a.b.c.d as e" and use e as
    the reference throughout the importing module.

    But the point is, modules should be MODULAR (duh) -- if I 'import x' I
    should NOT rely on what x itself imports (and so on transitively), just
    on what x ``exports'', so to speak, at its TOP level. The Law of
    Demeter in its simplified form "if you have more than one dot in your
    reference you're doing things wrong" applies. In fact, the many-dots
    form of module naming is intended for nested PACKAGES, *NOT* for
    "modules which import each other".
    [color=blue]
    > that you'd have to check each module for imports outside this tree.[/color]

    Why would you have to check any module for such "outside imports"? I
    don't see the need for any checks.


    If you just simply forget the very _existence_ of "from X import *",
    your Python code can only grow better as a result. I earnestly hope it
    disappears come Python 3.0 time...


    Alex

    Comment

    • Alex Martelli

      #17
      Re: Confused about namespaces

      KvS <keesvanschaik@ gmail.com> wrote:
      [color=blue][color=green]
      > > There's no reason not to just "import wx" if you want that.[/color]
      >
      > Yes, that's clear. But if you would make some huge application that has
      > a large number of nested modules, each importing the former one, then
      > avoiding the use of "from ... import *" would mean that you have to use
      > long references like foo1.foo2.... to get to the lowest modules plus[/color]

      Not at all -- you may perfectly well "import a.b.c.d as e" and use e as
      the reference throughout the importing module.

      But the point is, modules should be MODULAR (duh) -- if I 'import x' I
      should NOT rely on what x itself imports (and so on transitively), just
      on what x ``exports'', so to speak, at its TOP level. The Law of
      Demeter in its simplified form "if you have more than one dot in your
      reference you're doing things wrong" applies. In fact, the many-dots
      form of module naming is intended for nested PACKAGES, *NOT* for
      "modules which import each other".
      [color=blue]
      > that you'd have to check each module for imports outside this tree.[/color]

      Why would you have to check any module for such "outside imports"? I
      don't see the need for any checks.


      If you just simply forget the very _existence_ of "from X import *",
      your Python code can only grow better as a result. I earnestly hope it
      disappears come Python 3.0 time...


      Alex

      Comment

      • Bruno Desthuilliers

        #18
        Re: Confused about namespaces

        KvS a écrit :[color=blue]
        > Ok, makes sense but didn't seem "natural" to me,[/color]

        It will seem more natural if you understand that modules should be
        modulars (ie: low coupling, high cohesion). A module should *never*
        bother about no rely upon other modules being imported by the module it
        imports itself. Err, not quite clear... Let's rephrase it : a module
        should only used symbols it defines itself or that it *explicitely*
        imports from other modules.
        [color=blue]
        > although it is an
        > obvious consequence of what you just pointed out, namely that modules
        > are evaluated in their own namespace, something to keep in mind... On
        > the other hand it does apparently work recursively "the other way
        > around" since I didn't explicitly import wx in main.py but only
        > indirect via importing GUIclasses in which wx is imported right?[/color]

        You've already got technical answers to this - please carefully (re)read
        Alex Martelli's post.

        Now about coding style: this is *very* Bad Style(tm). Your main module
        imports should look like this:

        import wx
        # import settings -> we don't use the settings module here
        from GUIclasses import KFrame

        (...)

        and the GUIclasses module's import :

        import wx
        import wx.lib.mixins.l istctrl as listmix
        import settings -> *here* we use the settings module

        (...)

        Python's philosophy is to favour readability.

        Talking about imports, take time to open your Python interactive shell
        and type "import this" !-)

        Comment

        • Bruno Desthuilliers

          #19
          Re: Confused about namespaces

          KvS a écrit :[color=blue]
          > Ok, makes sense but didn't seem "natural" to me,[/color]

          It will seem more natural if you understand that modules should be
          modulars (ie: low coupling, high cohesion). A module should *never*
          bother about no rely upon other modules being imported by the module it
          imports itself. Err, not quite clear... Let's rephrase it : a module
          should only used symbols it defines itself or that it *explicitely*
          imports from other modules.
          [color=blue]
          > although it is an
          > obvious consequence of what you just pointed out, namely that modules
          > are evaluated in their own namespace, something to keep in mind... On
          > the other hand it does apparently work recursively "the other way
          > around" since I didn't explicitly import wx in main.py but only
          > indirect via importing GUIclasses in which wx is imported right?[/color]

          You've already got technical answers to this - please carefully (re)read
          Alex Martelli's post.

          Now about coding style: this is *very* Bad Style(tm). Your main module
          imports should look like this:

          import wx
          # import settings -> we don't use the settings module here
          from GUIclasses import KFrame

          (...)

          and the GUIclasses module's import :

          import wx
          import wx.lib.mixins.l istctrl as listmix
          import settings -> *here* we use the settings module

          (...)

          Python's philosophy is to favour readability.

          Talking about imports, take time to open your Python interactive shell
          and type "import this" !-)

          Comment

          • KvS

            #20
            Re: Confused about namespaces

            Thanks a lot for all the answers. After rereading everything said here
            today it's become more clear to me what you guys are telling me and
            I'll actively try to forget about "from ... import *" ;).

            Comment

            • KvS

              #21
              Re: Confused about namespaces

              Thanks a lot for all the answers. After rereading everything said here
              today it's become more clear to me what you guys are telling me and
              I'll actively try to forget about "from ... import *" ;).

              Comment

              • Alex Martelli

                #22
                Re: Confused about namespaces

                KvS <keesvanschaik@ gmail.com> wrote:
                [color=blue]
                > Thanks a lot for all the answers. After rereading everything said here
                > today it's become more clear to me what you guys are telling me and
                > I'll actively try to forget about "from ... import *" ;).[/color]

                I commend you for your decision. It's a construct that I sometimes find
                quite handy in an experimentation session in the interactive
                interpreter, but just has no really good place in 'true' code;-0.


                Alex

                Comment

                • Alex Martelli

                  #23
                  Re: Confused about namespaces

                  KvS <keesvanschaik@ gmail.com> wrote:
                  [color=blue]
                  > Thanks a lot for all the answers. After rereading everything said here
                  > today it's become more clear to me what you guys are telling me and
                  > I'll actively try to forget about "from ... import *" ;).[/color]

                  I commend you for your decision. It's a construct that I sometimes find
                  quite handy in an experimentation session in the interactive
                  interpreter, but just has no really good place in 'true' code;-0.


                  Alex

                  Comment

                  Working...