##### Practice problem using lambda inside the class
 Practice problem using lambda inside the class jagasrik Programmer named Tim Posts: 12 Threads: 8 Joined: Aug 2020 Reputation: Sep-12-2020, 12:34 PM 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 `.>`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()``` Reply ndc85430 Da Bishop Posts: 1,401 Threads: 1 Joined: Apr 2017 Reputation: Sep-12-2020, 01:14 PM (This post was last modified: Sep-12-2020, 01:14 PM by ndc85430.) 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. Reply jagasrik Programmer named Tim Posts: 12 Threads: 8 Joined: Aug 2020 Reputation: Sep-12-2020, 01:23 PM 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. Reply deanhystad So-and-so of the Yard Posts: 2,147 Threads: 11 Joined: Feb 2020 Reputation: Sep-12-2020, 03:18 PM 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 False```If it does, then this should look equally odd: `lambda self:True if (self.angle1+self.angle2+self.angle3)==180 else False`Because it does the same thing as this: `lambda self:(self.angle1+self.angle2+self.angle3)==180`And 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)==180```is the same as this: ```def check_angles(self): def really_check_angles(self): return (self.angle1+self.angle2+self.angle3)==180 return really_check_angles```Of course what you really want is this: ```def check_angles(self): return (self.angle1+self.angle2+self.angle3)==180```And 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. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post How to read file inside class Mekala 11 2,235 May-02-2020, 11:36 AM Last Post: snippsat [split] Python Class Problem astral_travel 12 1,653 Apr-29-2020, 07:13 PM Last Post: michael1789 Python Class Problem JPCrosby 2 872 Apr-28-2020, 06:18 PM Last Post: buran Threading best practice EvanS1 2 700 Apr-21-2020, 10:11 PM Last Post: EvanS1 Is it mandatory to call superclass init inside the class init? psolar 3 995 Feb-14-2020, 09:16 PM Last Post: wavic Class problem duckduck23 2 831 Feb-10-2020, 08:52 PM Last Post: jefsummers Class code problem from CS Dojo YouTube Dixon 3 838 Feb-04-2020, 10:23 PM Last Post: snippsat Class Problem scratchmyhead 3 977 Nov-19-2019, 08:28 AM Last Post: Larz60+ Get variable from class inside another class hcne 3 1,343 Mar-30-2019, 03:02 PM Last Post: ichabod801 problem with class method AmirAB 3 1,535 Feb-13-2019, 01:51 AM Last Post: AmirAB

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020