Classes and Objects in Python
In this post, you will learn how to work with classes and objects so that you can use that same knowledge and thus evolve.
- a class represents a set of objects, and defines their behavior through methods and which states it is able to maintain through its attributes;
- an object is an instance of a class and is capable of storing states through its attributes and reacting to messages sent to it.
class Enemy: lives = 5 def attack(self): print('I was attacked and lost a live.') self.lives -= 1 def check Life(self): if self.lives <= 0: print('I\'m dead because I don\'t have any more lives.') else: print('I\'m still in combat and have', self.lives, 'lives.') enemy1 = Enemy() enemy2 = enemy() enemy1.attack() # I was attacked and lost a live. enemy1.attack() # I was attacked and lost a live. enemy1.attack() # I was attacked and lost a live. enemy1.checkLife() # I'm still in combat and have 2 lives. enemy2.checkLife() # I'm still in combat and have 5 lives.