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


Groups > comp.lang.python > #21828

Re: Using non-dict namespaces in functions

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ericsnowcurrently@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(of': 0.05; 'cpython': 0.05; 'python': 0.08; 'dict': 0.09; 'globals': 0.09; 'none)': 0.09; 'am,': 0.12; 'argument': 0.15; 'lambda': 0.16; 'object)': 0.16; 'proceed?': 0.16; 'subject:non': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'ticket.': 0.18; '(most': 0.21; 'cc:no real name:2**0': 0.21; 'received:209.85.210.174': 0.21; 'received:mail- iy0-f174.google.com': 0.21; 'tracker': 0.21; 'header:In-Reply- To:1': 0.22; 'though.': 0.23; 'traceback': 0.24; 'sat,': 0.25; 'cc:2**0': 0.26; 'up.': 0.26; 'function': 0.27; 'looks': 0.27; 'message-id:@mail.gmail.com': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.29; 'typeerror:': 0.30; 'least': 0.30; 'quite': 0.31; 'cases': 0.32; 'done,': 0.32; "i've": 0.32; 'does': 0.32; 'implement': 0.32; 'there': 0.33; "can't": 0.33; 'instead': 0.33; 'updated': 0.33; 'checking': 0.34; 'steven': 0.34; 'someone': 0.34; '17,': 0.34; 'last):': 0.34; 'received:google.com': 0.37; 'received:209.85': 0.38; 'uses': 0.38; 'purposes': 0.38; 'could': 0.38; 'couple': 0.38; 'unless': 0.39; 'suggestions': 0.39; 'received:209': 0.39; 'change': 0.40; 'might': 0.40; 'type': 0.61; 'more': 0.61; 'bothered': 0.68; 'care': 0.71; 'spot': 0.79; 'dict,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=ZoE3+7KsBzQgs5pJGXiKIl39OyUw0gYKA/6NLDzfAcc=; b=oAd0rbfIUgLHuNVM8pUzmhU02MLZtFULeyS/RqomjT0oQurI9eIpKgI2dR95ba/LRi t5dWt0xnz4Jnz0BYUaMwbguN1wZTGZwhTSnsEXWkeGb7xG2XBr86ciO6GYi9h4oFn8Hj NcJBUFiIsucBHHMfFtfxPwXVbgWN16PNa/Cqw+xr/hf+sq3zzSJxjgD1o4P4g0rj2oT0 hCo4bVGoXuZtFQE4sCNojG1B/iWj/aZ5l01GPL+FPeS+1OnxZYB7VnWCi5hK9XclDklT 4VMu8ANF3QZ5jAt39FaDqo/UvQAvxCG76otkg5PziEj/X1Ac+X4/gER27GnWFqSB3tJD B9Hw==
MIME-Version 1.0
In-Reply-To <4f647317$0$29981$c3e8da3$5496439d@news.astraweb.com>
References <4f647317$0$29981$c3e8da3$5496439d@news.astraweb.com>
Date Sat, 17 Mar 2012 11:42:49 -0700
Subject Re: Using non-dict namespaces in functions
From Eric Snow <ericsnowcurrently@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.764.1332009772.3037.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332009772 news.xs4all.nl 6926 [2001:888:2000:d::a6]:53950
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21828

Show key headers only | View raw


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?

> Now I try to create a function that uses a MockChainMap instead of a dict
> for its globals:
>
> function = type(lambda: None)
> f = lambda x: (a+b+x)
> g = function(f.__code__, MockChainMap(), 'g')
>
> And that's where I get into trouble:
>
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: function() argument 2 must be dict, not MockChainMap
>
>
> How do I build a function with globals set to a non-dict mapping?
>
> If this can't be done, any suggestions for how I might proceed?

This looks like one of those cases where there is strict type checking
(of a Python object) at the C level.  You may consider bringing this
up in a tracker ticket.  Unless there are performance implications,
it's likely a case of no one having bothered to change this spot to be
more duck-type friendly.  There are quite a few of those in CPython
and I've seen at least a couple updated when someone brought it up.

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.

-eric

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


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