TypeError: must be real number, not Circle
TypeError: must be real number, not Circle
import math as m
class Circle:
def __init__(self,radius):
self.radius = radius
count = 0
def area(r):
k = m.pi * m.pow(r,2)
return k
I am not sure what wrong with the code above but when I am trying execute like below
a = Circle(4)
a.area()
it throws an error like
" k = m.pi * m.pow(r,2)
TypeError: must be real number, not Circle"
Unable to understand how it's considering the float value as an instance to the circle.
import math as m
Just for convenience to use m. instead of math.
– Mahesh
Jun 30 at 11:15
2 Answers
2
The first argument of an instance method is a reference to the instance itself (usually called self) - that's where the Circe is coming from. You shouldn't be passing the radius, you should be using the member you have:
self
def area(self):
k = m.pi * m.pow(self.radius, 2)
return k
import math as m
DEFAULT_RADIUS = 0.5
class Circle(object):
def __init__(self, radius=DEFAULT_RADIUS):
self.radius = radius
count = 0
def area(self, r=None):
if r is not None:
self.radius = r
k = m.pi * m.pow(self.radius, 2)
return k
a = Circle(4)
a.area()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why
import math as m?– Reblochon Masque
Jun 30 at 8:28