[Python-Dev] namedtuple implementation grumble (original) (raw)
Eric V. Smith eric at trueblade.com
Sun Jun 8 23:27:41 CEST 2014
- Previous message: [Python-Dev] namedtuple implementation grumble
- Next message: [Python-Dev] namedtuple implementation grumble
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 6/8/2014 3:37 PM, dw+python-dev at hmmz.org wrote:
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-exec approach, something like: class NamedTuple(...): ... def namedtuple(name, fields): cls = tuple(name, (NamedTuple,), { 'fields': fields.split() }) for i, fieldname in enumerate(cls.fields): prop = property(functools.partial(NamedTuple.getitem, i) functools.partial(NamedTuple.setitem, i)) setattr(cls, fieldname, prop) return cls
How would you write _Namedtuple.new?
- Previous message: [Python-Dev] namedtuple implementation grumble
- Next message: [Python-Dev] namedtuple implementation grumble
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]