[Python-ideas] Add recordlcass to collections module

Zaur Shibzukhov szport at gmail.com
Mon Sep 3 14:17:13 EDT 2018



понедельник, 3 сентября 2018 г., 2:11:06 UTC+3 пользователь Greg Ewing 
написал:
>
> Zaur Shibzukhov wrote: 
>
>  > `Recordclass` is defined on top of` memoryslots` just like `namedtuple` 
>  > above` tuple`. Attributes are accessed via a descriptor (`itemgetset`), 
>  > which supports both` __get__` and `__set__` by the element index. 
>  > 
>  > As a result, `recordclass` takes up as much memory as` namedtuple`, it 
>  > supports quick access by `__getitem__` /` __setitem__` and by attribute 
>  > name via the protocol of the descriptors. 
>
> I'm not sure why you need a new C-level type for this. Couldn't you 
> get the same effect just by using __slots__? 
>
> e.g. 
>
> class C: 
>
>       __slots__ = ('attr_1', ..., 'attr_m') 
>
>       def __new __ (cls, attr_1, ..., attr_m): 
>           self.attr_1 = attr_1 
>           ... 
>           self.attt_m = attr_m 
>
> Yes, you can. The only difference is that access by index to fields are 
slow. So if you don't need fast access by index but only by name then using 
__slots__ is enough. Recordclass is actually a fixed array with named 
access to the elements in the same manner as namedtuple is a actually a 
tuple with named access to it's elements. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180903/1f3d7901/attachment.html>


More information about the Python-ideas mailing list