Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21851
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Using non-dict namespaces in functions |
| Date | 2012-03-18 08:50 +0100 |
| Organization | None |
| References | <4f647317$0$29981$c3e8da3$5496439d@news.astraweb.com> <mailman.764.1332009772.3037.python-list@python.org> <4f653fd9$0$29981$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.776.1332057058.3037.python-list@python.org> (permalink) |
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
>> <steve+comp.lang.python@pearwood.info> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Using non-dict namespaces in functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-17 11:18 +0000
Re: Using non-dict namespaces in functions Eric Snow <ericsnowcurrently@gmail.com> - 2012-03-17 11:42 -0700
Re: Using non-dict namespaces in functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-18 01:52 +0000
Re: Using non-dict namespaces in functions Peter Otten <__peter__@web.de> - 2012-03-18 08:50 +0100
csiph-web