0

Why is output 28?? (edited)

class Geom(object): def __init__(self, h = 10, w = 10): self.h, self.w = h, w def rectangle(self): rect = self.h * 2 + self.w * 2 return rect a = Geom(4) print(a.rectangle())

8th Feb 2022, 4:56 PM
cryptic blake
cryptic blake - avatar
11 Respostas
+ 3
Cryptic Blake, As you can see in the __init__ magic method, there are 3 parameters <self>, <h> and <w>. But for <h> and <w>, there are default parameter values specified (both set as 10). When you create a Geom object, you passed only one argument (4) for __init__(). That value will be used for parameter <h>, and since there's only one argument were supplied, <w> will use the default parameter value (10) So in rectangle() method, self.<h> will be (4) and self.<w> will be (10). self.h * 2 + self.w * 2 (4) * 2 + (10) * 2 8 + 20 It goes like that I guess ...
8th Feb 2022, 5:44 PM
Ipang
+ 3
cryptic blake a = Geom(4) Here a is an instance of class Geom and class Geom has constructor which assign value to h and w Since Geom has parameter value 4 which will be assign to h but there is no second parameter so 10 (which is default value) will be assign to w Now rectangle function is called by instance variable a So self.h * 2 + self.w * 2 Since self.h = 4 and self.w = 10 So finally 4 * 2 + 10 * 2 = 28
8th Feb 2022, 5:48 PM
AÍąJ
AÍąJ - avatar
+ 1
Yes. self.h = 4, self.w = 10 Rect = 4*2 + 10*2 => 8+20 => 28 Returns 28
8th Feb 2022, 5:10 PM
Jayakrishna 🇼🇳
+ 1
Ipang I've corrected it thanks🙂
8th Feb 2022, 5:27 PM
cryptic blake
cryptic blake - avatar
+ 1
Cryptic Blake, It should work now after the correction ...
8th Feb 2022, 5:29 PM
Ipang
+ 1
Ipang Can you explain to me why output is 28?
8th Feb 2022, 5:33 PM
cryptic blake
cryptic blake - avatar
+ 1
cryptic blake __init__ constructor assigning default values h=10,w=10 a=Geom(4) here, You are passing 4 so it override h value so h=4, but not given value w so remain w=10. so values now are h=4 , w=10 Remaining in my first post. Also by others.... try a=Geom(4, 5) then output is 4*2+5*2=18
8th Feb 2022, 6:32 PM
Jayakrishna 🇼🇳
0
Cryptic Blake, Magic method has a couple of underscores wrapping the name. __init__ # <-- like this ... Your code had just one underscore wrapping the name ... * Code had been edited ...
8th Feb 2022, 5:24 PM
Ipang
0
8th Feb 2022, 5:26 PM
cryptic blake
cryptic blake - avatar
0
a=Geom(4) python does position matching. So the 4 passed is used to replace h which is the first parameter so h=4 while w remains unchanged as w=10
10th Feb 2022, 1:10 AM
okonkwo calistus
okonkwo calistus - avatar
0
Hello everyone, By default you defined the values of h and w each 10. After you made the instance of the class ( a = Geom(4) ) you changed the value of h from 10 to 4, but you didnt change the value of w, which will remain 10. So when you call the method ( a.rectangel() ) this method returns rect and rect = 4 * 2 + 10 * 2 When you print it (print(rect) ) the answer is 28
11th Feb 2022, 1:13 PM
Ferdaws Kukcha
Ferdaws Kukcha - avatar