Classes and Objects (Part I)¶

Outline¶

  • Reminders
  • Programmer-Defined Types
  • Attributes
  • Rectangles
  • Returning Instances

In [1]:
# import packages
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns; sns.set()

Important Notes/Reminders ¶

  • Amazon Web Services (AWS) will be on campus on Nov. 18th. Be sure to register here. We will not have office hours or class in the normal room that day. You will be required to attend at least one session in the afternoon.
  • Fresno State's Department of Math Day (DMD) will be hosting Math Jeopardy. If you are interested, go here.
  • Math Club will have tutoring this Friday from 4PM - 6PM.
  • Student Research Opportunities.

Practice!

  • If you wanted to represent the point $(x,y)$ in Python, how many ways can you do so?
In [25]:
# 1) declare them explicitly
x = 0
y = 0
# 2) in a function
# 3) tuple
(0,0)
Out[25]:
(0, 0)

Programmer-defined Types ¶

A program-defined type is also called a class . A class definition looks like this:

In [26]:
class Point:
    """Represents a point in 2-D space."""

Defining a class named Point creates a class object. Let's assign a point to a variable, called blank.

In [28]:
blank = Point()
In [29]:
blank
Out[29]:
<__main__.Point at 0x1a23cb3240>

The return value is a reference to a Point object, which we assign to blank.

Creating a new object is called instantiation , and the object is an instance of the class.

Attributes ¶

Just as we could select variables (and functions) from loaded modules/packages, we can assign values to an instance by doing the following

In [30]:
blank.x = 3.0
blank.y = 4.0

Named elements of an object that are assigned values are known as attributes. In other words, they belong to the object.

Practice!

  • Assign a new variable x the value of blank.x. Discuss with a partner why you don't think there is a conflict.
  • Can you do normal math operations with the attributes of an object?
In [32]:
blank.x ** 2
Out[32]:
9.0

You can even pass objects into new functions, where you access those attributes!

In [36]:
print( '%f' % blank.x )
3.000000
In [37]:
def print_point(p):
    print('(%g, %g)' % (p.x, p.y))
In [41]:
print_point(blank)
(3, 4)

Practice!

  • As an exercise, write a function called distance_between_points that takes two Points as arguments and returns the distance between them.
In [49]:
chicken = Point()
chicken.x = 1
chicken.y = 0

nugget = Point()
nugget.x = 4
nugget.y = 4

def dbp(p1, p2):
    return ((p1.x - p2.x)**2 + (p1.y - p2.y)**2)**0.5
In [48]:
dbp(nugget, chicken)
Out[48]:
5.0

Rectangles ¶

If you wanted to create a class for Rectangles, you could do so by

• Specifying one corner of the rectangle (or the center), the width, and the height. Let's do that now

In [50]:
class Rectangle:
    """Represents a rectangle.
    attributes: width, height, (lower left) corner.
    """
In [52]:
# create an instance of a rectangle
box = Rectangle()
# assign width and height to box
box.width = 100.0
box.height = 200.0
# assign a corner as an instance of a point
box.corner = Point()
# assing x, y to the the corner of the box
box.corner.x = 0.0
box.corner.y = 0.0

An object that is an attribute of another object is embedded.

Instances as return values ¶

Functions can return instances. For example, find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle:

In [53]:
def find_center(rect):
    p = Point()
    p.x = rect.corner.x + rect.width/2
    p.y = rect.corner.y + rect.height/2
    return p
In [55]:
temp = find_center(box)
In [58]:
temp.y
Out[58]:
100.0