[Python-Dev] Possible patch for functools partial - Interested?

Daniel Urban urban.dani at gmail.com
Thu May 13 11:51:23 CEST 2010


> While a partial object should reasonably never change, you could change it:
>>>> from functools import partial
>>>> p = partial(lambda *a, **kw: kw, 1, 2, spam='eggs')
>>>> p()
> {'spam': 'eggs'}
>>>> p.keywords['spam'] = 'bacon'
>>>> p()
> {'spam': 'bacon'}
>>>>
> 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?). 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).

You're right. I think it is possible to stop people from modifying
p.keywords. If p.keywords wouldn't be a dict, but a read-only proxy
for that dict, that may solve this problem. I think that is possible,
with PyDictProxy_New [1]. But I'm not certain if that is a good idea,
it may have side effects I'm not aware of. Any thoughts about this?

> Your thoughts? Should we continue this discussion at issue8699?
I don't know, I'm new here...

[1] http://docs.python.org/py3k/c-api/dict.html#PyDictProxy_New

Thanks,
Daniel Urban


More information about the Python-Dev mailing list