Objects in Python
Declaring a Class
The Point Class
class MyFirstClass:
pass
# END Class
The Point Class
class MyFirstClass: “move along, nothing
pass to see here”
# END Class
The Point Class
class MyFirstClass:
pass
# END Class
class <ClassName>:
<Do stuff>
# END Class
>>> a = MyFirstClass()
>>> print(a)
<__main__.MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
<__main__.MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
< main .MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
< main .MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
< main .MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
< main .MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
< main .MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
< main .MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
< main .MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
< main .MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
< main .MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
<__main__.MyFirstClass object at 0x02D60B30>
>>> a = MyFirstClass()
>>> print(a)
< main .MyFirstClass object at 0x02D60B10>
>>> b = a
>>> print(b)
< main .MyFirstClass object at 0x02D60B10>
>>> b = MyFirstClass()
>>> print(b)
< main .MyFirstClass object at 0x02D60B30>
The Point Class
class Point:
pass
# END Class
p1 = Point()
p2 = Point()
The Point Class
class Point:
Creating a class
pass
# END Class
p1 = Point()
p2 = Point()
The Point Class
class Point:
Creating a class
pass
# END Class
Creating objects
of that class
p1 = Point()
p2 = Point()
Adding Attributes
The Point Class
p1.x = 5
p1.y = 4
p2.x = 3
p2.y = 6
print("P1-x, P1-y is: ", p1.x, p1.y);
print("P2-x, P2-y is: ", p2.x, p2.y);
The Point Class
p1.x = 5 Adding Attributes:
p1.y = 4 This is all you need to
do, just declare them
p2.x = 3
p2.y = 6
print("P1-x, P1-y is: ", p1.x, p1.y);
print("P2-x, P2-y is: ", p2.x, p2.y);
Python: Object Attributes
• In Python the general form of declaring an attribute is as
follows (we call this dot notation):
OBJECT. ATTRIBUTE = VALUE
Adding Methods
The Point Class
class Point:
def reset(self):
self.x = 0
self.y = 0
# END Reset
# END Class
The Point Class
class Point:
def reset(self): Adding Methods:
self.x = 0 This is all you need
self.y = 0
# END Reset
# END Class
The Point Class
p = Point()
p.x = 5
p.y = 4
print("P-x, P-y is: ", p.x, p.y);
[Link]()
print("P-x, P-y is: ", p.x, p.y);
The Point Class
p = Point()
p.x = 5
p.y = 4
print("P-x, P-y is: ", p.x, p.y);
5 4
[Link]()
print("P-x, P-y is: ", p.x, p.y);
The Point Class
p = Point()
p.x = 5
p.y = 4
print("P-x, P-y is: ", p.x, p.y);
5 4
[Link]()
print("P-x, P-y is: ", p.x, p.y);
0 0
Let’s try that again…
The Point Class
p = Point()
p.x = 5
p.y = 4
print("P-x, P-y is: ", p.x, p.y);
[Link]()
print("P-x, P-y is: ", p.x, p.y);
The Point Class
p = Point()
p.x = 5
p.y = 4
print("P-x, P-y is: ", p.x, p.y);
[Link]()
print("P-x, P-y is: ", p.x, p.y);
The Point Class
p = Point()
p.x = 5
p.y = 4
print("P-x, P-y is: ", p.x, p.y);
We can also say:
[Link]() [Link](p)
print("P-x, P-y is: ", p.x, p.y);
Multiple Arguments
The Point Class
class Point:
def reset(self):
self.x = 0
self.y = 0
# END Reset
# END Class
The Point Class
• We can do this in a slightly different way, as follows:
The Point Class
class Point:
def move(self,a,b):
self.x = a
self.y = b
# END Move
def reset(self):
[Link](0,0)
# END Reset
# END Class
The Point Class
class Point:
Declare a new method
called “move” that writes
def move(self,a,b): values into the object.
self.x = a
self.y = b
# END Move
def reset(self):
[Link](0,0)
# END Reset
# END Class
The Point Class
class Point:
Declare a new method
called “move” that writes
def move(self,a,b): values into the object.
self.x = a
self.y = b Move the values 0 and 0
# END Move into the class to reset.
def reset(self):
[Link](0,0)
# END Reset
# END Class
Distance between two points
The Point Class
• The distance between two points is:
d = √(x2 – x1)2 + (y2 – y1) 2
d = √(6 – 2)2 + (5 – 2) 2
d d = √(4)2 + (3) 2
d = √16 + 9
d = √25
d=5
The Point Class
• Let’s see what we have already:
The Point Class
class Point:
def move(self,a,b):
self.x = a
self.y = b
# END Move
def reset(self):
[Link](0,0)
# END Reset
# END Class
The Point Class
• Now let’s add a new method in:
The Point Class
import math
class Point:
def calc_distance(self, other_point):
return [Link](
(self.x – other_point.x)**2 +
(self.y – other_point.y)**2)
# END calc_distance
# END Class d = √(x2 – x1)2 + (y2 – y1) 2
The Point Class
• Now let’s add some code to make it run:
The Point Class
p2
p1 = Point()
p2 = Point()
p1
[Link](2,2)
[Link](6,5)
print("P1-x, P1-y is: ", p1.x, p1.y)
print("P2-x, P2-y is: ", p2.x, p2.y)
print("Distance from P1 to P2 is:", p1.calc_distance(p2))
Initialising an Object
Initialising an Object
• What if we did the following:
Initialising an Object
p1 = Point()
p1.x = 5
print("P1-x, P1-y is: ", p1.x, p1.y);
Initialising an Object
p1 = Point()
p1.x = 5
print("P1-x, P1-y is: ", p1.x, p1.y);
>>>
Traceback (most recent call last):
File "C:/Users/[Link]/AppData/
Local/Programs/Python/Python35-32/[Link]",
line 11, in <module>
print("P1-x, P1-y is: ", p1.x, p1.y);
AttributeError: 'Point' object has no attribute 'y‘
>>>
Initialising an Object
• So what can we do?
Initialising an Object
• So what can we do?
• We need to create a method that forces the programmers to
initialize the attributes of the class to some starting value, just
so that we don’t have this problem.
Initialising an Object
• So what can we do?
• We need to create a method that forces the programmers to
initialize the attributes of the class to some starting value, just
so that we don’t have this problem.
• This is called an initialization method.
Initialising an Object
• Python has a special name it uses for initialization methods.
_ _ init _ _()
Initialising an Object
class Point:
def init (self,x,y):
[Link](x,y)
# END Init
def move(self,a,b):
self.x = a
self.y = b
# END Move
def reset(self):
[Link](0,0)
# END Reset
# END Class
Initialising an Object
class Point:
def init (self,x,y): When you create an object from
this class, you are going to have to
[Link](x,y)
declare initial values for X and Y.
# END Init
def move(self,a,b):
self.x = a
self.y = b
# END Move
def reset(self):
[Link](0,0)
# END Reset
# END Class
Initialising an Object
• So without the initialization method we could do this:
– p1 = Point()
– p2 = Point()
• but with the initialization method we have to do this:
– p1 = Point(6,5)
– p2 = Point(2,2)
Initialising an Object
• And if we forget to include the values, what happens?
Initialising an Object
• And if we forget to include the values, what happens?
Traceback (most recent call last):
File "C:/Users/[Link]/AppData/Local/
Programs/Python/Python35-32/[Link]", line
21, in <module>
p = Point()
TypeError: init () missing 2 required
positional arguments: 'x' and 'y'
Initialising an Object
• But if we want to be lazy we can do the following:
Initialising an Object
def init (self, x=0, y=0):
[Link](x,y)
# END Init
Initialising an Object
def init (self, x=0, y=0):
[Link](x,y)
# END Init
Initialising an Object
• And then we can do:
– p1 = Point()
– p2 = Point(2,2)
Initialising an Object
• And then we can do: If we don’t supply any values, the
initialization method will set the
values to 0,0.
– p1 = Point()
– p2 = Point(2,2)
Initialising an Object
• And then we can do: If we don’t supply any values, the
initialization method will set the
values to 0,0.
– p1 = Point()
– p2 = Point(2,2)
But we can also supply the values,
and the object is created with these
default values.
Documenting the Methods
Documenting the Methods
• Python is considered one of the most easy
programming languages, but nonetheless a vital part
of object-orientated programming is to explain what
each class and method does to help promote object
reuse.
Documenting the Methods
• Python supports this through the use of
docstrings.
• These are strings enclosed in either quotes(‘) or
doublequotes(“) just after the class or method
declaration.
Documenting the Methods
class Point:
“Represents a point in 2D space”
def init (self,x,y):
‘Initialise the position of a new point’
[Link](x,y)
# END Init
Documenting the Methods
def move(self,a,b):
‘Move the point to a new location’
self.x = a
self.y = b
# END Move
def reset(self):
‘Reset the point back to the origin’
[Link](0,0)
# END Reset
Initialising an Object
• Now run the program, and then do:
>>>
>>> help (Point)
Initialising an Object
• And you’ll get:
Help on class Point in module main :
class Point([Link])
| Represents a point in 2D space
|
| Methods defined here:
|
| calc_distance(self, other_point)
| Get the distance between two points
|
| move(self, a, b)
| Move the point to a new location
|
| reset(self)
| Reset the point back to the origin
| ----------------------------------------------