Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'function,': 0.07; 'dict': 0.09; 'globals': 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; 'am,': 0.12; 'def': 0.13; 'f(a,': 0.16; 'globals.': 0.16; 'lookup': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subclassing': 0.16; 'subject:non': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'help.': 0.19; 'trying': 0.21; 'from:addr:web.de': 0.23; 'though.': 0.23; 'sat,': 0.25; 'code': 0.26; 'function': 0.27; 'work,': 0.28; 'class': 0.29; 'does': 0.32; 'implement': 0.32; "won't": 0.33; 'header :User-Agent:1': 0.33; 'instead': 0.33; 'header:X-Complaints-To:1': 0.34; 'eric': 0.34; 'steven': 0.34; '17,': 0.34; 'to:addr:python- list': 0.35; 'something': 0.35; 'received:org': 0.36; 'skip:_ 10': 0.38; 'purposes': 0.38; 'could': 0.38; 'some': 0.38; 'think': 0.38; 'change': 0.40; 'to:addr:python.org': 0.40; 'back': 0.60; 'custom': 0.61; 'care': 0.71; '__call__': 0.84; 'snow': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Using non-dict namespaces in functions Date: Sun, 18 Mar 2012 08:50:46 +0100 Organization: None References: <4f647317$0$29981$c3e8da3$5496439d@news.astraweb.com> <4f653fd9$0$29981$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50848f73.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332057058 news.xs4all.nl 6950 [2001:888:2000:d::a6]:53004 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21851 Steven D'Aprano wrote: > On Sat, 17 Mar 2012 11:42:49 -0700, Eric Snow wrote: > >> On Sat, Mar 17, 2012 at 4:18 AM, Steven D'Aprano >> wrote: >>> Note that it is important for my purposes that MockChainMap does not >>> inherit from dict. >> >> Care to elaborate? > > I want to use collections.ChainMap, or something very like it, and I > don't want to be forced into an unnatural is-a relationship with dict if > I don't have to. > > > [...] >> Regardless, you could also implement __call__() on a function look-alike >> class to get what you're after. It may not be as performant though. > > I don't think that can work, because __call__ itself is a function, and I > would need to change *its* globals. Which brings me back exactly where I > started, trying to change globals in a function to a non-dict. The key lookup code in ceval.c is inlined, so even subclassing dict and overriding __getitem__() won't help. Instead of def f(a): return a + b # b taken from some custom namespace you have to resort to the conventional approach def f(a, ns=magic()): return a + ns["b"] or def f(self, a): return a + self.b