Top Banner
Objects in Python Damian Gordon
73

Creating Objects in Python

Jan 23, 2018

Download

Education

Damian Gordon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Creating Objects in Python

Objects in Python

Damian Gordon

Page 2: Creating Objects in Python

Declaring a Class

Page 3: Creating Objects in Python

The Point Class

class MyFirstClass:

pass

# END Class

Page 4: Creating Objects in Python

The Point Class

class MyFirstClass:

pass

# END Class

“move along, nothing to see here”

Page 5: Creating Objects in Python

The Point Class

class MyFirstClass:

pass

# END Class

class <ClassName>:

<Do stuff>

# END Class

Page 6: Creating Objects in Python

>>> 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>

Page 7: Creating Objects in Python

>>> 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>

Page 8: Creating Objects in Python

>>> 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>

Page 9: Creating Objects in Python

>>> 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>

Page 10: Creating Objects in Python

>>> 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>

Page 11: Creating Objects in Python

>>> 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>

Page 12: Creating Objects in Python

>>> 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>

Page 13: Creating Objects in Python

>>> 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>

Page 14: Creating Objects in Python

>>> 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>

Page 15: Creating Objects in Python

The Point Class

class Point:

pass

# END Class

p1 = Point()

p2 = Point()

Page 16: Creating Objects in Python

The Point Class

class Point:

pass

# END Class

p1 = Point()

p2 = Point()

Creating a class

Page 17: Creating Objects in Python

The Point Class

class Point:

pass

# END Class

p1 = Point()

p2 = Point()

Creating a class

Creating objects of that class

Page 18: Creating Objects in Python

Adding Attributes

Page 19: Creating Objects in Python

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);

Page 20: Creating Objects in Python

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);

Adding Attributes:This is all you need to do, just declare them

Page 21: Creating Objects in Python

Python: Object Attributes

• In Python the general form of declaring an attribute is as follows (we call this dot notation):

OBJECT. ATTRIBUTE = VALUE

Page 22: Creating Objects in Python

Adding Methods

Page 23: Creating Objects in Python

The Point Class

class Point:

def reset(self):

self.x = 0

self.y = 0

# END Reset

# END Class

Page 24: Creating Objects in Python

The Point Class

class Point:

def reset(self):

self.x = 0

self.y = 0

# END Reset

# END Class

Adding Methods:This is all you need

Page 25: Creating Objects in Python

The Point Class

p = Point()

p.x = 5

p.y = 4

print("P-x, P-y is: ", p.x, p.y);

p.reset()

print("P-x, P-y is: ", p.x, p.y);

Page 26: Creating Objects in Python

The Point Class

p = Point()

p.x = 5

p.y = 4

print("P-x, P-y is: ", p.x, p.y);

p.reset()

print("P-x, P-y is: ", p.x, p.y);

5 4

Page 27: Creating Objects in Python

The Point Class

p = Point()

p.x = 5

p.y = 4

print("P-x, P-y is: ", p.x, p.y);

p.reset()

print("P-x, P-y is: ", p.x, p.y);

5 4

0 0

Page 28: Creating Objects in Python

Let’s try that again…

Page 29: Creating Objects in Python

The Point Class

p = Point()

p.x = 5

p.y = 4

print("P-x, P-y is: ", p.x, p.y);

p.reset()

print("P-x, P-y is: ", p.x, p.y);

Page 30: Creating Objects in Python

The Point Class

p = Point()

p.x = 5

p.y = 4

print("P-x, P-y is: ", p.x, p.y);

p.reset()

print("P-x, P-y is: ", p.x, p.y);

Page 31: Creating Objects in Python

The Point Class

p = Point()

p.x = 5

p.y = 4

print("P-x, P-y is: ", p.x, p.y);

p.reset()

print("P-x, P-y is: ", p.x, p.y);

We can also say:Point.reset(p)

Page 32: Creating Objects in Python

Multiple Arguments

Page 33: Creating Objects in Python

The Point Class

class Point:

def reset(self):

self.x = 0

self.y = 0

# END Reset

# END Class

Page 34: Creating Objects in Python

The Point Class

• We can do this in a slightly different way, as follows:

Page 35: Creating Objects in Python

The Point Classclass Point:

def move(self,a,b):

self.x = a

self.y = b

# END Move

def reset(self):

self.move(0,0)

# END Reset

# END Class

Page 36: Creating Objects in Python

The Point Classclass Point:

def move(self,a,b):

self.x = a

self.y = b

# END Move

def reset(self):

self.move(0,0)

# END Reset

# END Class

Declare a new method called “move” that writes

values into the object.

Page 37: Creating Objects in Python

The Point Classclass Point:

def move(self,a,b):

self.x = a

self.y = b

# END Move

def reset(self):

self.move(0,0)

# END Reset

# END Class

Declare a new method called “move” that writes

values into the object.

Move the values 0 and 0 into the class to reset.

Page 38: Creating Objects in Python

Distance between two points

Page 39: Creating Objects in Python

The Point Class

• The distance between two points is:

d

d = √(x2 – x1)2 + (y2 – y1) 2

