Why does this method not return a value?
Question:
I have the following code:
class Thing:
def __init__(self):
self.a = 30
self.b = 10
def sumit(self):
return self.a + self.b
giventhing = Thing
print(giventhing.sumit/2)
I get this error:
TypeError: unsupported operand type(s) for /: 'function and 'int'
Answers:
sumit
is a function: you need to call it with brackets: print(giventhing.sumit()/2)
There are two issues here:
sumit
is an instance method, so you need to call it on an instance, not a class (or type).
- To execute callables, such as methods, you need to use the propert syntax, which is
method()
, note the () at the end.
Doing giventhing = Thing
won’t give you an instance, it will give you a reference to the class/type itself, which is only useful if you want to operate with class members, which is not your use case.
Doing giventhing.sumit / 2
, won’t divide the result of sumit
by 2. In fact, giventhing.sumit
will yield a reference to the function itself, not its result. You need to call the function in order to get its return value, i.e. sumit()
Fixed code:
giventhing = Thing() # You need an instance of Thing
print(giventhing.sumit() / 2) # You need to actually call sumit on the instance
Functions are type of function you need to first call it
So need:
giventhing = Thing()
print(giventhing.sumit()/2)
So totally need parentheses
Here’s an example:
>>> class A:
def __init__(self,a):
self.a=a
def out(self):
return self.a
>>> A
<class '__main__.A'>
>>> a=A(1)
>>> a.out
<bound method A.out of <__main__.A object at 0x0000005F9D177EB8>>
>>> a.out()
1
>>>
I have the following code:
class Thing:
def __init__(self):
self.a = 30
self.b = 10
def sumit(self):
return self.a + self.b
giventhing = Thing
print(giventhing.sumit/2)
I get this error:
TypeError: unsupported operand type(s) for /: 'function and 'int'
sumit
is a function: you need to call it with brackets: print(giventhing.sumit()/2)
There are two issues here:
sumit
is an instance method, so you need to call it on an instance, not a class (or type).- To execute callables, such as methods, you need to use the propert syntax, which is
method()
, note the () at the end.
Doing giventhing = Thing
won’t give you an instance, it will give you a reference to the class/type itself, which is only useful if you want to operate with class members, which is not your use case.
Doing giventhing.sumit / 2
, won’t divide the result of sumit
by 2. In fact, giventhing.sumit
will yield a reference to the function itself, not its result. You need to call the function in order to get its return value, i.e. sumit()
Fixed code:
giventhing = Thing() # You need an instance of Thing
print(giventhing.sumit() / 2) # You need to actually call sumit on the instance
Functions are type of function you need to first call it
So need:
giventhing = Thing()
print(giventhing.sumit()/2)
So totally need parentheses
Here’s an example:
>>> class A:
def __init__(self,a):
self.a=a
def out(self):
return self.a
>>> A
<class '__main__.A'>
>>> a=A(1)
>>> a.out
<bound method A.out of <__main__.A object at 0x0000005F9D177EB8>>
>>> a.out()
1
>>>