472,354 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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 1252
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Raymond Hettinger | last post by:
Found in a pamphlet at a pre-school: --------------------------------------- Reading improves vocabulary Reading raises cultural literacy through shared knowledge Reading develops writing skills...
6
by: Steven Bethard | last post by:
I was wondering if there's any plans to add a "key" argument to max (and min) like was done for sort(ed)? I fairly often run into a situation where I have something like: counts = {} for item...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
12
by: Paul Miller | last post by:
I see lambda is "going away", so I want to use something that will be around for awhile. All I want to do is provide an "inline function" as an argument to another function. For example,...
6
by: Felix I. Wyss | last post by:
It appears that VC++2003 has a code generator bug related to template parameters that are a pointer-to-member type: If the actual template argument is a virtual method, VC generates code that...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37) Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51) RFE : 224 open...
18
by: bearophileHUGS | last post by:
In few minutes I have just written this quite raw class, it lacks doctring (the same of the functions of the heapq module), it may contain bugs still, I haven't tested it much. It's just a simple...
2
by: jm.suresh | last post by:
I wanted to have a heap of custom objects, and in different heaps I wanted to have the weights for my elements differently. So, I modified the heapq module to accept key arguments also. The...
7
by: Peter Laan | last post by:
Is there a simple way to encapsulate the functionality to redo a method call a second time in case a specific exception is thrown? We are sending commands to an external system and if the sessionId...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.