DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

There are two routers instantiated in this example: rtr1
and rtr2. Using the print function, you can call the
getdesc() method to return formatted text about the
object’s attributes. The following output would be
displayed:


Click here to view code image


Rtr1
Router Model :iosV
Software Version :15.6.7
Router Management Address:10.10.10.1

Rtr2
Router Model :isr4221
Software Version :16.9.5
Router Management Address:10.10.10.5

Inheritance


Inheritance in Python classes allows a child class to take
on attributes and methods of another class. In the
previous section, Example 4-1 creates a class for routers,
but what about switches? If you look at the Router
class, you see that all of the attributes apply to a switch
as well, so why not reuse the code already written for a
new Switch class? The only part of Example 4-1 that
wouldn’t work for a switch is the getdesc() method,
which prints information about a router. When you use
inheritance, you can replace methods and attributes that
need to be different. To inherit in a class, you create the
class as shown earlier in this chapter, but before the
colon, you add parentheses that include the class from
which you want to pull attributes and methods. It is
important to note that the parent class must come before

Free download pdf