squared functions--most Pythonic way?
Luigi Ballabio
ballabio at mac.com
Thu Jun 27 15:38:03 EDT 2002
Hi all,
suppose we want to define a function which, given f(x), returns
its square. There are a number of ways which come to mind, such as:
object-oriented programming:
def fsquare:
def __init__(self,f):
self.f = f
def __call__(self,x):
return self.f(x)**2
functional programming:
# with an internal named function
def fsquare(f):
def f2(x):
return f(x)**2
return f2
# with lambda
def fsquare(f):
return lambda x: f(x)**2
and I'm sure people can come out with others that I didn't figure but
are obvious to any Dutchmen.
My question: is any of the above considered more Pythonic?
Thanks,
Luigi
More information about the Python-list
mailing list