Developers created classes to organize their code in a logical way.
Imagine you store a switch in a dictionary, then you write functions to change it:
switch = { "name": "SW1", "ip": "10.0.0.2", "vlans": [1, 10, 20] } def add_vlan(switch, vlan_id): switch["vlans"].append(vlan_id) def remove_vlan(switch, vlan_id): switch["vlans"].remove(vlan_id)A class puts the values and the functions of the switch in one place:

Figure 1 - A Class Holds the Values and the Functions of a Switch
A class is a new type that you create, like
intfor numbers orstrfor text.In the lab, the script prints the type of a number and the type of a text.
Print the type ofsw1to get the flag.PS: look at the output of the script. The types
intandstrare classes too. In Python, every type is a class :)Answer the question below
Enter the flag you obtained.
To use a class, you create an object, a value of this new type.
You write the name of the class, then the values of the switch between the parentheses:
Figure 2 - Two Objects from the Same Class
The class is the model, and each object keeps its own values.
In the lab, create an object named
corefor the switch CORE1 with the address 10.0.0.1.
Printcore.ipto get the flag.Answer the question below
Enter the flag you obtained.
The values of an object are its attributes and its functions are its methods.
You reach both with a dot:
Figure 3 - An Attribute and a Method of an Object
An attribute has a type that you already know.
Heresw1.vlansis a list, so you use it like any list:sw1 = Switch("SW1", "10.0.0.2") sw1.add_vlan(30) sw1.add_vlan(40) print(sw1.vlans) # Output: [1, 30, 40] print(len(sw1.vlans)) # Output: 3In the lab, the object
sw1has the VLANs 1, 10 and 20.
Remove VLAN 10, add VLAN 60 and print the attributevlansto get the flag.Answer the question below
Enter the flag you obtained.
Well done, you used the class
Switchwithout knowing how it is built.
Let's read it step by step:
Figure 4 - The Parts of a Class
The class Line
A class starts with the word
class, then its name and a colon:class Switch: def __init__(self, name, ip): self.name = name self.ip = ip self.vlans = [1] def add_vlan(self, vlan_id): self.vlans.append(vlan_id)The __init__ Method
The method
__init__runs each time you create an object.
Its parameters get the values that you give to the class:class Switch: def __init__(self, name, ip): self.name = name self.ip = ip self.vlans = [1] def add_vlan(self, vlan_id): self.vlans.append(vlan_id)With
Switch("SW1", "10.0.0.2"), the parameternameholds"SW1"andipholds"10.0.0.2".The Attributes
selfis the object that is created.
A line that starts withself.gives an attribute to this object:class Switch: def __init__(self, name, ip): self.name = name self.ip = ip self.vlans = [1] def add_vlan(self, vlan_id): self.vlans.append(vlan_id)Every new switch starts with VLAN 1, the default VLAN.
The Methods
A method is a function written inside the class.
It takesselffirst to reach the attributes of the object:class Switch: def __init__(self, name, ip): self.name = name self.ip = ip self.vlans = [1] def add_vlan(self, vlan_id): self.vlans.append(vlan_id)When you call
sw1.add_vlan(30), Python putssw1inself, so you only write the VLAN number.In the lab, add a method
summary()to the classSwitchthat returns the name and the address in the formatSW1 (10.0.0.2).
Print the summary ofsw1andsw2to get the flag.Answer the question below
Enter the flag you obtained.