[New-bugs-announce] [issue7830] Flatten nested functools.partial

Alexander Belopolsky report at bugs.python.org
Mon Feb 1 20:07:31 CET 2010


New submission from Alexander Belopolsky <alexander.belopolsky at gmail.com>:

Currently applying functools.partial to a callable that is already functools.partial object results in a nested object:

>>> from functools import partial
>>> def f(a,b,c): pass
... 
>>> p = partial(partial(f, 1), 2)
>>> p.func, p.args
(<functools.partial object at 0x100431d60>, (2,))


Proposed patch makes partial(partial(f, 1), 2) return partial(f, 1, 2) instead:
>>> p.func, p.args
(<function f at 0x10055d3a8>, (1, 2))

This patch is partially (no pun intended) motivated by a patch submitted by Christophe Simonis for issue4331. Christophe's patch flattens nested partials for a specific case of using partials as bound methods.

As proposed, the patch will enable flattening for subclasses of functools.partial, but will return a baseclass instance.  Flattening will also discard any state attached to the nested partial such as __name__, __doc__, etc or any subclass data.  I believe this is the right behavior, but this caveat is the reason I classify this patch as a "feature request" rather than "performance" or "resource usage".

----------
components: Library (Lib)
files: no-nested-partial.diff
keywords: patch
messages: 98674
nosy: Alexander.Belopolsky
severity: normal
status: open
title: Flatten nested functools.partial
type: feature request
versions: Python 2.7, Python 3.2
Added file: http://bugs.python.org/file16083/no-nested-partial.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7830>
_______________________________________


More information about the New-bugs-announce mailing list