Advertisement
elena1234

Super() and polymorphism ( Python )

Feb 5th, 2022
1,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. class User():
  2.     def __init__(self, email):
  3.         self.email = email
  4.        
  5.     def sign_in(self):
  6.         print("logged in...")
  7.        
  8.    
  9. class Wizard(User):
  10.     def __init__(self, name, power, email):
  11.         super().__init__(email)
  12.         self.name = name
  13.         self.power = power
  14.    
  15.     def attack(self):
  16.         print(f"{self.name} attacking with power of {self.power}.")
  17.        
  18.  
  19. class Archer(User):
  20.     def __init__(self, name, num_arrows):
  21.         self.name = name
  22.         self.num_arrows = num_arrows
  23.    
  24.     def attack(self):
  25.         print(f"{self.name} attacking with arrows: arrows left - {self.num_arrows}.")
  26.  
  27.  
  28. wizard1 = Wizard("Merlin", 50, "wizard@abv.bg")
  29. archer1 = Archer("Robin", 100)
  30. wizard1.sign_in()
  31. archer1.sign_in()
  32. wizard1.attack()
  33. archer1.attack()
  34. print(wizard1.email)
  35. print(isinstance(User, object)) # True
  36. print(isinstance(wizard1, object)) # True
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement