Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '16,': 0.03; 'suddenly': 0.07; 'fname': 0.09; 'generators': 0.09; 'cc:addr:python-list': 0.11; 'language.': 0.14; "wouldn't": 0.14; '(file': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'refactoring': 0.16; 'rule.': 0.16; 'sorts': 0.16; 'splitting': 0.16; 'subject:File': 0.16; 'well-known': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'versions': 0.24; 'cc:2**0': 0.24; 'subject:/': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; '[1]': 0.29; 'absolute': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'url:wiki': 0.31; 'piece': 0.31; 'url:wikipedia': 0.31; 'file': 0.32; 'this.': 0.32; 'figure': 0.32; 'another': 0.32; 'up.': 0.33; 'cases': 0.33; 'could': 0.34; 'subject:with': 0.35; "can't": 0.35; 'board': 0.35; 'common': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'are,': 0.36; 'done': 0.36; 'doing': 0.36; 'similar': 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'sometimes': 0.38; 'depends': 0.38; 'pm,': 0.38; 'that,': 0.38; 'does': 0.39; 'expensive': 0.39; 'enough': 0.39; 'how': 0.40; 'read': 0.60; 'cost.': 0.60; 'most': 0.60; 'new': 0.61; 'times': 0.62; 'high': 0.63; 'places': 0.64; 'worth': 0.66; 'between': 0.67; 'mar': 0.68; 'subject: & ': 0.68; 'potentially': 0.81; 'thing,': 0.91; 'to:none': 0.92 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=f7qtIXX5xIUSzktRmWXS3a/Y+dn6fFgMiGp+dM5AenM=; b=xIcbn1+V74SNWVxw75H8bObDkHa/BWXUwfQ7ODYnCaNQOMRJjcN6OsWl0E9XeyqFJ8 b5e9Fuk7lzrprXgKxgyVAQXmsl/J6f2ObJF8i6PQf22dtTjyV56+L/2Nub++fIdDdY2j LV3kW//r5LQaRORrMlnX2zFsHOa3CB45OSMViCOS2RErwC6qf/RDOPW70goxBwIEyc8I MEpu9KQe4iTcuLq0g57vvdFxUiyJl1gv64bc8jHC1IVpnCbshi0rwXD+DsxyoUF05J7+ 35OLnfF76lqpbPxqXNLjB/bIiH3ySXTr34jsedWzFkU8EYqvXP5FKcPe37fiwlRqjEmR XFcg== MIME-Version: 1.0 X-Received: by 10.68.34.197 with SMTP id b5mr18219729pbj.16.1394941717237; Sat, 15 Mar 2014 20:48:37 -0700 (PDT) In-Reply-To: References: <532505f0$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 16 Mar 2014 14:48:36 +1100 Subject: Re: Sharing: File Reader Generator with & w/o Policy From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394941726 news.xs4all.nl 2897 [2001:888:2000:d::a6]:41758 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68382 On Sun, Mar 16, 2014 at 2:34 PM, Mark H Harris wrote: > And you are right about another thing, I just want to use this thing over > and over. > > for line in getnumline(filename): > {whatever} > > There does seem to be just one way of doing this (file reads) but there > are actually many ways of doing this. Is a file object really better than a > generator, are there good reasons for using the generator, are there > absolute cases for using a file object? I recommend you read up on the Rule of Three. Not the comedic principle - although that's worth knowing about too - but the refactoring rule. [1] As a general rule, code should be put into a function when it's been done three times the same way. It depends a bit on how similar the versions are, of course; having two places where the exact same thing is done might well be enough to refactor, and sometimes you need to see four or five places doing something only broadly similar before you can figure out what the common part is, but most of the time, three usages is the point to give it a name. There's a cost to refactoring. Suddenly there's a new primitive on the board - a new piece of language. If you can't give it a good name, that's potentially a high cost. Splitting out all sorts of things into generators when you could use well-known primitives like enumerate gets expensive fast - what's the difference between fName and fnName? I certainly wouldn't be able to call that, without actually looking them up. Let your use-cases justify your refactoring. ChrisA [1] https://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)