понедельник, 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.
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/