Note that the basis is explained here given that the reader has no experience with OOP. Some examples are not very correct from Python’s ideology point of view, but they help to better understand how it works. At the end, an explanation is given of how this should be done in proper way.
class is used in python to create classes. The easiest class you can create in Python:
In : class Switch: ...: pass ...:
Class names: usually class names in Python are written in CamelCase format.
To create a class instance, call class:
In : sw1 = Switch() In : print(sw1) <__main__.Switch object at 0xb44963ac>
Using dot notation, it is possible to get values of instance variables, create new variables and assign a new value to existing ones:
In : sw1.hostname = 'sw1' In : sw1.model = 'Cisco 3850'
In another instance of Switch class, the variables may be different:
In : sw2 = Switch() In : sw2.hostname = 'sw2' In : sw2.model = 'Cisco 3750'
You can see value of instance variables using the same dot notation:
In : sw1.model Out: 'Cisco 3850' In : sw2.model Out: 'Cisco 3750'