Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'returned.': 0.07; '>>>>': 0.09; 'callable': 0.09; 'instance.': 0.09; 'def': 0.13; '3:51': 0.16; 'iterable,': 0.16; 'subject:classes': 0.16; 'cc:addr:python- list': 0.16; 'mon,': 0.16; 'wrote:': 0.18; 'jan': 0.19; '(which': 0.19; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'result.': 0.21; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; 'cc:2**0': 0.24; 'received:74.125.82.174': 0.24; 'code,': 0.27; 'message- id:@mail.gmail.com': 0.28; 'yield': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'class': 0.29; 'iterating': 0.30; 'key,': 0.30; 'skip:% 10': 0.30; 'value.': 0.32; 'actually': 0.33; 'received:74.125.82': 0.35; 'received:74.125': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'using': 0.38; 'fail': 0.39; 'returned': 0.39; 'more': 0.61; 'your': 0.61; 'here.': 0.66; '2012': 0.67 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=gNSj4cD7Wz1nQJ6JqNTNA7vXfgmOSWGwGX3pMwktktQ=; b=nZ2+sR8YG/MpY7BO/yXiso0HHMVzjIg4jkYMuxI+Ik09tjsL9s1RYCfBEGTc7RxnfC r5Pe2V773NARcEMp0qOx04ENsP7+RQKM2XuwoqEVt9SUwho/a0t/nd9l3kXEZXwRp/DJ Bb3tpQxanSUI8WN+/yaVkpzTvz5ExMa+zm5Io= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Mon, 9 Jan 2012 16:20:32 -0700 Subject: Re: classes and __iter__ To: "david.garvey@gmail.com" Content-Type: text/plain; charset=ISO-8859-1 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326151265 news.xs4all.nl 6916 [2001:888:2000:d::a6]:48081 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18717 On Mon, Jan 9, 2012 at 3:51 PM, david.garvey@gmail.com wrote: > ...=A0=A0=A0=A0 def __iter__(self): > ...=A0=A0=A0=A0=A0=A0=A0=A0 return iter(self.keylist, self.d) This method is incorrect. The 2-argument form of iter() is very different from the 1-argument form. Whereas the 1-argument form takes an iterable, the 2-argument form takes a callable and a sentinel value. In that case, iter() attempts to call the callable until the sentinel is returned. In your code, it would attempt to call the keylist (which would fail with a TypeError) until it returned the dictionary as a result. This would be more in line with what you're trying to do: def __iter__(self): for key in self.keylist: yield key, self.d[key] >>>> for key in helo.keylist: > ...=A0=A0=A0=A0 print "Key:%s Value:%s" %(key,helo.d[key]) Note that this works because you're not actually using the class's __iter__ method here. You're iterating over the keylist directly, not over the containing class instance.