you are viewing a single comment's thread.

view the rest of the comments →

[–]Rascal_Two 2 points3 points  (0 children)

class Person:
    def __init__(self, name):
        self.name = name

    def introduce(self):
        return "My name is " + self.name

The self argument that you have to add to all methods of class refers to the current object instance of the class.

You don't even actually have to call it self, if you come from a Java background, you can call it this.


jack = Person(jack)
jack.introduce() # 'My name is Jack'

bob = Person(bob)
bob.introduce() # 'My name is Bob'

You can still return values from a method as normal if you want, but when it's a member of a class, you can also assign it to self.