I was trying to create a class in a problem set online, where the proper syntax slipped my mind and i ran the code below.
class Cylinder(object): def __init__(self, height=1, radius=1): self.height=height self.radius=radius def volume(height, radius): return ((3.14*(radius)**2)*height) def surface_area(height, radius): return (((3.14*2*radius)*height)+2*3.14*radius**2) c=Cylinder(2,3) print c.volume() print c.surface_area().
This produced the following error :-
Traceback (most recent call last):
File "variable.py", line 40, in
TypeError: volume() takes exactly 2 arguments (1 given)
later on I corrected the code by replacing arguments with (self) and other some other tweaks but this piece of code was stuck in my mind.
Here's what I want to know :-
1) Why is the error saying I passed in one argument while I clearly passed two (height and radius).
2) What is fundamentally wrong with the code. By proper observation we can conclude that using self as argument also does the same task(that is fetch the height and radius from init method), while here I pass these two arguments directly.