parameterized functions: a question of style
Peter Hansen
peter at engcorp.com
Sun Aug 11 23:17:58 EDT 2002
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
More information about the Python-list
mailing list