If you come from a language such as Java maybe you can make some kind of association between self
and this
. self
will be the reference to the object that called that method but you need to declare a class first. Try:
class MyClass(object)
def __init__(self)
#equivalent of constructor, can do initialisation and stuff
def setavalue(self):
self.myname = "harry"
def printaname(self):
print "Name", self.myname
def main():
#Now since you have self as parameter you need to create an object and then call the method for that object.
my_obj = MyClass()
my_obj.setavalue() #now my_obj is passed automatically as the self parameter in your method declaration
my_obj.printname()
if __name__ == "__main__":
main()
You can try some Python basic tutorial like here: Python guide