On Tue, Dec 31, 2013 at 7:26 PM, Steven D'Aprano <st...@pearwood.info> wrote:
>
> from collections import namedtuple
>
> class Source(namedtuple("Source", "string i n")):
>     def __new__(cls, string, i=0, n=None):
>         if n is None:
>             n = len(string)
>         return super(Source, cls).__new__(cls, string, i, n)

namedtuple is a factory to create a tuple subclass that has properties
that use operator.itemgetter, a __dict__ property that returns a
collections.OrderedDict, plus the convenience functions _make and
_replace.

It also sets __slots__ = () to prevent instances from getting a dict.
If you subclass a 2nd time, remember to also set __slots__ = (). But
this only matters if you need better performance and smaller memory
footprint when creating thousands of instances.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to