The Python tutorials are written as Jupyter notebooks and run directly in Google Colab—a hosted notebook environment that requires no setup. Click the Run in Google Colab button.
Colab link - Open colab
# Import the library
import matplotlib.pyplot as plt
%matplotlib inline
The first step in creating your own class is to use the class
keyword, then the name of the class as shown in Figure 4. In this course the class parent will always be object:
__init__
, which is used to initialize the object. The input are data attributes. The term self
contains all the attributes in the set. For example the self.color
gives the value of the attribute color and self.radius
will give you the radius of the object. We also have the method add_radius()
with the parameter r
, the method adds the value of r
to the attribute radius. To access the radius we use the syntax self.radius
. The labeled syntax is summarized in Figure 5:
drawCircle
to display the image of a circle. We set the default radius to 3 and the default colour to blue:
# Create a class Circle
class Circle(object):
# Constructor
def __init__(self, radius=3, color='blue'):
self.radius = radius
self.color = color
# Method
def add_radius(self, r):
self.radius = self.radius + r
return(self.radius)
# Method
def drawCircle(self):
plt.gca().add_patch(plt.Circle((0, 0), radius=self.radius, fc=self.color))
plt.axis('scaled')
plt.show()
RedCircle
of type Circle to do the following:
# Create an object RedCircle
RedCircle = Circle(10, 'red')
We can use the dir
command to get a list of the object's methods. Many of them are default Python methods.
# Find out the methods can be used on the object RedCircle
dir(RedCircle)
We can look at the data attributes of the object:
# Print the object attribute radius
RedCircle.radius
# Print the object attribute color
RedCircle.color
We can change the object's data attributes:
# Set the object attribute radius
RedCircle.radius = 1
RedCircle.radius
We can draw the object by using the method drawCircle()
:
# Call the method drawCircle
RedCircle.drawCircle()
We can increase the radius of the circle by applying the method add_radius()
. Let increases the radius by 2 and then by 5:
# Use method to change the object attribute radius
print('Radius of object:',RedCircle.radius)
RedCircle.add_radius(2)
print('Radius of object of after applying the method add_radius(2):',RedCircle.radius)
RedCircle.add_radius(5)
print('Radius of object of after applying the method add_radius(5):',RedCircle.radius)
Let’s create a blue circle. As the default colour is blue, all we have to do is specify what the radius is:
# Create a blue circle with a given radius
BlueCircle = Circle(radius=100)
As before we can access the attributes of the instance of the class by using the dot notation:
# Print the object attribute radius
BlueCircle.radius
# Print the object attribute color
BlueCircle.color
We can draw the object by using the method drawCircle()
:
# Call the method drawCircle
BlueCircle.drawCircle()
Compare the x and y axis of the figure to the figure for RedCircle
; they are different.
# Create a new Rectangle class for creating a rectangle object
class Rectangle(object):
# Constructor
def __init__(self, width=2, height=3, color='r'):
self.height = height
self.width = width
self.color = color
# Method
def drawRectangle(self):
plt.gca().add_patch(plt.Rectangle((0, 0), self.width, self.height ,fc=self.color))
plt.axis('scaled')
plt.show()
Let’s create the object SkinnyBlueRectangle
of type Rectangle. Its width will be 2 and height will be 3, and the color will be blue:
# Create a new object rectangle
SkinnyBlueRectangle = Rectangle(2, 10, 'blue')
As before we can access the attributes of the instance of the class by using the dot notation:
# Print the object attribute height
SkinnyBlueRectangle.height
# Print the object attribute width
SkinnyBlueRectangle.width
# Print the object attribute color
SkinnyBlueRectangle.color
We can draw the object:
# Use the drawRectangle method to draw the shape
SkinnyBlueRectangle.drawRectangle()
Let’s create the object FatYellowRectangle
of type Rectangle :
# Create a new object rectangle
FatYellowRectangle = Rectangle(20, 5, 'yellow')
We can access the attributes of the instance of the class by using the dot notation:
# Print the object attribute height
FatYellowRectangle.height
# Print the object attribute width
FatYellowRectangle.width
# Print the object attribute color
FatYellowRectangle.color
We can draw the object:
# Use the drawRectangle method to draw the shape
FatYellowRectangle.drawRectangle()