<br><div class="gmail_quote">On Mon, May 13, 2013 at 11:33 AM, Juancarlo Añez <span dir="ltr"><<a href="mailto:apalala@gmail.com" target="_blank">apalala@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="gmail_extra">In Python, functions, methods, and classes are objects too, and their constructors are "class" and "def". But there's an asymmetry in the language in that those two constructors don't produce an assignable value.</div>
<div class="gmail_extra"></div></blockquote></div><br>Not all functions produce a useful value. <div><br></div><div><font face="courier new, monospace"> [].append(1)</font></div><div><font face="courier new, monospace"> [].sort()</font></div>
<div><font face="courier new, monospace"> print(1)</font></div><div><br></div><div>Yes, these do return a value since functions always return a value, but they don't return a useful value. Likewise, class and def have side effects so if they were functions they would probably return None and you would have the same issue that you couldn't usefully use them inside another statement, just like this:</div>
<div><br></div><div><font face="courier new, monospace"> x = print(y)</font></div><div><br></div><div>Def is not a constructor. It is an assignment statement.</div><div><br></div><div><font face="courier new, monospace"> def f(x): return x+1</font></div>
<div><font face="courier new, monospace"> f = lambda x: x+1</font></div><div><br></div><div>are equivalent. Python does not allow either one of these assignment statements to be embedded in another statement. It does allow lambda functions to be embedded in other statements. The issue here is essentially that the def syntax allows more complex function syntax than lambda. And the complaint is that that you have to declare a function "out of order" and choose a name for it. This has the same problem:</div>
<div><br></div><div><font face="courier new, monospace"> # can't do this</font></div><div><font face="courier new, monospace"> case a.b[i]:</font></div><div><font face="courier new, monospace"> when 0: pass</font></div>
<div><font face="courier new, monospace"> when 1: pass</font></div><div><font face="courier new, monospace"> when 2 ... 4: pass</font></div><div><font face="courier new, monospace"> else: pass</font></div>
<div><br></div><div><div><font face="courier new, monospace"> </font><span style="font-family:'courier new',monospace"># can't do this either</span></div><div><span style="font-family:'courier new',monospace"> </span><span style="font-family:'courier new',monospace"> </span><font face="courier new, monospace">if (_value = a.b[i]) == 0: pass</font></div>
<div><font face="courier new, monospace"> elif _value == 1: pass</font></div><div><font face="courier new, monospace"> elif _value in 2 ... 4</font><span style="font-family:'courier new',monospace">: pass</span></div>
<div><font face="courier new, monospace"> else: pass</font></div><div><br></div></div><div><div><font face="courier new, monospace"> </font><span style="font-family:'courier new',monospace"># have to do this</span></div>
<div><span style="font-family:'courier new',monospace"> </span><span style="font-family:'courier new',monospace"> </span><span style="font-family:'courier new',monospace">_value = a.b[i]</span></div>
<div><span style="font-family:'courier new',monospace"> if _value == 0: pass</span></div><div><font face="courier new, monospace"> elif _value == 1: pass</font></div><div><font face="courier new, monospace"> elif _value >= 2 and value <= 4: pass</font></div>
<div><font face="courier new, monospace"> else: pass</font></div></div><div><font face="courier new, monospace"><br></font></div><div>This is a much more common scenario than wanting anonymous blocks. I'm not proposing to change this. I'm just pointing out that if you're complaining about not being able to assign a value inside a statement, there are more common cases to look at.</div>
<div><br clear="all"><div><font face="arial, helvetica, sans-serif">--- Bruce</font><div><span style="font-family:arial,helvetica,sans-serif">Latest blog post: Alice's Puzzle Page </span><a href="http://www.vroospeak.com/" style="font-family:arial,helvetica,sans-serif" target="_blank">http://www.vroospeak.com</a></div>
<div><div><font face="arial, helvetica, sans-serif">Learn how hackers think: <a href="http://j.mp/gruyere-security" target="_blank">http://j.mp/gruyere-security</a></font></div></div></div>
</div>