Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'method.': 0.05; 'needed,': 0.05; 'class,': 0.07; 'emulate': 0.07; 'list?': 0.07; 'method,': 0.07; 'python': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'subject:()': 0.09; '~ethan~': 0.09; 'cc:addr:python-list': 0.10; 'properly': 0.15; "(i'm": 0.16; 'foo()': 0.16; 'iterator': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'define': 0.20; 'object.': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'subject:list': 0.28; '(possibly': 0.29; 'container': 0.29; 'class': 0.29; 'print': 0.32; 'protocol': 0.35; 'method': 0.36; 'charset:us-ascii': 0.36; 'does': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'supports': 0.38; 'header:Received:5': 0.40; 'received:69.56': 0.65; 'believe': 0.69; 'common,': 0.84; 'received:69.56.148': 0.84; 'received:gateway14.websitewelcome.com': 0.91; 'angel': 0.93 Date: Mon, 17 Dec 2012 15:27:47 -0800 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: d@davea.name Subject: Re: Supporting list() References: <50CF3253.5020607@davea.name> In-Reply-To: <50CF3253.5020607@davea.name> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: yes X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.11.105]) [173.12.184.238]:57301 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355759624 news.xs4all.nl 6864 [2001:888:2000:d::a6]:36666 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34984 Dave Angel wrote: > On 12/17/2012 09:33 AM, Skip Montanaro wrote: >> What method(s) does a class have to support to properly emulate a container >> which supports turning it into a list? For example: >> >> class Foo: >> pass >> >> f = Foo() >> print list(f) >> >> Is it just __iter__() and next()? (I'm still using 2.4 and 2.7.) > > I believe the container class needs to define the __iter__() method, > which has to return an iterator object. > > That (possibly different) iterator class needs both an __iter__() method > and a next() method. > > If the container class is also the iterator class, which is common, then > you just need one __iter__() method, which returns self. The `next()` method is also needed, as `__iter__()` and `next()` are the two methods that make up the iterator protocol (`__next__` in python 3k). ~Ethan~