[Python-Dev] Possible patch for functools partial (original) (raw)

Steven D'Aprano steve at pearwood.info
Thu May 13 13:30:29 CEST 2010


On Thu, 13 May 2010 06:50:02 pm Yaniv Aknin wrote:

I'm never certain where to reply in such a case, on the list or on the issue, but since no one is nosy yet to Daniel's patch, I thought I'd ask here.

While a partial object should reasonably never change, you could change it: [...] I realize touching p.keywords voids your warranty, but if we can stop people from doing it, maybe we should (or at least put a warning in the documentation, no?).

Modifying partial.keywords will almost certainly effect hashing, so I think this is relevant to the patch.

So I'm thinking either we make an immutable/hashable dict while we're at it, or store the keyword arguments as a tuple (which guarantees immutability), and only convert them back to a dict when you want to call the partial object (simpler, slower).

I'd support an immutable dict. partial objects already impose a significant (~ 30%) performance penalty:

from timeit import Timer min(Timer('f(5)', 'f = lambda x: x').repeat()) 0.93580079078674316 min(Timer('p(5)', 'from functools import partial; p = partial(lambda x: x)').repeat()) 1.2715129852294922

No need to make that worse if that can be avoided.

-- Steven D'Aprano



More information about the Python-Dev mailing list