Department of Mathematics

Math 300: Mathematical Computing

Python Classes

Python is a so-called object oriented language. We have no intention here of discussing object orientation and classes in detail, but if you can understand just a tiny bit about classes and objects generated from them, then much of the functionality we will see later will make more sense. Thus, please understand this page not as a tutorial in how to create a class, but rather as an illustration of how variables and methods are associated with Python objects.

Let's just look at a simple class, and spend some time talking about it. This class abstracts a circle, and provides a couple of methods to evaluate properties of that circle. The following code is all typed into a file called, say, "".

from numpy import pi,sqrt class circle: def __init__(self,cx,cy,r): = cx = cy self.r = r def area(self): return self.r*self.r*pi def inside(self,x,y): dist = sqrt((*(*( if dist<= self.r: return True else: return False

Before we even start properly, we have to import the methods and variables we will need from numpy. We have to do this even if numpy was already imported into the Python session where we plan to use our circles. We only imported pi and sqrt, because that was all we needed for this class - we save some memory by not importing everything.

As we know, a circle is completely characterized by its center and radius. The x and y coordinates of the center of the circle, and its radius, are the attributes of this class. Everything else the class does will depend on those attributes. Thus cx, cy, and r are variables associated with the class. Nothing else in Python can see those variable names unless it refers to a specific circle object. Because they are particular attributes of the class, they must be invoked using a reference to the class itself, even from within the class. This is why those attributes are referred to using e.g. the notation. The self variable is always available inside a class as a way to identify itself. Note that we did need to list the self as the first argument in every method of the class.

There is a second kind of attribute associated with a class: the method attribute. These are functions that are part of the class. Like variable attributes, method attributes are called by prepending the name of a class object and a period to the name of the function. The circle class has three method attributes: area, inside, and __init__. The last is a very special method. It is invoked any time an object of this class is created. The idea is that every circle requires a center and radius, so we should specify those when we create any circle.

The class itself is an abstraction - it is the idea of a circle. To make an actual object based on the class, we define a Python variable that is identified as a circle.

>>> from circle import * >>> C=circle(1,-1,3) >>> 1 >>> -1

This creates a circle object called C. The circle C has center (1,-1) and radius 3. These numbers are installed in the attributes cx, cy as soon as the object C is created, through the arguments we passed to the circle class at that time. After the circle is created, we can gain access to its variable attributes using the notation and so on. We note in passing that, unlike other languages, Python provides no way to hide variable attributes from the Python session. If you have not seen other object oriented languages, pay no attention to the last remark.

We can make as many different objects based on our class as we like. Below we create a second object using the circle class. Once we have an object based on a class, we can use the method attributes to perform calculations or acquire information about that object.

>>> D=circle(2,0,2) >>> C.area() 28.274333882308138 >>> D.area() 12.566370614359172 >>> C.inside(-1,0) True >>> D.inside(-1,0) False

We refer to each method attribute of an object using the notation object_name.method_name. This means that in our case, C.area() is different from D.area(), even though the code for those methods was the same. The methods we called refer to the different variable attributes for their respective objects.

Thus, we see that a class object is a way of encapsulating some variables together with all the functions required to manage and maintain those variables. In this class we will not need to write our own classes (unless we want to), but we will see many classes that are part of other packages we will load.

The last test will take place at the final exam time on Tuesday, 12 December, from 1:30-3:30. It will be written as a one-hour (not 50 minute) exam, but you may have the full two hours for it. In other respects it will be very like the other tests, but comprehensive - it will emphasize Python, but cover all the topics we have seen. There is a Sample Exam, but be aware that things will have changed somewhat with the advent of ChatGPT.

Assignment A is posted.

Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2020 Kevin Cooper