[Python-ideas] functools.partial

Nick Coghlan ncoghlan at gmail.com
Fri Jun 8 05:40:25 CEST 2012

On Fri, Jun 8, 2012 at 12:57 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> Hello,
> While I was working on adding support for 'functools.partial' in PEP 362,
> I discovered that it doesn't do any sanity check on passed arguments
> upon creation.
> Example:
>    def foo(a):
>        pass
>    p = partial(foo, 1, 2, 3) # this line will execute
>    p() # this line will fail
> Is it a bug?  Or is it a feature, because we deliberately don't do any checks
> because of performance issues?  If the latter - I think it should be at least
> documented.

Partly the latter, but also a matter of "this is hard to do, so we
don't even try". There are many other "lazy execution" APIs with the
same problem - they accept an arbitrary underlying callable, but you
don't find out until you try to call it that the arguments don't match
the parameters. This leads to errors being raised far away from the
code that actually introduced the error.

If you dig up some of the older PEP 362 discussions, you'll find that
allowing developers to reduce this problem over time is the main
reason the Signature.bind() method was added to the PEP. While I
wouldn't recommend it for the base partial type, I could easily see
someone using PEP 362 to create a "checked partial" that ensures
arguments are valid as they get passed in rather than leaving the
validation until the call is actually made.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list