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


Groups > comp.lang.python > #19671

'class' named tuple

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!feed.xsnews.nl!border-3.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <emayssat@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attributes': 0.05; 'python': 0.08; '__name__': 0.09; '25,': 0.12; 'def': 0.13; "subject:' ": 0.15; '"__main__":': 0.16; 'bye': 0.16; 'subject:named': 0.16; 'thing?': 0.16; 'tuple,': 0.16; 'trying': 0.20; '(most': 0.21; "doesn't": 0.22; 'assigning': 0.23; 'received:74.125.82.174': 0.24; 'index': 0.24; 'traceback': 0.24; 'skip:_ 20': 0.26; 'message-id:@mail.gmail.com': 0.28; 'print': 0.29; 'class': 0.29; 'typeerror:': 0.30; 'does': 0.32; 'named': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'there': 0.33; 'assignment': 0.34; 'last):': 0.34; 'received:74.125.82': 0.34; 'file': 0.35; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'to:addr:python.org': 0.40; 'custom': 0.61; 'methods)': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; bh=O21HA+mz1Mq2o+zz/1Fw4C8pLgO5v4LmTFNbo3g4mUU=; b=ZZirpHQPvPzVrcQDmvtZq6rjMQ4As0fjHXDU8uAQD/RiwSFjRbpcyN6a/joMbObLxB 1gd9+aSnEUhjontdGupbLW6Z34nOke47mtFxy9K5VBiZ1+IcoONPGH59zJLT6XdiuU1L rrAWDDmRJj/Henz0p6ruWsLZZ5jOPCtWOPxx8=
MIME-Version 1.0
From Emmanuel Mayssat <emayssat@gmail.com>
Date Tue, 31 Jan 2012 16:54:14 -0800
Subject 'class' named tuple
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5273.1328058001.27778.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328058001 news.xs4all.nl 6851 [2001:888:2000:d::a6]:42540
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19671

Show key headers only | View raw


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'

    def __getitem__(self,index):
        if index == 0 : return self.toto
        if index == 1 : return self.tata

   # [other methods]



if __name__ == "__main__":
    i = LIter()
    print i[0]
    print i[1]
    i.toto = 2
    i.tata = 'bye'
    print i[0]
    print i[1]
    i[0] = -1
    i[1] = 'salut'
    print i[0]
    print i[1]





$ python iter.py
3
terto
2
bye
Traceback (most recent call last):
  File "iter.py", line 25, in <module>
    i[0] = -1
TypeError: 'LIter' object does not support item assignment

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

'class' named tuple Emmanuel Mayssat <emayssat@gmail.com> - 2012-01-31 16:54 -0800

csiph-web