This chapter covers the following topics:
Python Functions: This section provides an overview of working with and building Python functions.
Object-Oriented Programming and Python: This section describes key aspects of using object-oriented programming techniques.
Python Classes: This section provides an overview of creating and using Python classes.
Working with Python Modules: This section provides an overview of creating and using Python modules.
This chapter moves away from the basics introduced in Chapter 3, “Introduction to Python,” and introduces Python functions, classes, and modules. Building Python functions allows for the creation of reusable code and is the first step toward writing object-oriented code. Classes are the Python tools used to construct Python objects and make it easier to produce scalable applications that are easy to maintain and readable. Finally, this chapter introduces the wide world of Python modules and how they can extend the capabilities of Python and make your job of coding much easier.
“Do I Know This Already?” Quiz
The “Do I Know This Already?” quiz allows you to assess whether you should read this entire chapter thoroughly or jump to the “Exam Preparation Tasks” section. If you are in doubt about your answers to these questions or your own assessment of your knowledge of the topics, read the entire chapter. Table 4-1 lists the major headings in this chapter and their corresponding “Do I Know This Already?” quiz questions. You can find the answers in Appendix A, “Answers to the ‘Do I Know This Already?’ Quiz Questions.”
Table 4-1 “Do I Know This Already?” Section-to-Question Mapping
Foundation Topics Section |
Questions |
Python Functions |
1–3 |
Object-Oriented Programming and Python |
4–5 |
Python Classes |
6–8 |
Working with Python Modules |
9–10 |
Which of the following is the correct syntax for a Python function?
define function (arg):
function function(arg);
def function(arg):
func function(arg):
Which of the following is a valid Python function name?
1function
__init__
True
Funct1on
When three single quotation marks are used on the next line directly after defining a function, what does this indicate?
Multi-line text
A docstring
A string value including double or single quotation marks
None of the above
What are key components of object-oriented programming in Python? (Choose two.)
Functions that can be performed on a data structure
Attributes that are stored in an object
Configuration templates
YAML files
Which of the following are benefits of OOP? (Choose all that apply.)
Reusable code
Easy to follow
Low coupling/high cohesion
Complex integration
Which of the following are used to define a class in Python? (Choose two.)
class classname(parent):
class classname:
def class classname(arg):
None of the above
What is a method?
A variable applied to a class
Syntax notation
A function within a class or an object
Something that is not used in a class
Which of the following describes inheritance?
A hierarchy for functions in Python
Class attributes and methods used as the starting point for another class
A function only applied to methods being used in another class
None of the above
Which module provides access to the file system and directory structure?
filesystem
open
system
os
Which module is a testing framework for Cisco infrastructure?
pyATS
pyang
devnetats
ncclient