Using the Python Interpreter as a Reference

Travis Parks jehugaleahsa at gmail.com
Mon Nov 28 16:34:07 EST 2011


On Nov 28, 2:32 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> On Sun, Nov 27, 2011 at 4:55 PM, Steven D'Aprano
>
> <steve+comp.lang.pyt... at pearwood.info> wrote:
> >> My language combines generators and collection initializers, instead of
> >> creating a whole new syntax for comprehensions.
>
> >> [| for i in 0..10: for j in 0.10: yield return i * j |]
>
> > Are we supposed to intuit what that means?
>
> > Is | a token, or are the delimiters [| and |] ?
>
> > Is there a difference between iterating over 0..10 and iterating over
> > what looks like a float 0.10?
>
> > What is "yield return"?
>
> I would assume that "yield return" is borrowed from C#, where it is
> basically equivalent to Python's yield statement.  The advantage of
> using two keywords like that is that you can compare the statements
> "yield return foo" and "yield break", which is a bit clearer than
> comparing the equivalent "yield foo" and "return".
>
> Having to type out "yield return" in every comprehension seems a bit
> painful to me, but I can understand the approach: what is shown above
> is a full generator, not a single "generator expression" like we use
> in Python, so the statement keywords can't be omitted.  It's trading
> off convenience for expressiveness (a bad trade-off IMO -- complex
> generators should be named, not anonymous).
>
> >> Lambdas and functions are the same thing in my language, so no need for
> >> a special keyword.
>
> > That does not follow. Lambdas and def functions are the same thing in
> > Python, but Python requires a special keyword.
>
> I think the implication is that Unit has only one syntax for creating
> functions, which is lambda-style.  In any case, why does Python
> require a special keyword?  def is only used in a statement context,
> and lambda is only used in an expression context.  Why not use the
> same keyword for both?  I think the answer is historical:  def came
> first, and when anonymous functions were added it didn't make sense to
> use the keyword "def" for them, because "def" implies a name being
> defined.
>
> Cheers,
> Ian
>
>

Most languages I have worked with have a "lambda" syntax and a
function syntax. It has always been a historical artifact. Languages
start out avoiding functional features and then eventually adopt them.
It seems that eventually, convenient high-order functions become a
must-have (most standard algorithm packages). It is a conflict between
old C-style programming and the need for functional code. As soon as
functions can be assigned to variables, the code starts looking oddly
like JavaScript.



More information about the Python-list mailing list