π Creating a Class for Storing and Operating on Information#
Classes allow for both storing and operating on information.
This allows you to build scaffolding that supports information, functions, and methods in ways that can be written once and reused.
Revisiting Circuits with Classes#
As you learn more, you can start to appreciate things you have seen before in new ways. Letβs return to the examples we did last week with circuits but use a class.
Letβs build a class for a series type circuit called
SeriesCircuit
that
holds information about a circuit with resistors in series and
calculates the total resistance, current, and voltages across each resistor.
class SeriesCircuit:
# create a constructor for the SeriesCircuit class
def __init__(self):
self.components = [] # List to store resistances of the circuit components
Letβs create a method that allows the resistance of each resistor to be associated with the circuit.
class SeriesCircuit:
# create a constructor for the SeriesCircuit class
def __init__(self):
self.components = [] # List to store resistances of the circuit components
# create a method that adds a resistor to an instantiation of the SeriesCircuit class
def add_resistor(self, resistance):
"""Add a resistor to the circuit."""
self.components.append(resistance)
Letβs add a method to the class to calculate the total resistance
class SeriesCircuit:
# create a constructor for the SeriesCircuit class
def __init__(self):
self.components = [] # List to store resistances of the circuit components
# create a method that adds a resistor to an instantiation of the SeriesCircuit class
def add_resistor(self, resistance):
"""Add a resistor to the circuit."""
self.components.append(resistance)
# create a method that sums the list `components` of an instantiation of the SeriesCircuit class
def total_resistance(self):
"""Calculate the total resistance in the circuit."""
return sum(self.components)
We can also add methods that calculate the current flowing through the circuit or the voltage across each resistor.
class SeriesCircuit:
# create a constructor for the SeriesCircuit class
def __init__(self):
self.components = [] # List to store resistances of the circuit components
# create a method that adds a resistor to an instantiation of the SeriesCircuit class
def add_resistor(self, resistance):
"""Add a resistor to the circuit."""
self.components.append(resistance)
# create a method that sums the list `components` of an instantiation of the SeriesCircuit class
def total_resistance(self):
"""Calculate the total resistance in the circuit."""
return sum(self.components)
# create a method that calculates the current flowing through an instantiation of the SeriesCircuit class
def circuit_current(self, voltage):
"""Calculate the current flowing through the circuit given a voltage source."""
total_r = self.total_resistance()
if total_r > 0:
return voltage / total_r
else:
return 0
# create a method that calculates the voltage across each resistor an instantiation of the SeriesCircuit class
def component_voltages(self, voltage):
"""Calculate the voltage across each resistor in the series circuit."""
current = self.circuit_current(voltage)
return [current * r for r in self.components]
We can now use this class to revisit the circuit with 9 volts of energy and three different resistors in series that we considered with nested lists last week.
# instantiate the class
circuit = SeriesCircuit()
# use the add_resistor method to construct the circuit
circuit.add_resistor(3000) # Add a 3000 ohm resistor
circuit.add_resistor(10000) # Add a 10000 ohm resistor
circuit.add_resistor(5000) # Add a 5000 ohm resistor
# apply the total_resistance method to the instantiation of the class
total_resistance = circuit.total_resistance()
print(f"Total Resistance: {total_resistance} Ohms")
# apply the circuit_current method to the instantiation of the class
voltage_source = 9 # 9V voltage source
current = circuit.circuit_current(voltage_source)
print(f"Circuit Current: {current} A")
# apply the component_voltages method to the instantiation of the class
voltages = circuit.component_voltages(voltage_source)
print(f"Voltages across each component: {voltages}")