I'm trying to understand the self
keyword in python classes. Ergo I made up a simple class:
class Question(object):
"""A class for answering my OO questions about python.
attributes: a_number"""
def __init__():
print "You've initialized a Question!"
def myself_add(self, new_number):
self.a_number += new_number
And in a __main__
function below the class definition I'm running the code
my_q = Question
print my_q
my_q.a_number = 12
print 'A number:',my_q.a_number
my_q.myself_add(3)
print 'A number:',my_q.a_number
The result I'm getting (including error) is
<class '__main__.Question'>
A number: 12
Traceback (most recent call last):
File "question.py", line 21, in <module>
my_q.myself_add(3)
TypeError: unbound method myself_add() must be called with Question instance as first argument (got int instance instead)
I'm trying to understand why the method myself_add
is considered unbound. What does that mean? And why doesn't it know that I'm calling it on an instance of the Question
class? Also, why doesn't my __init__
print happen?