Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!feeder3.eweka.nl!81.171.88.15.MISMATCH!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'attributes': 0.05; 'def': 0.13; 'received:209.85.214.174': 0.13; "subject:' ": 0.15; 'cc:addr:python-list': 0.15; 'subject:named': 0.16; 'thing?': 0.16; 'tuple,': 0.16; 'wrote:': 0.16; 'trying': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'assigning': 0.23; 'index': 0.24; 'cc:2**0': 0.25; 'skip:_ 20': 0.26; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'class': 0.29; 'value)': 0.30; 'received:209.85.214': 0.32; 'named': 0.33; 'object': 0.33; 'there': 0.33; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'skip:_ 10': 0.37; 'received:209.85': 0.38; 'received:209': 0.39; 'custom': 0.61; 'skip:\xc2 10': 0.74; 'emmanuel': 0.84; 'methods)': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=keyqoGUb5FZn44tWKSH9AVRsA5RshcTGOskatvyXrNA=; b=GT2nYVtgl6TkeAaGOAq+dlHvHbqmt28lNToQ9eqWJfXAn6CZ7pn1t+OIRap+kCHBRs ShdFezAFHV3XZRh4SoRquJA62koxFCeAKH5l06MsBQdjKlBimmACCI54N7wp1Mgae9vR b8NdQ+Wbo60YZooN4bKu0Ph2xgOUWiUN4Se5A= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 1 Feb 2012 07:17:10 +0000 Subject: Re: 'class' named tuple From: Arnaud Delobelle To: Emmanuel Mayssat Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328080633 news.xs4all.nl 6860 [2001:888:2000:d::a6]:56715 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19693 On 1 February 2012 00:54, Emmanuel Mayssat 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): > =C2=A0 =C2=A0def __init__(self,parent=3DNone): > =C2=A0 =C2=A0 =C2=A0 =C2=A0super(LIter, self).__init__() > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.toto =3D 3 > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.tata =3D 'terto' > Add _attrs =3D 'toto', 'tata' def __getitem__(self, index): return getattr(self, _attrs[index]) def __setitem__(self, index, value) setattr(self, _attrs[index], value) --=20 Arnaud