Reputation: 10149
Here is my code, and I should refer class method printID as student.printID(), but I by mistake refer it by student.printID, I think it should return a name Error/Exception, but the code runs without any issues, any thoughts why?
class Student:
def __init__(self, id):
self.id = id
def printID(self):
print self.id
student = Student(100)
student.printID
thanks in advance, Lin
Upvotes: 1
Views: 43
Reputation: 251458
There is no real distinction between methods and attributes in Python. Methods are attributes. student.printID
is a reference to the method object. When you add parentheses to it, you call that object. In other words, student.printID()
is no different than:
x = student.printID
x()
So it is not an error to type student.printID
without parentheses. It just gives you a reference to the method. You might want to use that reference for some other purpose. (In your particular case you didn't do anything with it, but Python doesn't know that.)
Upvotes: 1