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

(andrew) #1

Importing a whole module when you need only a specific
method or function adds unneeded overhead. To help
with this, Python allows you to import specific methods
by using the from syntax. Here is an example of
importing the sqrt() and tan() methods:


Click here to view code image


>>> from math import sqrt,tan
>>> sqrt(15)
3.872983346207417

As you can see here, you can import more than one
method by separating the methods you want with
commas.


Notice that you no longer have to use math.sqrt and
can just call sqrt() as a function, since you imported
only the module functions you needed. Less typing is
always a nice side benefit.


The Python Standard Library


The Python standard library, which is automatically
installed when you load Python, has an extensive range
of prebuilt modules you can use in your applications.
Many are built in C and can make life easier for
programmers looking to solve common problems
quickly. Throughout this book, you will see many of these
modules used to interact programmatically with Cisco
infrastructure. To get a complete list of the modules in
the standard library, go to at
https://docs.python.org/3/library/. This documentation
lists the modules you can use and also describes how to
use them.


Importing Your Own Modules


As discussed in this chapter, modules are Python files
that save you time and make your code readable. To save
the class example from earlier in this chapter as a

Free download pdf