473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1291
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.