[issue7830] Flatten nested functools.partial

Josh Rosenberg report at bugs.python.org
Tue Oct 7 00:55:19 CEST 2014


Josh Rosenberg added the comment:

The use case in question, simplified, was:

from functools import partial

class Foo:
    Bar = othermodule.Bar

    def __new__(cls, ...):
        ...
        cls.Bar(...)
        ...

    def bind_stuff(cls, *args, **kwargs):
        cls.Bar = partial(Bar, *args, **kwargs)

Every time they created an instance of Foo, there would be a Foo.bind_stuff call beforehand that fixed some settings they didn't want to make a part of Foo's __new__ profile. And in fact, in practice, they were only binding the same keyword args over and over, so they could have solved the problem by just rebinding the base othermodule.Bar. I'm not defending this design, but from what I can tell, it's a textbook example of where your patch would solve the problem. cls.Bar has no instance variables assigned (hence no __dict__?), and it's always functools.partial that's used, not some special variant.

A simple way to repro the fundamental problem they were experiencing is to just wrap int a lot:

>>> for i in range(1001):
        int = partial(int)
>>> int(5) # Kaboom! Which I assume your patch would prevent

----------

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


More information about the Python-bugs-list mailing list