Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20114

Re: multiple namespaces within a single module?

From Peter Otten <__peter__@web.de>
Subject Re: multiple namespaces within a single module?
Date 2012-02-09 22:53 +0100
Organization None
References <6f50fa3d-74e6-4682-b5d8-2634d418dd23@d15g2000yqg.googlegroups.com> <jh1725$slv$1@dough.gmane.org> <4F3436CA.2020800@stoneleaf.us>
Newsgroups comp.lang.python
Message-ID <mailman.5616.1328824410.27778.python-list@python.org> (permalink)

Show all headers | View raw


Ethan Furman wrote:

> Peter Otten wrote:
>> jkn wrote:
>> 
>>>     is it possible to have multiple namespaces within a single python
>>> module?
>> 
>> Unless you are abusing classes I don't think so.
> 
> 
> Speaking of...
> 
> <code>
> class NameSpace(object):
>      def __init__(self, globals):
>          self.globals = globals
>          self.current_keys = list(globals.keys())
>      def __enter__(self):
>          return self
>      def __exit__(self, *args):
>          new_items = []
>          for key, value in self.globals.items():
>              if key not in self.current_keys and value is not self:
>                  new_items.append((key, value))
>          for key, value in new_items:
>              setattr(self, key, value)
>              del self.globals[key]
> 
> if __name__ == '__main__':
>      with NameSpace(globals()) as a:
>          def function():
>              print('inside a!')
>      with NameSpace(globals()) as b:
>          def function():
>              print('inside b!')
> 
>      a.function()
>      b.function()
>      print(vars())
> </code>
> 
> The NameSpace objects do *not* get their own copy of globals(), but for
> functions, etc., it should work fine.  As a bonus the above code works
> for both 2.x and 3.x.

Hm, what about

with NameSpace(globals()) as a:
    x = "inside a!"
    def function():
        print(x)
with NameSpace(globals()) as b:
    x = "inside b!"
    def function():
        print(x)

x = "inside main!"
a.function()
b.function()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

multiple namespaces within a single module? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-02-09 10:32 -0800
  Re: multiple namespaces within a single module? Peter Otten <__peter__@web.de> - 2012-02-09 20:33 +0100
    Re: multiple namespaces within a single module? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-02-09 15:24 -0800
      Re: multiple namespaces within a single module? Peter Otten <__peter__@web.de> - 2012-02-10 12:10 +0100
        Re: multiple namespaces within a single module? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-02-10 08:28 -0800
  Re: multiple namespaces within a single module? Ethan Furman <ethan@stoneleaf.us> - 2012-02-09 13:12 -0800
  Re: multiple namespaces within a single module? Peter Otten <__peter__@web.de> - 2012-02-09 22:53 +0100
  Re: multiple namespaces within a single module? Ethan Furman <ethan@stoneleaf.us> - 2012-02-09 14:25 -0800
  Re: multiple namespaces within a single module? Ethan Furman <ethan@stoneleaf.us> - 2012-02-09 16:05 -0800
  Re: multiple namespaces within a single module? Arnaud Delobelle <arnodel@gmail.com> - 2012-02-10 10:41 +0000

csiph-web