Confused about namespaces

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

    #1

    Confused about namespaces

    Hi all,

    to start with, excuse me, I'm still learning programming alltogether,
    probably I'm making some fundamental mistake here...

    I have the files settings.py, GUIclasses.py and main.py in the same
    directory. In the file main.py are the statements:

    import settings
    from GUIclasses import *

    class Toepassing(wx.A pp):
    def OnInit(self):
    window = KFrame(None, "Testerdete st", (1000,900))
    self.SetTopWind ow(window)
    window.Show(Tru e)
    return True

    app = Toepassing(None )
    app.MainLoop()

    In the file GUIclasses.py I have the statements:

    import wx
    import wx.lib.mixins.l istctrl as listmix

    class KFrame(wx.Frame ):
    def __init__(self, parent, title, Size):
    ...some code...
    self.lst = settings.attrLi jst
    ....some more code......

    Now if I run the main.py file I get the error:

    File "G:\Programmere n\Codes contactenlijst\ GUIclasses.py", line 40,
    in __init_
    _
    self.lst = settings.attrLi jst
    NameError: name 'settings' is not defined

    Why is this? Since "from GUIclasses import *" this KFrame is now at
    "the lowest" namespace and should therefore be able to make use of any
    variables living there, including "settings.* ", no?

    Thanks in advance!

    - Kees

  • Chris Mellon

    #2
    Re: Confused about namespaces

    On 18 Nov 2005 15:04:23 -0800, KvS <keesvanschaik@ gmail.com> wrote:[color=blue]
    > Hi all,
    >
    > to start with, excuse me, I'm still learning programming alltogether,
    > probably I'm making some fundamental mistake here...
    >
    > I have the files settings.py, GUIclasses.py and main.py in the same
    > directory. In the file main.py are the statements:
    >
    > import settings
    > from GUIclasses import *
    >
    > class Toepassing(wx.A pp):
    > def OnInit(self):
    > window = KFrame(None, "Testerdete st", (1000,900))
    > self.SetTopWind ow(window)
    > window.Show(Tru e)
    > return True
    >
    > app = Toepassing(None )
    > app.MainLoop()
    >
    > In the file GUIclasses.py I have the statements:
    >
    > import wx
    > import wx.lib.mixins.l istctrl as listmix
    >
    > class KFrame(wx.Frame ):
    > def __init__(self, parent, title, Size):
    > ...some code...
    > self.lst = settings.attrLi jst
    > ....some more code......
    >
    > Now if I run the main.py file I get the error:
    >
    > File "G:\Programmere n\Codes contactenlijst\ GUIclasses.py", line 40,
    > in __init_
    > _
    > self.lst = settings.attrLi jst
    > NameError: name 'settings' is not defined
    >
    > Why is this? Since "from GUIclasses import *" this KFrame is now at
    > "the lowest" namespace and should therefore be able to make use of any
    > variables living there, including "settings.* ", no?
    >[/color]

    import is not like a C/C++ #include - the code in the imported module
    is evaluated in it's own namespace, not in the namespace of the
    importing file.

    So no, this is expected behavior, and should be solved by importing
    settings in GUIclasses.py as well.

    You don't need to worry about multiple or redundent imports.

    [color=blue]
    > Thanks in advance!
    >
    > - Kees
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >[/color]

    Comment

    • Chris Mellon

      #3
      Re: Confused about namespaces

      On 18 Nov 2005 15:04:23 -0800, KvS <keesvanschaik@ gmail.com> wrote:[color=blue]
      > Hi all,
      >
      > to start with, excuse me, I'm still learning programming alltogether,
      > probably I'm making some fundamental mistake here...
      >
      > I have the files settings.py, GUIclasses.py and main.py in the same
      > directory. In the file main.py are the statements:
      >
      > import settings
      > from GUIclasses import *
      >
      > class Toepassing(wx.A pp):
      > def OnInit(self):
      > window = KFrame(None, "Testerdete st", (1000,900))
      > self.SetTopWind ow(window)
      > window.Show(Tru e)
      > return True
      >
      > app = Toepassing(None )
      > app.MainLoop()
      >
      > In the file GUIclasses.py I have the statements:
      >
      > import wx
      > import wx.lib.mixins.l istctrl as listmix
      >
      > class KFrame(wx.Frame ):
      > def __init__(self, parent, title, Size):
      > ...some code...
      > self.lst = settings.attrLi jst
      > ....some more code......
      >
      > Now if I run the main.py file I get the error:
      >
      > File "G:\Programmere n\Codes contactenlijst\ GUIclasses.py", line 40,
      > in __init_
      > _
      > self.lst = settings.attrLi jst
      > NameError: name 'settings' is not defined
      >
      > Why is this? Since "from GUIclasses import *" this KFrame is now at
      > "the lowest" namespace and should therefore be able to make use of any
      > variables living there, including "settings.* ", no?
      >[/color]

      import is not like a C/C++ #include - the code in the imported module
      is evaluated in it's own namespace, not in the namespace of the
      importing file.

      So no, this is expected behavior, and should be solved by importing
      settings in GUIclasses.py as well.

      You don't need to worry about multiple or redundent imports.

      [color=blue]
      > Thanks in advance!
      >
      > - Kees
      >
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]

      Comment

      • KvS

        #4
        Re: Confused about namespaces

        Ok, makes sense but didn't seem "natural" to me, 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?

        Thanks. :).

        Comment

        • KvS

          #5
          Re: Confused about namespaces

          Ok, makes sense but didn't seem "natural" to me, 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?

          Thanks. :).

          Comment

          • Chris Mellon

            #6
            Re: Confused about namespaces

            On 18 Nov 2005 15:29:43 -0800, KvS <keesvanschaik@ gmail.com> wrote:[color=blue]
            > Ok, makes sense but didn't seem "natural" to me, 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]

            it worked because all the code that used stuff from the wx namespace
            was in GUIclasses.py. If you tried to use create wx classes directly
            in your toplevel file it would fail unless you did an "import wx"
            first.

            Now, as a slight consequence of the specific way you imported, it may
            appear to do something different - when GUIClasses.py imported wx, an
            entry in the modules namespace was created with the value "wx", and
            the value being the wx module. When you do "from GUIClasses import *",
            that imports all the symbols in GUIClasses namespace into your local
            namespace, including the "wx" entry. This kind of cascading behavior
            is one reason that "from foo import *" is frowned up, because the
            namespace pollution can cause confusing bugs.
            [color=blue]
            > Thanks. :).
            >
            > --
            > http://mail.python.org/mailman/listinfo/python-list
            >[/color]

            Comment

            • Chris Mellon

              #7
              Re: Confused about namespaces

              On 18 Nov 2005 15:29:43 -0800, KvS <keesvanschaik@ gmail.com> wrote:[color=blue]
              > Ok, makes sense but didn't seem "natural" to me, 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]

              it worked because all the code that used stuff from the wx namespace
              was in GUIclasses.py. If you tried to use create wx classes directly
              in your toplevel file it would fail unless you did an "import wx"
              first.

              Now, as a slight consequence of the specific way you imported, it may
              appear to do something different - when GUIClasses.py imported wx, an
              entry in the modules namespace was created with the value "wx", and
              the value being the wx module. When you do "from GUIClasses import *",
              that imports all the symbols in GUIClasses namespace into your local
              namespace, including the "wx" entry. This kind of cascading behavior
              is one reason that "from foo import *" is frowned up, because the
              namespace pollution can cause confusing bugs.
              [color=blue]
              > Thanks. :).
              >
              > --
              > http://mail.python.org/mailman/listinfo/python-list
              >[/color]

              Comment

              • KvS

                #8
                Re: Confused about namespaces

                Hmm. But actually I was doing this import from GUIclasses with exactly
                this in mind, namely that it would make wx also available at top level.
                I (in my naive understanding) see this as "natural" and actually
                desirable, how could this cause confusing bugs? Do you mean multiple
                "from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
                and foo1.attr both would become just attr and therefore ambiguous at
                top level?

                If you import foo in two different modules, does the interpreter then
                create one instance of foo and create a reference in both modules to
                the same foo rather than creating two different instances of foo?

                Comment

                • KvS

                  #9
                  Re: Confused about namespaces

                  Hmm. But actually I was doing this import from GUIclasses with exactly
                  this in mind, namely that it would make wx also available at top level.
                  I (in my naive understanding) see this as "natural" and actually
                  desirable, how could this cause confusing bugs? Do you mean multiple
                  "from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
                  and foo1.attr both would become just attr and therefore ambiguous at
                  top level?

                  If you import foo in two different modules, does the interpreter then
                  create one instance of foo and create a reference in both modules to
                  the same foo rather than creating two different instances of foo?

                  Comment

                  • Chris Mellon

                    #10
                    Re: Confused about namespaces

                    On 18 Nov 2005 16:09:44 -0800, KvS <keesvanschaik@ gmail.com> wrote:[color=blue]
                    > Hmm. But actually I was doing this import from GUIclasses with exactly
                    > this in mind, namely that it would make wx also available at top level.[/color]

                    There's no reason not to just "import wx" if you want that.
                    [color=blue]
                    > I (in my naive understanding) see this as "natural" and actually
                    > desirable, how could this cause confusing bugs? Do you mean multiple
                    > "from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
                    > and foo1.attr both would become just attr and therefore ambiguous at
                    > top level?[/color]

                    the second import will overwrite the first, making the first inaccessible
                    [color=blue]
                    >
                    > If you import foo in two different modules, does the interpreter then
                    > create one instance of foo and create a reference in both modules to
                    > the same foo rather than creating two different instances of foo?
                    >[/color]

                    No. It creates the foos within each module, but which foo you have
                    access to in the importing module is determined by the order of
                    import.
                    [color=blue]
                    > --
                    > http://mail.python.org/mailman/listinfo/python-list
                    >[/color]

                    Comment

                    • Chris Mellon

                      #11
                      Re: Confused about namespaces

                      On 18 Nov 2005 16:09:44 -0800, KvS <keesvanschaik@ gmail.com> wrote:[color=blue]
                      > Hmm. But actually I was doing this import from GUIclasses with exactly
                      > this in mind, namely that it would make wx also available at top level.[/color]

                      There's no reason not to just "import wx" if you want that.
                      [color=blue]
                      > I (in my naive understanding) see this as "natural" and actually
                      > desirable, how could this cause confusing bugs? Do you mean multiple
                      > "from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
                      > and foo1.attr both would become just attr and therefore ambiguous at
                      > top level?[/color]

                      the second import will overwrite the first, making the first inaccessible
                      [color=blue]
                      >
                      > If you import foo in two different modules, does the interpreter then
                      > create one instance of foo and create a reference in both modules to
                      > the same foo rather than creating two different instances of foo?
                      >[/color]

                      No. It creates the foos within each module, but which foo you have
                      access to in the importing module is determined by the order of
                      import.
                      [color=blue]
                      > --
                      > http://mail.python.org/mailman/listinfo/python-list
                      >[/color]

                      Comment

                      • KvS

                        #12
                        Re: Confused about namespaces

                        > There's no reason not to just "import wx" if you want that.

                        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
                        that you'd have to check each module for imports outside this tree.

                        If you would use "from ... import *" (except at top level) you have to
                        be aware of overriding, but you can also use this to your advantage and
                        any foo1.attr reference would just work right, without further ado...
                        Or would in such a case a class hierarchy be the thing to use?
                        [color=blue]
                        > No. It creates the foos within each module, but which foo you have
                        > access to in the importing module is determined by the order of
                        > import.[/color]

                        Am I understanding correctly that if you have a module foo importing wx
                        and a module main importing both foo and wx there are actually two
                        instances of wx created, one referenced to by (at top level) foo.wx.*
                        and one wx.*? If this is indeed the case it isn't too good for the
                        performance doing this importing of wx multiple times right?

                        - Kees

                        Comment

                        • KvS

                          #13
                          Re: Confused about namespaces

                          > There's no reason not to just "import wx" if you want that.

                          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
                          that you'd have to check each module for imports outside this tree.

                          If you would use "from ... import *" (except at top level) you have to
                          be aware of overriding, but you can also use this to your advantage and
                          any foo1.attr reference would just work right, without further ado...
                          Or would in such a case a class hierarchy be the thing to use?
                          [color=blue]
                          > No. It creates the foos within each module, but which foo you have
                          > access to in the importing module is determined by the order of
                          > import.[/color]

                          Am I understanding correctly that if you have a module foo importing wx
                          and a module main importing both foo and wx there are actually two
                          instances of wx created, one referenced to by (at top level) foo.wx.*
                          and one wx.*? If this is indeed the case it isn't too good for the
                          performance doing this importing of wx multiple times right?

                          - Kees

                          Comment

                          • Diez B. Roggisch

                            #14
                            Re: Confused about namespaces

                            > Am I understanding correctly that if you have a module foo importing wx[color=blue]
                            > and a module main importing both foo and wx there are actually two
                            > instances of wx created, one referenced to by (at top level) foo.wx.*
                            > and one wx.*? If this is indeed the case it isn't too good for the
                            > performance doing this importing of wx multiple times right?
                            >[/color]

                            No, they aren't two instances. The first import will evaluate the
                            wx-module (there could be code in there executed upon importing it). All
                            subsequent imports of that module will only bind the already imported
                            module to the name.

                            So - the way to have one module be available in several other modules is
                            to import them in each of them - causing actually close to none
                            overhead, as the import statement is evaluated only once per file
                            (unless you put it in a loop or something, but this still only would
                            return the same reference.)

                            Regards,

                            Diez

                            Comment

                            • Diez B. Roggisch

                              #15
                              Re: Confused about namespaces

                              > Am I understanding correctly that if you have a module foo importing wx[color=blue]
                              > and a module main importing both foo and wx there are actually two
                              > instances of wx created, one referenced to by (at top level) foo.wx.*
                              > and one wx.*? If this is indeed the case it isn't too good for the
                              > performance doing this importing of wx multiple times right?
                              >[/color]

                              No, they aren't two instances. The first import will evaluate the
                              wx-module (there could be code in there executed upon importing it). All
                              subsequent imports of that module will only bind the already imported
                              module to the name.

                              So - the way to have one module be available in several other modules is
                              to import them in each of them - causing actually close to none
                              overhead, as the import statement is evaluated only once per file
                              (unless you put it in a loop or something, but this still only would
                              return the same reference.)

                              Regards,

                              Diez

                              Comment

                              Working...