469,927 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

heapreplace, methodcaller

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
Oct 18 '08 #1
2 1137
On Sat, 18 Oct 2008 07:01:26 -0700, bearophileHUGS wrote:
Hello, I'm experimenting more with Python 2.6 and its numerous changes.

[…]

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;
Then use the ``lambda``.

Your example has just literal constants. Let's take this example:

methodcaller(meth, arg_a, arg_b)

which is expressed as ``lambda`` function:

lambda x, m=meth, a=arg_a, b=arg_b: getattr(x, meth)(a, b)

Which is longer and IMHO less readable than `methodcaller()`.
- 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.
How? The functions in `operator` are meant to be passed directly or to
to create other functions that are passed as HOFs into other functions.
So the compiler doesn't know in which functions they are used in the end
and it's possible that different functions are used there too.

Ciao,
Marc 'BlackJack' Rintsch
Oct 18 '08 #2
On Oct 18, 7:01*am, bearophileH...@lycos.com wrote:
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.
Too late for 2.6 and possibly too late and too disruptive for 3.0
(which needs to minimize transitions from 2.6).
Raymond
Oct 19 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Raymond Hettinger | last post: by
6 posts views Thread by Steven Bethard | last post: by
26 posts views Thread by Steven Bethard | last post: by
12 posts views Thread by Paul Miller | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
18 posts views Thread by bearophileHUGS | last post: by
2 posts views Thread by jm.suresh | last post: by
7 posts views Thread by Peter Laan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.