Lesson 10.3
A method is a function inside a class: an action of the object.
class Hero:
def __init__(self, name, hp):
self.name = name
self.hp = hp
def heal(self):
... # add to the HP
def info(self):
... # print the name and the HP
hero = Hero("Anna", 30)
hero.info()
hero.heal()
hero.info()
def inside the class, and the first parameter is always self;hero.heal(), without brackets the method doesn't run;self: self.hp += 20 saves the result, while
self.hp + 20 without = throws the result away.So there is a pair: data in __init__, actions in methods.
The check gives name, hp and a list of actions: "heal" — heal by points,
"info" — print. Run them in order.
A method is a function inside a class, and its first parameter is self.
heal adds to self.hp, don't forget the =: self.hp += points.
info only prints. Methods are called with a dot, with brackets.