Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!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; 'emulate': 0.07; 'list?': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'integers': 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; 'properly': 0.15; "(i'm": 0.16; 'foo()': 0.16; 'iterator': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'define': 0.20; 'raise': 0.24; 'pass': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'subject:list': 0.28; 'container': 0.29; 'class': 0.29; 'print': 0.32; 'to:addr :python-list': 0.33; 'next': 0.35; 'charset:us-ascii': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'supports': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'received:69.56': 0.65; 'received:69.56.148': 0.84; 'received:gateway14.websitewelcome.com': 0.91 Date: Mon, 17 Dec 2012 15:30:27 -0800 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: python-list@python.org Subject: Re: Supporting list() References: In-Reply-To: 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]:57311 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 3 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355758228 news.xs4all.nl 6917 [2001:888:2000:d::a6]:50193 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34979 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.) You can either use __iter__ and next to conform to the iterator protocol, or you can define __getitem__. If using __getitem__ it needs to work with integers from 0 to len(f)-1, and raise IndexError for len(f), len(f+1), etc. ~Ethan~