3.4.6 Lab Explore Python Classes
3.4.6 Lab Explore Python Classes
Objectives
Part 1: Launch the DEVASC VM
Part 2: Review Functions, Methods, and Classes
Part 3: Define a Function
Part 4: Define a Class with Methods
Part 5: Review the circleClass.py Script
Background / Scenario
In this lab, you review Python methods, functions, and classes. You then create a class and instantiate it
several times with different values. Finally, you review the Circle class example used in the course.
Required Resources
• 1 PC with operating system of your choice
• Virtual Box or VMWare
• DEVASC Virtual Machine
Instructions
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 6 www.netacad.com
Lab - Explore Python Classes
# Define a method
def method1Name
...blocks of code
c. Call the function myCity passing it different values for city, as shown in the following examples.
myCity("Austin")
myCity("Tokyo")
myCity("Salzburg")
d. Save and run the myCity.py file. You should get the following output.
devasc@labvm:~/labs/devnet-src/python$ python3 myCity.py
I live in Austin.
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 6 www.netacad.com
Lab - Explore Python Classes
I live in Tokyo.
I live in Salzburg.
devasc@labvm:~/labs/devnet-src/python$
Step 1: Define and then instantiate a class with the __init__() method.
A Python class is used to create objects that have properties and methods. All Python classes typically
include an explicitly defined __init__() function, although you can create a class without defining one. The
__init__() function is always initiated when a class is instantiated. Instantiating a class creates a copy of the
class which inherits all the class variables and methods.
Note: Although it is sometimes called the __init__() function, it is dependent on the class. Therefore, it is
technically a method.
a. Open a new text file and save it as myLocation.py.
b. Define a class with the name Location and press Enter. If you are working is VS Code, then the text
editor should automatically indent four spaces.
class Location:
|<-- cursor should now be here
c. Next, define the __init__() function. By convention, the first parameter is called self. The self parameter
is a reference to the current instance of the class itself and is used to access variables that belong to the
entire class. The __init__() function is then assigned any variables the entire class needs. In the following
example, define a name and country variable. Press Enter twice and then backspace twice to the left
margin.
def __init__(self, name, country):
self.name = name
self.country = country
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 6 www.netacad.com
Lab - Explore Python Classes
Your_Country
<class '__main__.Location'>
devasc@labvm:~/labs/devnet-src/python$
Step 3: Instantiate the Location class multiple times and call the myLocation method.
Now that you have a class, you can instantiate it as many times as you like providing different values for the
class variables each time.
a. Add the following code to your myLocation.py script to instantiate Location class and call the method.
You do not need to add the comments.
# First instantiation of the class Location
loc1 = Location("Tomas", "Portugal")
# Call a method from the instantiated class
loc1.myLocation()
b. Save and run your script. You should get the following output.
devasc@labvm:~/labs/devnet-src/python$ python3 myLocation.py
Hi, my name is Tomas and I live in Portugal.
devasc@labvm:~/labs/devnet-src/python$
c. Add two more instantiations and then a fourth one where you specify the name and values for your_loc.
loc2 = Location("Ying", "China")
loc3 = Location("Amare", "Kenya")
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 6 www.netacad.com
Lab - Explore Python Classes
loc2.myLocation()
loc3.myLocation()
your_loc = Location("Your_Name", "Your_Country")
your_loc.myLocation()
d. Save and run your script. You should get the following output.
devasc@labvm:~/labs/devnet-src/python$ python3 myLocation.py
Hi, my name is Tomas and I live in Portugal.
Hi, my name is Ying and I live in China.
Hi, my name is Amare and I live in Kenya.
Hi, my name is Your_Name and I live in Your_Country.
devasc@labvm:~/labs/devnet-src/python$
def myLocation(self):
print("Hi, my name is " + self.name + " and I live in " +
self.country + ".")
# Three more instantiations and method calls for the Location class
loc2 = Location("Ying", "China")
loc3 = Location("Amare", "Kenya")
loc2.myLocation()
loc3.myLocation()
your_loc = Location("Your_Name", "Your_Country")
your_loc.myLocation()
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 6 www.netacad.com
Lab - Explore Python Classes
• The circumference method calculates the circumference and returns the value storing it in the
circumferenceValue variable.
• The printCircumference method prints a string. Notice that the variables are casted as strings with the
str() function. Otherwise, the print statement would throw an error because self.radius and
myCircumference are not strings.
• The Circle class instantiated three times.
# Given a radius value, print the circumference of a circle.
# Formula for a circumference is c = pi * 2 * radius
class Circle:
def circumference(self):
pi = 3.14
circumferenceValue = pi * self.radius * 2
return circumferenceValue
def printCircumference(self):
myCircumference = self.circumference()
print ("Circumference of a circle with a radius of " + str(self.radius)
+ " is " + str(myCircumference))
# Two more instantiations and method calls for the Circle class.
circle2 = Circle(5)
circle2.printCircumference()
circle3 = Circle(7)
circle3.printCircumference()
End of document
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 6 www.netacad.com