[Tutor] Valid usage of lambda expression

Manprit Singh manpritsinghece at gmail.com
Tue Sep 28 20:20:41 EDT 2021


Dear sir,
Suppose i have to make 3 functions in a program
1) sqr for returning square of a number
2) cube for returning cube of a number
3) cuberoot for getting cube root of a number

Now if  start with a function definition  like this :
def make_powerfunction(n):
     return lambda x: x**n

and then :
square = make_powerfunction(2)
cube = make_powerfunction(3)
cuberoot = make_powerfunction(1/3)

and then doing some maths
>>> square(3)
9
>>> cube(4)
64
>>> cuberoot(27)
3.0

My question is, is it a good practice to derive new functions in this way,
as i have derived the functions square, cube and cuberoot from
make_powerfunction.

Regards
Manprit Singh


More information about the Tutor mailing list