[Tutor] beginning to code

Rick Johnson rantingrickjohnson at gmail.com
Tue Sep 12 13:52:01 EDT 2017


Paul  Moore wrote:
[...]
> Ignoring stylistic choices (variable naming, map vs
> generator) then for me the key distinction here is "lambda
> f: int(f)" vs just "int". Python's callables (of which the
> integer constructor int is one) are first class objects, so
> you should just pass them directly. Creating an anonymous
> function using lambda that just calls int is slower, harder
> to read, and less natural. The lambda is a direct
> translation of the Ruby "|f| f.to_i()" - I don't know if
> Ruby lacks a built in "convert to integer" function - maybe
> it does because it takes the "everything is an object"
> philosophy much further than Python does, but that's where
> "this code was translated from another language" shows.

I was wondering why i put that lambda in there, and that's
probably why i did it, superficially, to more accurately
compare the syntax of both languages, at the expense of
writing concise Python code.

In any event, i believe my point -- that complex statements
in Ruby follow a more intuitive left-to-right comprehension
flow, whereas Python, which due to a reliance on built-in
functions as opposed to Object methods is directly
responsible for our code being littered with these less
intuitive nested function calls -- remains a valid point.

> Using map vs comprehensions is mostly a stylistic choice.
> Python programmers will typically choose a comprehension,
> so that style looks more idiomatic, but the map is not
> wrong. I haven't tested which is faster - I can't imagine
> it would make much practical difference in a real program.
> Readability is certainly a personal choice - but writing
> code that looks natural to other users of the language is
> an element of readability (not the only one, but it is
> one).

I would agree on all points.



More information about the Python-list mailing list