parameterized functions: a question of style
laotseu
bdesth at nospam.free.fr
Mon Aug 12 23:48:27 EDT 2002
Peter Hansen wrote:
> Blair Hall wrote:
>
>>Suppose (just for arguments sake) I need various functions of the form
>>y = x**n, where n is a
>>parameter, ie I want n to be part of the function definition, not an
>>argument to it!
>>
>>I am tempted to write
>>
>>def gen_fn(n):
>> def _fn(x):
>> return x**n
>>
>> return _fn
>
> [...]
>
>>The only alternative I can think of to this would be inheritance
>
> [...]
>
>>This solution leaves me with a class, not a function (which is what I
>>intended to have), so there would be a further step needed to export
>>the class methods as functions.
>
>
> The further step would simply be to define __call__() methods in the
> classes to make the instances callable. Then you have "functions"
> in the sense that you probably need them.
>
> -Peter
*simply* ?
<AMHA>
I'd say the first solution (FP style) is much more simple and readable,
and so much more pythonic...
</AMHA>
Laotseu
More information about the Python-list
mailing list