[Python-Dev] Non-string keys in type dict

Chris Kaynor ckaynor at zindagigames.com
Thu Mar 8 02:49:55 CET 2012


On Wed, Mar 7, 2012 at 5:45 PM, Victor Stinner <victor.stinner at gmail.com>wrote:

> > During the Language Summit 2011 (*), it was discussed that PyPy and
> > Jython don't support non-string key in type dict. An issue was open to
> > emit a warning on such dict, but the patch has not been commited yet.
>
> It's the issue #11455. As written in the issue, there are two ways to
> create such type:
>
> class A(object):
>    locals()[42] = "abc"
>
> or
>
> type("A", (object,), {42: "abc"})
>
> Both look like an ugly hack.
>

Here is a cleaner version, using metaclasses (Python 2.6):

class M(type):
    def __new__(mcs, name, bases, dict):
            dict[42] = 'abc'
            return super(M, mcs).__new__(mcs, name, bases, dict)

class A(object):
    __metaclass__ = M


>
> Victor
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/ckaynor%40zindagigames.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120307/f7616c6e/attachment.html>


More information about the Python-Dev mailing list