Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19693 > unrolled thread

Re: 'class' named tuple

Started byArnaud Delobelle <arnodel@gmail.com>
First post2012-02-01 07:17 +0000
Last post2012-02-01 07:17 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: 'class' named tuple Arnaud Delobelle <arnodel@gmail.com> - 2012-02-01 07:17 +0000

#19693 — Re: 'class' named tuple

FromArnaud Delobelle <arnodel@gmail.com>
Date2012-02-01 07:17 +0000
SubjectRe: 'class' named tuple
Message-ID<mailman.5289.1328080633.27778.python-list@python.org>
On 1 February 2012 00:54, Emmanuel Mayssat <emayssat@gmail.com> wrote:
> I have the following program.
> I am trying to have index the attributes of an object using __getitem__.
> Reading them this way works great, but assigning them a value doesn't
> Is there a way to do such a thing?
> (Almost like a named tuple, but with custom methods)
>
> class LIter(object):
>    def __init__(self,parent=None):
>        super(LIter, self).__init__()
>        self.toto = 3
>        self.tata = 'terto'
>

Add
    _attrs = 'toto', 'tata'
    def __getitem__(self, index):
        return getattr(self, _attrs[index])
    def __setitem__(self, index, value)
        setattr(self, _attrs[index], value)

-- 
Arnaud

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web