Saturday, August 26, 2017

Python: calling a class/method with parenthesis vs. no-parenthesis? whats the diff?

calling a class/method with parenthesis vs. no-parenthesis? whats the diff?



>>> class Service:
secret = "He is a alian."
def setname(self, name):
self.name=name
def sum(self,a,b):
print "{}, {}+{} = {}".format(self.name,a,b,a+b)

code source: https://wikidocs.net/28


aman = Service()
awoman = Service

>>> type(aman)
<type 'instance'>
>>> type(awoman)
<type 'classobj'>
>>> 

classobj can assign a instance.

>>> kim=awoman()
>>> type(kim)
<type 'instance'>

>>> aman.setname("Strong")
>>> awoman.setname("Beauty")

Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    awoman.setname("Beauty")
TypeError: unbound method setname() must be called with Service instance as first argument (got str instance instead)
>>> 


No comments:

Post a Comment