[Python-ideas] Add recordlcass to collections module

Zaur Shibzukhov szport at gmail.com
Sun Sep 2 14:24:19 EDT 2018


As the author of `recordclass` I would like to shed some light...

Recorclass originated as a response to the 
[question](https://stackoverflow.com/questions/29290359/existence-of-mutable-named-tuple-in-python/29419745#29419745) 
on stackoverflow.

`Recordclass` was conceived and implemented as a type that, by api, memory 
and speed, would be completely identical to` namedtuple`, except that it 
would support an assignment in which any element could be replaced without 
creating a new instance, as in ` namedtuple`. Those. would be almost 
identical to `namedtuple` and support the assignment (` __setitem__` / 
`setslice__`).

The effectiveness of namedtuple is based on the effectiveness of the 
`tuple` type in python. In order to achieve the same efficiency it was 
necessary to create a type `memoryslots`. Its structure 
(`PyMemorySlotsObject`) is identical to the structure of` tuple` 
(`PyTupleObject`) and therefore takes up the same amount of memory as` 
tuple`.

`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.

The class generated by `recordclass` is:

`` `
from recordclass import memoryslots, itemgetset

class C (memoryslots):
    __slots__ = ()
    
    _fields = ('attr_1', ..., 'attr_m')
    
    attr_1 = itemgetset (0)
    ...
    attr_m = itemgetset (m-1)
    
    def __new __ (cls, attr_1, ..., attr_m):
        'Create new instance of {typename} ({arg_list})'
        return memoryslots .__ new __ (cls, attr_1, ..., attr_m)
`` `
etc. following the `namedtuple` definition scheme.

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.

Regards,

Zaur

суббота, 1 сентября 2018 г., 10:48:07 UTC+3 пользователь Martin Bammer 
написал:
>
> Hi, 
>
> what about adding recordclass 
> (https://bitbucket.org/intellimath/recordclass) to the collections module 
>
> It is like namedtuple, but elements are writable and it is written in C 
> and thus much faster. 
>
> And for convenience it could be named as namedlist. 
>
> Regards, 
>
> Martin 
>
>
> _______________________________________________ 
> Python-ideas mailing list 
> Python... at python.org <javascript:> 
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180902/4884ad28/attachment.html>


More information about the Python-ideas mailing list