Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:module': 0.04; '*not*': 0.05; 'python': 0.08; '__name__': 0.09; 'globals': 0.09; 'module?': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; "'__main__':": 0.16; '*args):': 0.16; 'a!"': 0.16; 'b!"': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; '>>>': 0.18; '2.x': 0.18; 'functions,': 0.18; 'bonus': 0.21; 'so.': 0.22; 'from:addr:web.de': 0.23; 'classes': 0.26; 'code': 0.26; 'fine.': 0.29; 'class': 0.29; 'key,': 0.30; 'value)': 0.30; 'subject:?': 0.31; 'objects': 0.32; 'header:X -Complaints-To:1': 0.34; 'to:addr:python-list': 0.35; 'received:org': 0.36; 'but': 0.37; 'skip:_ 10': 0.38; 'think': 0.38; 'should': 0.38; 'unless': 0.39; 'speaking': 0.39; 'to:addr:python.org': 0.40; 'of...': 0.84; 'otten': 0.84; 'self:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: multiple namespaces within a single module? Date: Thu, 09 Feb 2012 22:53:16 +0100 Organization: None References: <6f50fa3d-74e6-4682-b5d8-2634d418dd23@d15g2000yqg.googlegroups.com> <4F3436CA.2020800@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084adf6.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328824410 news.xs4all.nl 6896 [2001:888:2000:d::a6]:51737 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20114 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... > > > 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()) > > > 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()