[Tutor] Converting integers into digit sum (Python 3.3.0)

eryksun eryksun at gmail.com
Tue Dec 10 20:25:52 CET 2013


On Tue, Dec 10, 2013 at 1:08 PM, spir <denis.spir at gmail.com> wrote:
> There is another standard tool called 'apply' in general, which sequentially
> *performms* the effect of an action on a sequence of inputs. Since it just
> applies action, 'apply' does not return any result. 'apply' is rarely used
> (even more than map; actually I think apply does not even exist in Python),

There's a deprecated function named "apply" in 2.x, but it doesn't
apply a function iteratively to a sequence. apply(f, args, kwds) is
the same as the extended call syntax f(*args, **kwds).

Since the 3.x map class is an iterator, you could consume its output
in a zero-length deque:

    from collections import deque

    def apply(f, seq):
        deque(map(f, seq), maxlen=0)

> Apart from that, it is still *very* annoying to be forced to write
> mini-functions just for tiny blocks of codes to be used by map (or filter,
> or...). The solution in Python is comprehensions, where you write the code
> directly. Soon, you'll meet and learn about them, and probably soon later
> you'll start to love them ;-).

The case for using map and filter was weakened by comprehensions and
generator expressions. In CPython, however, map and filter are faster
at applying a built-in function or type to a built-in sequence type,
such as sum(map(int, str(n))) as opposed to sum(int(d) for d in
str(n)).


More information about the Tutor mailing list