[Python-Dev] Impact of Namedtuple on startup time

Ethan Furman ethan at stoneleaf.us
Mon Jul 17 19:17:57 EDT 2017


On 07/17/2017 03:27 PM, Greg Ewing wrote:
> Barry Warsaw wrote:

>> namedtuple is great and clever, but it’s also a bit clunky.  It has a weird
>> signature and requires a made up type name.
>
> Maybe a metaclass could be used to make something
> like this possible:
>
>
>     class Foo(NamedTuple, fields = 'x,y,z'):
>        ...
>
> Then the name is explicit and you get to add methods
> etc. if you want.

 From the NamedTuple tests from my aenum library [1]:

     LifeForm = NamedTuple('LifeForm', 'branch genus species', module=__name__)

     class DeathForm(NamedTuple):
         color = 0
         rigidity = 1
         odor = 2

     class WhatsIt(NamedTuple):
         def what(self):
             return self[0]
     class ThatsIt(WhatsIt):
         blah = 0
         bleh = 1

     class Character(NamedTuple):
         # second argument is doc string
         name = 0
         gender = 1, None, 'male'
         klass = 2, None, 'fighter'

     class Point(NamedTuple):
         x = 0, 'horizondal coordinate', 0
         y = 1, 'vertical coordinate', 0

     class Point(NamedTuple):
         x = 0, 'horizontal coordinate', 1
         y = 1, 'vertical coordinate', -1
     class Color(NamedTuple):
         r = 0, 'red component', 11
         g = 1, 'green component', 29
         b = 2, 'blue component', 37

     Pixel1 = NamedTuple('Pixel', Point+Color, module=__name__)
     class Pixel2(Point, Color):
         "a colored dot"
     class Pixel3(Point):
         r = 2, 'red component', 11
         g = 3, 'green component', 29
         b = 4, 'blue component', 37

--
~Ethan~

[1] https://pypi.python.org/pypi/aenum


More information about the Python-Dev mailing list