<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Asktav - Latest Comments in Ruby-style Blocks in Python</title><link>http://asktav.disqus.com/</link><description>Tav's Blog</description><atom:link href="https://asktav.disqus.com/ruby_style_blocks_in_python/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Sat, 17 Jan 2015 18:02:20 -0000</lastBuildDate><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-1798915543</link><description>&lt;p&gt;Nice work!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">tav</dc:creator><pubDate>Sat, 17 Jan 2015 18:02:20 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-1798840598</link><description>&lt;p&gt;I'm really late to this party but I've been working on something (&lt;a href="https://github.com/yawaramin/lambdak" rel="nofollow noopener" target="_blank" title="https://github.com/yawaramin/lambdak"&gt;lambdak&lt;/a&gt;) that provides multi-line (multi-statement) lambda functionality as a native Python module. Lmbdak can't do &lt;i&gt;everything&lt;/i&gt; a normal function block can do but I believe I can translate the &lt;code&gt;using&lt;/code&gt; examples you provided:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;webapp.runner(given_(lambda config:&lt;br&gt;  setattr_(config, "time_zone", "UTC", lambda:&lt;br&gt;  setattr_(config, "log_level", "debug"))))&lt;br&gt;&lt;br&gt;# Actually this can be done in just a single normal lambda thanks to the&lt;br&gt;# `if` expression. But just for the sake of argument....&lt;br&gt;employees.select(given_(lambda emp:&lt;br&gt;  fireEmployee(emp) if emp.salary &amp;gt; developer.salary&lt;br&gt;  else extendContract(emp)))&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Admittedly it looks really ugly, but in day-to-day work you can use a few &lt;a href="https://github.com/yawaramin/lambdak/wiki/Vim-Tips-for-Functional-Pythonistas" rel="nofollow noopener" target="_blank" title="https://github.com/yawaramin/lambdak/wiki/Vim-Tips-for-Functional-Pythonistas"&gt;tricks&lt;/a&gt; to make it look a little nicer in your editor screen.&lt;/p&gt;&lt;p&gt;I think it has the distinct advantage of being fully functional (pun intended) right now, without any approval needed from BDFL.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Yawar Amin</dc:creator><pubDate>Sat, 17 Jan 2015 16:50:29 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-361351092</link><description>&lt;p&gt;Meh, that's like saying "I hate keyboards 'cause they lead to lots of characters." It's all about discipling yourself as a developer to avoid doing things poorly. Fault oneself not the language or bad practice of others.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">hiwaylon</dc:creator><pubDate>Fri, 11 Nov 2011 16:20:12 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-154420761</link><description>&lt;p&gt;I hate Ruby's blocks for the simple fact that it leads to repeated logic everywhere. Defining a function for anything more than a one-liner "is a good thing" in my book.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Henrique</dc:creator><pubDate>Tue, 22 Feb 2011 23:11:02 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-79606046</link><description>&lt;p&gt;Could you provide the PEP number for this proposal&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Rajiv</dc:creator><pubDate>Mon, 20 Sep 2010 20:22:45 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46314654</link><description>&lt;p&gt;How about using 'as' instead of 'do'? Similar to 'import some as thing'.&lt;/p&gt;&lt;p&gt;using webapp.runner as (config)&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">JT</dc:creator><pubDate>Fri, 23 Apr 2010 18:01:35 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46263649</link><description>&lt;p&gt;No! I have it now. "give"!&lt;/p&gt;&lt;p&gt;give function def(x):&lt;br&gt;   blockblockblock&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lennart Regebro</dc:creator><pubDate>Fri, 23 Apr 2010 13:29:51 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46263433</link><description>&lt;p&gt;I prefer "using function def(x):" or possibly "on function def(x):" as syntax. But I agree, examples that can't easily be done as loops + generators are welcome. Suprisingly many uses of blocks can be done like that. See my Python vs Ruby blogpost: &lt;a href="http://regebro.wordpress.com/2009/07/12/python-vs-ruby/" rel="nofollow noopener" target="_blank" title="http://regebro.wordpress.com/2009/07/12/python-vs-ruby/"&gt;http://regebro.wordpress.co...&lt;/a&gt;&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lennart Regebro</dc:creator><pubDate>Fri, 23 Apr 2010 13:28:27 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46262580</link><description>&lt;p&gt;No. Yuck. Sorry. The block should be an anonymous function, and behave like an anonymous function. And that means return returns from the block. Python is not Ruby or lisp and should not be ruby or lisp.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lennart Regebr</dc:creator><pubDate>Fri, 23 Apr 2010 13:23:08 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46261666</link><description>&lt;p&gt;I completely agree with overloading def. I've suggested that instead of lambda before, ie that lambda x: x + x should be spelled def(x): x+x, but this is better. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lennart Regebro</dc:creator><pubDate>Fri, 23 Apr 2010 13:17:33 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46249663</link><description>&lt;p&gt;What were your concerns regarding exceptions? I would think it is the callers responsibility to catch them through either wrapping the entire block or from within the block.&lt;/p&gt;&lt;p&gt;While I honestly never really realized the potential of blocks in Ruby, I've been by smarter people than me that they are what make most of the interesting things in Ruby possible. If you can help provide some insight on what your concerns are regarding exceptions, I'm sure there are people that could help find a solution and work towards submitting a pep.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eric Larson</dc:creator><pubDate>Fri, 23 Apr 2010 12:04:12 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46220950</link><description>&lt;p&gt;I don't think you're talking nonsense. When I read the article, I couldn't decide if the examples weren't sufficient to make the case, or the fact is that I've never seen the attraction of Ruby's blocks, but something didn't look or feel right. Using "do" in the proposed syntax is part of why it didn't seem right - and probably why it looks awkward in Ruby too.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John C</dc:creator><pubDate>Fri, 23 Apr 2010 08:32:28 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-46209314</link><description>&lt;p&gt;I don't know anything about anything, and I think I've only half understood this, but the conventional meaning of the word 'do'  doesn't seem to match the intended use here. 'do' sounds like 'apply' - whereas when defining a lambda, we're doing the *opposite* of that. Creating a lambda is saying 'don't do this yet, but create it to do something with later'.&lt;/p&gt;&lt;p&gt;Instead of using 'do', why not overload 'def' ?&lt;/p&gt;&lt;p&gt;Def with a name currently defines a named function. Def in the context shown above, with no name, ought to define an anyonymous lambda.&lt;/p&gt;&lt;p&gt;Am I talking nonsense?&lt;/p&gt;&lt;p&gt;Best regards,&lt;/p&gt;&lt;p&gt;  Jonathan&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jonathan Hartley</dc:creator><pubDate>Fri, 23 Apr 2010 06:20:49 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-12622624</link><description>&lt;p&gt;Sorry to hear that. I'm not familiar with the intrinsics of the PEP process, but maybe there is a chance of publishing what you have so far, and open the door for others to work on it. Maybe someone else has a solution.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Name</dc:creator><pubDate>Tue, 14 Jul 2009 06:27:14 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-12583056</link><description>&lt;p&gt;Thanks man -- unfortunately I got stuck in terms of figuring out how to make exceptions raised inside blocks be Pythonic...&lt;/p&gt;&lt;p&gt;Without solving that, not much use in putting together a PEP =(&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">tav</dc:creator><pubDate>Mon, 13 Jul 2009 12:31:49 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-12571835</link><description>&lt;p&gt;Dude, this is cool as shit, I love it! Without reading all the 50 comments, is there any progress on this, like a PEP?!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Name</dc:creator><pubDate>Mon, 13 Jul 2009 05:17:26 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7337680</link><description>&lt;p&gt;How about 'let' or 'let .. in' ? we could even have letrec.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Verte</dc:creator><pubDate>Thu, 19 Mar 2009 04:52:32 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7106615</link><description>&lt;p&gt;Good idea, I would love this addition.  Here is a good example of a ruby lib that uses blocks to construct a dsl:&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pluginaweek.org/2009/03/08/state_machine-one-machine-to-rule-them-all/" rel="nofollow noopener" target="_blank" title="http://www.pluginaweek.org/2009/03/08/state_machine-one-machine-to-rule-them-all/"&gt;http://www.pluginaweek.org/...&lt;/a&gt;&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Eric</dc:creator><pubDate>Wed, 11 Mar 2009 14:12:19 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7094190</link><description>&lt;p&gt;I disagree on your last point.  The biggest reason to use functions is to ease understanding.  Most programs could be inlined into a single massive function, but the mental effort to understand it would increase by many times, effectively distracting you from the real work that's going on.&lt;/p&gt;&lt;p&gt;Unit testing, although nice, falls out of this "unitizing".  It's not the original motivation.&lt;/p&gt;&lt;p&gt;(Although that may also harm performance, compilers could be written to recognize all the duplication and merge common bits.  Functions are quite entrenched in common styles though, so there's little motivation to do this today.)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Adam Olsen</dc:creator><pubDate>Wed, 11 Mar 2009 01:57:00 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7083573</link><description>&lt;p&gt;A "split lambda" perhaps?&lt;/p&gt;&lt;p&gt;records = &lt;a href="http://employee.select" rel="nofollow noopener" target="_blank" title="employee.select"&gt;employee.select&lt;/a&gt;(lambda employee):&lt;br&gt;    return do_something(employee) if cond(employee) else None&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">MRAB</dc:creator><pubDate>Tue, 10 Mar 2009 18:35:46 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7056926</link><description>&lt;p&gt;Well you wouldn't need the imports in the version below... and yes the assumption is that you can skip the @contextmanager line.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Alia Khouri</dc:creator><pubDate>Tue, 10 Mar 2009 01:56:10 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7039346</link><description>&lt;p&gt;So... the only difference is that you can skip the @contextmanager line? (You need the imports anyway in either version)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan</dc:creator><pubDate>Mon, 09 Mar 2009 15:48:36 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7038558</link><description>&lt;p&gt;@employee.select&lt;br&gt;def records():&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Demur Rumed</dc:creator><pubDate>Mon, 09 Mar 2009 15:16:31 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7036367</link><description>&lt;p&gt;There is probably no need to introduce ruby-like blocks to python where iteration comes naturally with list&lt;br&gt;comprehensions and generators. But for the simple case of entering a block of code as one does with @contextmanager I suppose it would be nice to make a generator with a single yield statement a contextmanager by default such that:&lt;/p&gt;&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; def g():&lt;br&gt;...     print "a"&lt;br&gt;...     yield&lt;br&gt;...     print "b"&lt;br&gt;...&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; with g():&lt;br&gt;...     print "c"&lt;br&gt;...&lt;br&gt;a&lt;br&gt;c&lt;br&gt;b&lt;/p&gt;&lt;p&gt;would be equivalent to&lt;/p&gt;&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; from __future__ import with_statement&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; from contextlib import contextmanager&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; @contextmanager&lt;/p&gt;&lt;p&gt;... def g():&lt;br&gt;...     print "a"&lt;br&gt;...     yield&lt;br&gt;...     print "b"&lt;br&gt;...&lt;br&gt;&amp;gt;&amp;gt;&amp;gt; with g():&lt;/p&gt;&lt;p&gt;...     print "c"&lt;br&gt;...&lt;br&gt;a&lt;br&gt;c&lt;br&gt;b&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Alia Khouri</dc:creator><pubDate>Mon, 09 Mar 2009 13:41:45 -0000</pubDate></item><item><title>Re: Ruby-style Blocks in Python</title><link>http://www.asktav.com/ruby-style-blocks-in-python.html#comment-7036227</link><description>&lt;p&gt;Hugo, a function (anonymous or named) isn't "just a syntactic element" like while, if etc. It's a compound element taking arguments, with a defined entry point and a defined return value. A function is, or at least should be, coupled loosely to other functions while syntactic elements are coupled very strongly to other elements.&lt;/p&gt;&lt;p&gt;An analogy might be that functions are molecules while single elements are atoms. The semantics of the atom "if x &amp;gt; y: x += y" depends on the other atoms around it, while ideally the function sin() is always the trigonometric sine no matter what other functions are around it. So you should be able to test sin() in isolation, which you can't do with the if block.&lt;/p&gt;&lt;p&gt;(Besides, you can perform limited, ad hoc testing of individual elements simply by sprinkling asserts after each element. Not a good solution, but it is something.)&lt;/p&gt;&lt;p&gt;The whole purpose of functions (procedures or sub-routines) is to lessen the coupling between parts of code, to partially isolate chunks of code for ease of re-use and testing. If a code block is tightly coupled like a while or if, then it is a step backwards towards spaghetti code; and if it is loosely coupled, like a function, then there's no benefit to that looseness if you can't test it or re-use it.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Steven D'APrano</dc:creator><pubDate>Mon, 09 Mar 2009 13:35:32 -0000</pubDate></item></channel></rss>