DevAsc – Python Classes
Python classes are very useful when you need to create objects with the same characteristics. This is often referred to as Object Oriented Programming (OOP). Not having much of a programming background, I found classes to be a bit confusing, and I wasn’t fully understanding the use of __init__ and self. Thanks to the Twitter community, my friend Peter Palúch , and the videos of Cory Schafer, I know feel I have a better understanding, and wanted to share my findings, from a networking person’s perspective.
First, let’s look at why classes are needed in the first case. Let’s say that we want to keep track of our network devices. The attributes we are interested in are:
- Hostname
- Vendor
- Device type
- Model
- Loopback
We can of course create this information manually, without classes, like this:
daniel@devasc:~/DevAsc$ python3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> device1_hostname = "r1" >>> device1_vendor = "Cisco" >>> device1_type = "router" >>> device1_model = "ISR4331" >>> device1_loopback = "192.0.2.1" >>> device2_hostname = "sw1" >>> device2_vendor = "Cisco" >>> device2_type = "switch" >>> device2_model = Continue reading
