parameterized functions: a question of style
b.hall at irl.cri.nz
Sun Aug 11 22:46:16 EDT 2002
I would be interested in comments / suggestions regarding the following
attempt to generate a family of functions.
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
I could then obtain different functions like
squared = gen_fn(2)
cubed = gen_fn(3)
The only alternative I can think of to this would be inheritance, ie
return x ** self.n
self.n = 2
self.n = 3
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
Any suggestions, gotchas?
(I realize that the first way uses nested scopes, but that's ok from
More information about the Python-list