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

(andrew) #1

and become actions that you can perform on the object
you are creating. To store attributes, you map the name
self and the values you pass to it become variables inside
the object, which then store those values as attributes.
The last bit of code instantiates the object itself. Up until
now, you have been creating a template, and by assigning
data to the variables within the class, you have been
telling Python to build the object. Now you can access
any of the stored attributes of the class by using dot
notation, as shown here:


>>> rtr1.model
'iosV'

When you call rtr1.model, the interpreter displays the
value assigned to the variable model within the object.
You can also create more attributes that aren’t defined
during initialization, as shown in this example:


Click here to view code image


>>> rtr1.desc = 'virtual router'
>>> rtr1.desc
'virtual router'

This example shows how flexible objects are, but you
typically want to define any attributes as part of a class to
automate object creation instead of manually assigning
values. When building a class, you can instantiate as
many objects as you want by just providing a new
variable and passing over some data. Here is another
example of creating a second router object rtr2:


Click here to view code image


>>> rtr2= Router('isr4221', '16.9.5',
'10.10.10.5')
>>> rtr2.model
'isr4221'
Free download pdf