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.
# 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
# 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}.")
Has armor: True Roger's tank's armor has a strength of 5.
# 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}.")
Has armor: True Psalm's tank's armor has a strength of 8.
Let's add an owner.
# 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
# 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}.")
Has armor: True roger's tank's armor has a strength of 5.
# 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}.")
Has armor: True psalm's armor has a strength of 8.
You can add as many functions as you need to a class.
# 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.")
phils_tank = BetterTank()
phils_tank.owner = 'phil'
phils_tank.shoot()
phil's tank is shooting.
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!
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.")
eric's tank has 5 armor. psalm's tank has 5 armor. roger's tank has 5 armor. devin's tank has 5 armor. evan's tank has 5 armor. phil's tank has 5 armor.
That's boring, everyone has the same tank!
Let's give everyone a tank with random strength.
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.")
eric's tank has 8 armor. psalm's tank has 6 armor. roger's tank has 9 armor. devin's tank has 4 armor. evan's tank has 9 armor. phil's tank has 9 armor. abraham's tank has 0 armor.
But psalm deserves the strongest tank!
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.")
eric's tank has 5 armor. psalm's tank has 100 armor. roger's tank has 4 armor. devin's tank has 6 armor. evan's tank has 2 armor. phil's tank has 3 armor. abraham's tank has 10 armor.