Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] namedtuple implementation grumble

dw+python-dev at hmmz.orgdw+python-dev at hmmz.org
Sun Jun 8 21:37:46 CEST 2014


On Sun, Jun 08, 2014 at 03:13:55PM -0400, Eric V. Smith wrote:> > The current implementation is also *really* easy to understand,> > while writing out the dynamic type creation explicitly would likely> > require much deeper knowledge of the type machinery to follow.> As proof that it's harder to understand, here's an example of that> dynamically creating functions and types:Probably I'm missing something, but there's a much simpler non-execapproach, something like:    class _NamedTuple(...):        ...    def namedtuple(name, fields):        cls = tuple(name, (_NamedTuple,), {            '_fields': fields.split()        })        for i, field_name in enumerate(cls._fields):            prop = property(functools.partial(_NamedTuple.__getitem__, i)                            functools.partial(_NamedTuple.__setitem__, i))            setattr(cls, field_name, prop)        return clsDavid


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2025 Movatter.jp