heapreplace, methodcaller

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sat Oct 18 16:01:26 CEST 2008


Hello, I'm experimenting more with Python 2.6 and its numerous
changes.

To improve name coherence I think this method of the heapq module:
heapq.heapreplace(heap, item)

can grow an alias in Python 2.6.1/2.7 and 3.0/3.1:
heapq.heappoppush(heap, item)

So later the heapreplace() name can be deprecated.

The heapq can also become a Heap class (with methods named as the
functions), with an optional key function; time ago I have written
such class in the cookbook.

----------

Regarding the operators module, this syntax:
methodcaller('replace', 'old', 'new')

Has this meaning:
lambda s: s.replace('old', 'new')

I don't know if methodcaller() is faster than that lambda but:
- It's not shorter;
- For me it's not more readable;
- If it's faster than the lambda, then maybe CPython can start
performing a little more optimizations, like turning that tiny lambda
into inlined code.

Bye,
bearophile



More information about the Python-list mailing list