ClearCode

extra

Apr 27th, 2022
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. class Monster:
  2.     '''A monster that has some attributes'''
  3.     def __init__(self,health,energy):
  4.         self.health = health
  5.         self.energy = energy
  6.  
  7.         # private attributes
  8.         self._id = 5
  9.  
  10.     # methods
  11.     def attack(self,amount):
  12.         print('The monster has attacked!')
  13.         print(f'{amount} damage was dealt')
  14.         self.energy -= 20
  15.        
  16.     def move(self,speed):
  17.         print('The monster has moved')
  18.         print(f'It has a speed of {speed}')
  19.  
  20. monster = Monster(20,10)
  21.  
  22. # hasattr and setattr
  23. # if hasattr(monster,'health'):
  24. #   print(f'the monster has {monster.health} health')
  25.  
  26. setattr(monster,'weapon', 'Sword')
  27. # monster.weapon = 'Sword'
  28. # print(monster.weapon)
  29.  
  30. # new_attributes = (['weapon','Axe'],['armor','Shield'],['potion','mana'])
  31. # for attr,value in new_attributes:
  32. #   setattr(monster,attr,value)
  33. # print(vars(monster))
  34.  
  35. # doc
  36. # print(monster.__doc__)
  37. help(str)
Advertisement
Add Comment
Please, Sign In to add comment