#!/usr/bin/env python # coding: utf-8 # Classes # === # # Classes sound and look complicated because you can use them in such a huge variety of ways. But at their heart, they just allow you to **combine information and behavior**. # # You can represent just about anything from the real world, both physical things and concepts. # In[22]: # Example: let's represent a tank. class Tank: """This will represent a tank in a game.""" def __init__(self): # Any function starting with two underscores is special in python. # This function runs automaticaly. # self refers to the tank that's being created. # All tanks start with armor. self.has_armor = True self.armor_strength = 5 # In[23]: # Let's make a tank! rogers_tank = Tank() print(f"Has armor: {rogers_tank.has_armor}") print(f"Roger's tank's armor has a strength of {rogers_tank.armor_strength}.") # In[24]: # Let's make another tank! psalms_tank = Tank() psalms_tank.armor_strength = 8 print(f"Has armor: {psalms_tank.has_armor}") print(f"Psalm's tank's armor has a strength of {psalms_tank.armor_strength}.") # Let's add an owner. # In[ ]: # Example: let's represent a tank. class Tank: """This will represent a tank in a game.""" def __init__(self): # Any function starting with two underscores is special in python. # This function runs automaticaly. # self refers to the tank that's being created. # All tanks start with armor. self.has_armor = True self.armor_strength = 5 # Each tank needs an owner! self.owner = None # In[25]: # Let's make a tank! rogers_tank = Tank() rogers_tank.owner = 'roger' print(f"Has armor: {rogers_tank.has_armor}") print(f"{rogers_tank.owner}'s tank's armor has a strength of {rogers_tank.armor_strength}.") # In[26]: # Let's make another tank! psalms_tank = Tank() psalms_tank.armor_strength = 8 psalms_tank.owner = 'psalm' print(f"Has armor: {psalms_tank.has_armor}") print(f"{psalms_tank.owner}'s armor has a strength of {psalms_tank.armor_strength}.") # What about behavior? # --- # # You can add as many functions as you need to a class. # In[20]: # Example: let's represent a tank. class BetterTank: """This will represent a tank in a game.""" def __init__(self): # Any function starting with two underscores is special in python. # This function runs automaticaly. # self refers to the tank that's being created. # All tanks start with armor. self.has_armor = True self.armor_strength = 5 # Each tank needs an owner! self.owner = None def shoot(self): """This tank can shoot.""" # This is our own function, so it doesn't get double underscores. print(f"{self.owner}'s tank is shooting.") # In[21]: phils_tank = BetterTank() phils_tank.owner = 'phil' phils_tank.shoot() # An army of tanks! # --- # # Once you have a class defined, you can make as many objects as you want from that class. Let's make an army of tanks! # In[30]: names = ['eric', 'psalm', 'roger', 'devin', 'evan', 'phil'] # Make an empty list to store our tanks. tanks = [] # Create a tank for every person. for name in names: new_tank = Tank() new_tank.owner = name tanks.append(new_tank) # Show that we've made an army of tanks. for tank in tanks: print(f"{tank.owner}'s tank has {tank.armor_strength} armor.") # That's boring, everyone has the same tank! # # Let's give everyone a tank with random strength. # In[35]: from random import randint names = ['eric', 'psalm', 'roger', 'devin', 'evan', 'phil', 'abraham'] # Make an empty list to store our tanks. tanks = [] # Create a tank for every person. for name in names: new_tank = Tank() new_tank.owner = name # Give everyone a random armor strength. new_tank.armor_strength = randint(0, 10) tanks.append(new_tank) # Show that we've made an army of tanks. for tank in tanks: print(f"{tank.owner}'s tank has {tank.armor_strength} armor.") # But psalm deserves the strongest tank! # In[44]: from random import randint names = ['eric', 'psalm', 'roger', 'devin', 'evan', 'phil', 'abraham'] # Make an empty list to store our tanks. tanks = [] # Create a tank for every person. for name in names: new_tank = Tank() new_tank.owner = name # Give everyone except psalm a random armor strength. if name == 'psalm': new_tank.armor_strength = 100 else: new_tank.armor_strength = randint(0, 11) tanks.append(new_tank) # Show that we've made an army of tanks. for tank in tanks: print(f"{tank.owner}'s tank has {tank.armor_strength} armor.")