[Tutor] "Overloading" methods

Lie Ryan lie.1296 at gmail.com
Fri Sep 17 18:56:20 CEST 2010


On 09/17/10 00:22, Vince Spicer wrote:
> 
> 
> Well I can't comment on right or wrong I would think creating a simple
> class with a __call__ method is a little more pythonic.

I think even more pythonic is to use closure:

def create_setpattern(type_):
    def f(self, pattern, parameter):
        return pattern_generator(self, type_, pattern, parameter)
    return f
setpattern_iis = create_setpattern('iis')

for short function body, you can use lambda:

def create_setpattern(t):
    return lambda s, pat, par: pattern_generator(s, t, pat, par)



probably the best solution is to use functools.partial:

from functools import partial
setpattern_iis = partial(pattern_generator, type='iis')

however, this does NOT work due to (TypeError: pattern_generator() got
multiple values for keyword argument 'type'); do you think it will be
reasonable for partial to support this use case?



More information about the Tutor mailing list