Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Help on for loop understanding Date: Tue, 8 Dec 2015 14:23:40 +1100 Lines: 27 Message-ID: References: <5d02c61f-a979-4121-a9ae-764b44903156@googlegroups.com> <4f64bcda-15f2-4c6f-a279-b76330fb7f39@googlegroups.com> <56664210.7040007@lucidity.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de JoGI4mbwfQB5FgeP40SZzAGI/MaoQ0gqUnxHtzss37vw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'method.': 0.05; 'cc:addr:python-list': 0.09; 'get.': 0.09; 'iterate': 0.09; "object's": 0.09; 'subclass': 0.09; 'subject:Help': 0.10; 'def': 0.13; 'do,': 0.15; 'erik': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'generator.': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'defined': 0.23; '(you': 0.23; 'dec': 0.23; 'header:In-Reply- To:1': 0.24; 'sense': 0.26; 'bugs': 0.27; 'message- id:@mail.gmail.com': 0.27; '(such': 0.27; 'yield': 0.27; 'correct': 0.28; 'behaviour': 0.29; 'subject:skip:u 10': 0.29; 'saves': 0.30; 'another': 0.32; 'skip:_ 10': 0.32; 'says': 0.32; 'embedded': 0.32; 'class': 0.33; 'tue,': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'returning': 0.35; 'something': 0.35; 'skip:i 20': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:209': 0.38; 'anything': 0.38; 'someone': 0.38; 'your': 0.60; 'great': 0.63; 'delegate': 0.66; 'inherit': 0.66; 'chrisa': 0.84; "class's": 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=YcDKoq6y5B1NDxv6iZqD60fYriOdOu4Wx2tqZnP+ds4=; b=P2LnHhGpErVw+pQlyj8RG/N53nkxIZEWm/KsCIXYWu6p0sgu+NfJfqf9/MtUH8czPi K6xOuDfMTXzExyc2VJUrs/Fa3IvDJdoSzRSGQIDqdqktwIe7YoIizSPOypgSNfLlcvBi iM/IYfXpf+TqumOKyOMORg7/+qDXgR+TnQz58G3dSGQmSwrYYP6TbwJeHFpXKIZ0ajeU 9t3bNNn6LsI5iUKDk9sWJPCfcqSHKIvbRn6PKNE98QH6Wr2N2oBrGfKDx9cuaKR8VAhq WO3ZDRvrNz8okSh92jMxRMKe54T5s3Ao543A6PtXS852uCOVzADCZ0uxqkoc53mL7NPy DFLg== X-Received: by 10.107.10.233 with SMTP id 102mr1758116iok.31.1449545020746; Mon, 07 Dec 2015 19:23:40 -0800 (PST) In-Reply-To: <56664210.7040007@lucidity.plus.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100130 On Tue, Dec 8, 2015 at 1:36 PM, Erik wrote: > So, you can write your class's iterator to do anything that makes sense when > someone says "for i in myclassinstance:". > > If your class is a subclass of a class ("is-a") that already has a defined > iterator (such as a list or a dict) and the behaviour of that is correct for > you, then you need to do nothing (you inherit that class's __iter__() > method). > > If your class should iterate over an embedded object ("has-a") that already > has a defined iterator, then your __iter__() method can just delegate to > that object's iterator using something like: > > def __iter__(self): > return iter(self.embedded_thing) Another great way to write an __iter__ method is as a generator. def __iter__(self): yield "thing" yield from self.things yield "other thing" Like returning an embedded object's iterator, this saves you having to write a __next__ method. The less work you do, the less bugs you get. ChrisA