Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: avoid for loop calling Generator function Date: Mon, 22 Feb 2016 09:11:11 -0700 Lines: 41 Message-ID: References: <0c289f93-ce64-4eff-93f3-c70d7ff50817@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de pYyFCRaJud9p0mHdcLqjLQ+/PvBQr0bjS8o6O2k1DHgQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.035 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; '22,': 0.09; 'repr': 0.09; 'def': 0.13; 'file,': 0.15; '2016': 0.16; 'iterating': 0.16; 'readable': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'app': 0.16; 'memory': 0.17; '>': 0.18; 'creates': 0.18; 'email addr:gmail.com>': 0.18; 'function,': 0.22; 'object.': 0.22; 'am,': 0.23; 'feb': 0.23; 'slightly': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; '(such': 0.27; 'object,': 0.27; 'yield': 0.27; 'function': 0.28; 'prints': 0.29; 'way?': 0.29; 'creating': 0.30; 'received:google.com': 0.35; 'text': 0.35; 'returning': 0.35; 'text.': 0.35; 'something': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'monday,': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'skip:& 10': 0.37; 'received:209.85.213': 0.37; 'skip:s 50': 0.37; 'difference': 0.38; 'received:209': 0.38; 'skip:s 40': 0.38; 'wrong': 0.38; 'why': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; "you'll": 0.61; 'avoid': 0.61; 'more': 0.63; 'profile': 0.63; 'great': 0.63; 'between': 0.65; 'introduce': 0.79; 'notably,': 0.84; 'otten': 0.84; 'to:name:python': 0.84; 'utc+5:30,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=57taTq8tGBUYIyjR5VuLQWEczMwIqLCs28wCOAOzQ1A=; b=V0L+dN50PwR7BAt29mJJlZO3fT/ZnBYsBJPz5NavdO750edTMrx2+o043bpSyhIbOK 20TLnUNmRwSoN0VXZi9sHh7fHESVGnpscssx+NQecroMJw8B+UzKf1UHw6g+eY4xeLG2 mRTxFKA1GfEufS6YV/2p/5bsZEe8etGvEpI/Q0pSfhKPgHo/KUQrNQi+Ls2hRIL6TWE1 +rGEXnwlOQnJHA8Sw94AsFITCAbcKDS/q6pR+4n3f6PRUKVuWBLZaJ3oa6PXATSVG5LU +ndSCv7a6yjfegcy9FUr2nDwb8uCnjxYJ6vO/rWJD0G/gQRPVqp8pqvGVaeTJtMAwC7G VPZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=57taTq8tGBUYIyjR5VuLQWEczMwIqLCs28wCOAOzQ1A=; b=SACoH5h1ljFCO6J+39C+u/8Je427YpyvQYvdbKkZMjRCtESewWK7TAf94NH+mjsrM1 dBumtdnf3QWJ0zUdNmG9WnqWbRIRH8n3u/YPdF2c5OlaGp4bLYrJHfVnC1X6G2br9ED/ rOySBK7ZHXQnBvJxLWkbqwdUPrICsK9Z2HOYXvccRdd66WKjbQuvKSiUW9t3/36mXm0N ka+vZhTf70h4VUHG0LsAx4aF+IzUr5AJiRPKhCi29xhUeK2qrkaVjJf2eVXcv57Iimcn voOsM4LKsFXG7goThVp4gZjv8GryYhccSTaAwSfcihFyjBFR4XJDFL1tXyrUuozE8LHt Q/qg== X-Gm-Message-State: AG10YOTADhhGmkkJRIUibpAdLe2rngqyZiG0lTESBxPMkzsxk+hcQttv7sG+ui7QXvpmde7Ol8DRuHK21P4OEg== X-Received: by 10.50.112.10 with SMTP id im10mr12260529igb.93.1456157511020; Mon, 22 Feb 2016 08:11:51 -0800 (PST) In-Reply-To: <0c289f93-ce64-4eff-93f3-c70d7ff50817@googlegroups.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.21rc2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 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:103346 On Mon, Feb 22, 2016 at 8:38 AM, Arshpreet Singh wrote: > On Monday, 22 February 2016 19:05:24 UTC+5:30, Peter Otten wrote: >> or the slightly less convoluted >> >> sys.stdout.writelines(map("{}\n".format, read_pdf("book.pdf"))) > > Actually I am using this function in Android App which is being built using Kivy, Where I am returning whole text into a file, So what you think will be more efficient way? Profile them and find out, but I don't think you'll find the difference is great enough to be overly concerned with. Pick the way that is more readable and doesn't introduce any gross inefficiencies (such as concatenating strings in a loop). > But when I am calling pdf_read() from nother function to avoid for loop why it is not working? > say: > > def hello() > yield from read_pdf('book.pdf') This uses yield from, which makes it a generator function. > > print(hello()) # still returns memory location instead of text. If I am not wrong yield from can be used to avoid for loop? hello is a generator function, so calling it just creates a generator object. Printing it then prints out the repr of that generator object, which is just something like . Notably, you haven't actually *executed* the generator object, which would require iterating over it, e.g.: for i in hello(): print(i) So you haven't actually avoided creating a for loop; you've just added a redundant layer between the for loop and the thing it's actually iterating over.