Practice problem using lambda inside the class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Practice problem using lambda inside the class (/thread-29614.html) |
Practice problem using lambda inside the class - jagasrik - Sep-12-2020 I am trying to check if the sum of all 3 sides of triangle is equal to 180 , if it is then it should output true else false, I am using lamba function to implement it, as i want some practice. This is what i tried i am getting output as <function __main__.Triangle.check_angles.<locals>.<lambda>>instead i should be getting True or False, i really wanna use lamba here, do let me know what i am missing, thanks a lot in advance. My code here: class Triangle(): def __init__(self, angle1, angle2, angle3): self.angle1=angle1 self.angle2=angle2 self.angle3=angle3 self.number_of_sides=3 def check_angles(self): return (lambda self:'True' if (self.angle1+self.angle2+self.angle3)==180 else 'False') cal=Triangle(45,45,90) cal.check_angles() RE: Practice problem using lambda inside the class - ndc85430 - Sep-12-2020 Of course you get that output, because check_angles returns a function, which you then need to call.Having said that, this is not really a good case for using a lambda because here all it does is complicate things. If you at least want to get used to passing functions around, then look at using lambdas with say, map , filter or even passing a function for the key argument of the sorted function (or the sort method on sequences). reduce is another example of a function that takes a function as an argument (a so-called "higher-order function"), but it might be a bit more complicated to understand how it works.
RE: Practice problem using lambda inside the class - jagasrik - Sep-12-2020 Thanks calling it again helps. Just wanted to see how it works inside class, i will take a note not to complicate it this way. RE: Practice problem using lambda inside the class - deanhystad - Sep-12-2020 Why do you return 'True' or 'False'? Shouldn't check_angles() return True or False? 'True' and 'False' are strings, not Boolean constants. If you want to return True or False, does this code look odd to you? if True: return True else: return FalseIf it does, then this should look equally odd: lambda self:True if (self.angle1+self.angle2+self.angle3)==180 else FalseBecause it does the same thing as this: lambda self:(self.angle1+self.angle2+self.angle3)==180And then there is the oddness of the check_angles function. This is a really strange place to use a lambda. Remember that the only thing lambda does is create a function. So this code: def check_angles(self): return (lambda self:'True' (self.angle1+self.angle2+self.angle3)==180is the same as this: def check_angles(self): def really_check_angles(self): return (self.angle1+self.angle2+self.angle3)==180 return really_check_anglesOf course what you really want is this: def check_angles(self): return (self.angle1+self.angle2+self.angle3)==180And to do that with lambda you end up with this horrible code: def check_angles(self): return (lambda self: (self.angle1+self.angle2+self.angle3)==180)(self)Don't use lambda unless you have to. And if you have to use lambda it may be a sign that your design is bad. |