[Python-Dev] Re: PEP309 re-written

Peter Harris scav at blueyonder.co.uk
Mon Apr 5 19:29:10 EDT 2004


David Abrams wrote:

>>    
>>
>C = lambda *args, **kw: \
>        Canvas(my_window, width=100, height=100, bg='white', *args, **kw)
>
>  
>
C(bg='red')

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in -toplevel-
    C(bg='red')
  File "<pyshell#3>", line 1, in <lambda>
    C = lambda *args, **kw: \
TypeError: Canvas constructor got multiple values for keyword argument 'bg'

>OK, partial is a little shorter in that case.  I'm not sure that
>example justifies a PEP, though.
>
>  
>
PEPs can be for the little things too.

>    def _merge(d1, d2):
>        d1.update(d2)
>        return d1
>
>    def partial(f, *args, **kw):
>         return lambda *args2, **kw2: f(args + args2, merge(kw2,kw))
>
>??
>  
>
def partial(f, *args, **kw):
    def _merge(d1, d2):
        d1.update(d2)
        return d1
    return lambda *args2, **kw2: f(*(args + args2), **_merge(kw2,kw))


A good implementation indeed.   I may be inclined to provide both a 
class called Partial (for people who want to mess with the internals, or 
for some other reason want a class) and a function called partial.

Would that be OK, everyone?

>>You could maybe sub-class partial to override __call__ , in case you
>>want to do anything fancy like pre-supplying arguments at arbitrary
>>positions.
>>    
>>
>
>A *perfect* example of what lambda is already good at.
>  
>
Yup, no argument from me there.  I can't foresee what others may one day 
want to do, however.

Peter Harris



More information about the Python-Dev mailing list