Problem with method arguments


#1



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
print c.volume()
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.


#2

It shows you the line, you gave it no arguments, and self was added implicitly for a total of 1.

Also note that a cylinder has a size so you one shouldn't be telling it what size to use when asking it what it's volume is. It should instead be inspecting its own data.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.