Convert '165.0' to int

SigmundV sigmundv at gmail.com
Mon Jul 25 12:39:15 EDT 2011


On Jul 25, 10:48 am, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
>
> One other important proviso: if your map function is a wrapper around a
> Python expression:
>
> map(lambda x: x+1, data)
> [x+1 for x in data]
>
> then the list comp will be much faster, due to the overhead of the function
> call. List comps and gen exprs can inline the expression x+1, performing it
> in fast C rather than slow Python.
>
> But if you're calling a function in both cases:
>
> map(int, data)
> [int(x) for x in data]
>
> then the overhead of the function call is identical for both the map and the
> list comp, and they should be equally as fast. Or slow, as the case may be.

I would like to thank Steven for his enlightening (at least for me)
post.

In the OP's case I'd keep everything as lists initially. If speed then
is an issue other constructs can be considered. The use of map in the
example only reflects my inherently mathematical way of thinking.

Generally, I'd say

1) write code that works, i.e. does what it's intended to do in all
cases, and
2) if speed is an issue, try to sort out the main culprits.

Coding style is a different issue altogether, but in general I'd say
that one should use self-explanatory variable names.


Sigmund



More information about the Python-list mailing list