d = √(6 – 2)2 + (5 – 2) 2

d = √(4)2 + (3) 2

d = √16 + 9

d = √25

d = 5

Page 40: Creating Objects in Python

The Point Class

• Let’s see what we have already:

Page 41: Creating Objects in Python

The Point Classclass Point:

def move(self,a,b):

self.x = a

self.y = b

# END Move

def reset(self):

self.move(0,0)

# END Reset

# END Class

Page 42: Creating Objects in Python

The Point Class

• Now let’s add a new method in:

Page 43: Creating Objects in Python

The Point Classimport math

class Point:

def calc_distance(self, other_point):

return math.sqrt(

(self.x – other_point.x)**2 +

(self.y – other_point.y)**2)

# END calc_distance

# END Class d = √(x2 – x1)2 + (y2 – y1) 2

Page 44: Creating Objects in Python

The Point Class

• Now let’s add some code to make it run:

Page 45: Creating Objects in Python

The Point Class

p1 = Point()

p2 = Point()

p1.move(2,2)

p2.move(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))

p1

p2

Page 46: Creating Objects in Python

Initialising an Object

Page 47: Creating Objects in Python

Initialising an Object

• What if we did the following:

Page 48: Creating Objects in Python

Initialising an Object

p1 = Point()

p1.x = 5

print("P1-x, P1-y is: ", p1.x, p1.y);

Page 49: Creating Objects in Python

Initialising an Object

p1 = Point()

p1.x = 5

print("P1-x, P1-y is: ", p1.x, p1.y);

Page 50: Creating Objects in Python

>>>

Traceback (most recent call last):

File "C:/Users/damian.gordon/AppData/

Local/Programs/Python/Python35-32/Point-error.py",

line 11, in <module>

print("P1-x, P1-y is: ", p1.x, p1.y);

AttributeError: 'Point' object has no attribute 'y‘

>>>

Page 51: Creating Objects in Python

Initialising an Object

• So what can we do?

Page 52: Creating Objects in Python

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.

Page 53: Creating Objects in Python

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.

Page 54: Creating Objects in Python

Initialising an Object

• Python has a special name it uses for initialization methods.

__init__()

Page 55: Creating Objects in Python

class Point:

def __init__(self,x,y):

self.move(x,y)

# END Init

def move(self,a,b):

self.x = a

self.y = b

# END Move

def reset(self):

self.move(0,0)

# END Reset

# END Class

Initialising an Object

Page 56: Creating Objects in Python

Initialising an Objectclass Point:

def __init__(self,x,y):

self.move(x,y)

# END Init

def move(self,a,b):

self.x = a

self.y = b

# END Move

def reset(self):

self.move(0,0)

# END Reset

# END Class

When you create an object from this class, you are going to have to declare initial values for X and Y.

Page 57: Creating Objects in Python

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)

Page 58: Creating Objects in Python

Initialising an Object

• And if we forget to include the values, what happens?

Page 59: Creating Objects in Python

Initialising an Object

• And if we forget to include the values, what happens?

Traceback (most recent call last):

File "C:/Users/damian.gordon/AppData/Local/

Programs/Python/Python35-32/Point-init.py", line

21, in <module>

p = Point()

TypeError: __init__() missing 2 required

positional arguments: 'x' and 'y'

Page 60: Creating Objects in Python

Initialising an Object

• But if we want to be lazy we can do the following:

Page 61: Creating Objects in Python

Initialising an Object

def __init__(self, x=0, y=0):

self.move(x,y)

# END Init

Page 62: Creating Objects in Python

Initialising an Object

def __init__(self, x=0, y=0):

self.move(x,y)

# END Init

Page 63: Creating Objects in Python

Initialising an Object

• And then we can do:

– p1 = Point()

– p2 = Point(2,2)

Page 64: Creating Objects in Python

Initialising an Object

• And then we can do:

– p1 = Point()

– p2 = Point(2,2)

If we don’t supply any values, the initialization method will set the

values to 0,0.

Page 65: Creating Objects in Python

Initialising an Object

• And then we can do:

– p1 = Point()

– p2 = Point(2,2)

If we don’t supply any values, the initialization method will set the

values to 0,0.

But we can also supply the values, and the object is created with these

default values.

Page 66: Creating Objects in Python

Documenting the Methods

Page 67: Creating Objects in Python

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.

Page 68: Creating Objects in Python

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.

Page 69: Creating Objects in Python

Documenting the Methodsclass Point:

“Represents a point in 2D space”

def __init__(self,x,y):

‘Initialise the position of a new point’

self.move(x,y)

# END Init

Page 70: Creating Objects in Python

Documenting the Methodsdef 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’

self.move(0,0)

# END Reset

Page 71: Creating Objects in Python

Initialising an Object

• Now run the program, and then do:

>>>

>>> help (Point)

Page 72: Creating Objects in Python

Initialising an Object• And you’ll get:

Help on class Point in module __main__:

class Point(builtins.object)

| 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

| ----------------------------------------------

Page 73: Creating Objects in Python

etc.