In each of these, think about what data you might need to store to represent the “object” and what functionality you might need to apply to the data.
Simplest step is to just tell Python that Point2d will exist as a class, deferring the addition of information until later.
class Point2d(object):
pass
The Python reserved word pass says that this is the end of the class definition.
Points have an x and a y location, so we can write, for example,
p = Point2d()
p.x = 10
p.y = 5
dist_from_origin = sqrt(p.x**2 + p.y**2)
We have to do this for each class instance.
This is prone to mistakes:
Example of an error
q = Point2d()
q.x = -5
dist_from_origin = sqrt(q.x**2 + q.y**2) # q.y does not exist
The simplest way to make sure that all variables that are instances of a class have the appropriate attributes is to define them inside the class.
For example, we could redefine our class as
class Point2d(object):
x = 0
y = 0
pass
All instances of Point2d now have two attributes, x and y, and they are each initialized to 0.
We no longer need the pass because there is now something in the class.
We still need to initialize x and y to values other than 0:
p = Point2d()
p.x = 10
p.y = 5
What we’d really like to do is initialize them at the time we actually create the Point2d object:
p = Point2d(10,5)
We do this through a special function called an initializer in Python and a constructor in most other programming languages.
Inside the class this looks like
class Point2d(object):
def __init__( self, x0, y0 ):
self.x = x0
self.y = y0
Our code to create the point now becomes
p = Point2d(10,5)
Notes:
If we’d like to initialize the point to without passing these values to the constructor every time then we can specify default arguments
class Point2d(object):
def __init__( self, x0=0, y0=0 ):
self.x = x0
self.y = y0
allowing the initalization
p = Point2d()
We create functions that operate on the class objects inside the class definition:
import math
class Point2d(object):
def __init__( self, x0, y0 ):
self.x = x0
self.y = y0
def magnitude(self):
return math.sqrt(self.x**2 + self.y**2)
def dist(self, o):
return math.sqrt( (self.x-o.x)**2 + (self.y-o.y)**2 )
these are called methods
This is used as
p = Point2d(0,4)
q = Point2d(5,10)
leng = q.magnitude()
print "Magnitude %2f" %leng
print "Distance is %2f", %p.dist(q)
Note that with the above definition of Point2d, the following is illegal
q = Point2d(5,10)
leng = magnitude(q)
print "Magnitude %.2f" %leng
Another special method/function — one that we have to write — is __str__ which converts an object to a string. Write method __str__ for the Point2d so that it produces the following
>>> q = Point2d(5,10)
>>> print str(q)
(5,10)
Write a new Point2d method called scale that takes as an input argument a single value and multiples both the x and y attributes by this value.
We’d like to write code that uses our new objects in the most intuitive way possible.
For our point class, this involves use of operators such as
p = Point2d(1,2)
q = Point2d(3,5)
r = p+q
s = p-q
t = -s
Notice how in each case, we work with the Point2d variables (objects) just like we do with int and float variable (objects).
We implement these by writing the special functions __add__, __sub__, and __neg__
For example, inside the Point2d class we might have
def __add__(self,other):
return Point2d(self.x + other.x, self.y+other.y)
Similarly, we can define boolean operators such as == and != through the special functions __eq__ and __neq__