SlideShare a Scribd company logo
Objects in Python
Damian Gordon
Declaring a Class
The Point Class
class MyFirstClass:
pass
# END Class
The Point Class
class MyFirstClass:
pass
# END Class
“move along, nothing
to see here”
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:
pass
# END Class
p1 = Point()
p2 = Point()
Creating a class
The Point Class
class Point:
pass
# END Class
p1 = Point()
p2 = Point()
Creating a class
Creating objects
of that class
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
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
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):
self.x = 0
self.y = 0
# END Reset
# END Class
Adding Methods:
This is all you need
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);
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
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
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);
p.reset()
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);
p.reset()
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);
p.reset()
print("P-x, P-y is: ", p.x, p.y);
We can also say:
Point.reset(p)
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):
self.move(0,0)
# END Reset
# END Class
The Point Class
class 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.
The Point Class
class 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.
Distance between two points
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
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):
self.move(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 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
The Point Class
• Now let’s add some code to make it run:
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
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/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‘
>>>
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 _ _()
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
Initialising an Object
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
When you create an object from
this class, you are going to have to
declare initial values for X and Y.
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/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'
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):
self.move(x,y)
# END Init
Initialising an Object
def __init__(self, x=0, y=0):
self.move(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:
– p1 = Point()
– p2 = Point(2,2)
If we don’t supply any values, the
initialization method will set the
values to 0,0.
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.
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’
self.move(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’
self.move(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(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
| ----------------------------------------------
etc.

More Related Content

What's hot

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
Rokonuzzaman Rony
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Exceptions in python
Exceptions in pythonExceptions in python
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
Python Pandas
Python PandasPython Pandas
Python Pandas
Sunil OS
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
University of Madras
 
Files in c++
Files in c++Files in c++
Files in c++
Selvin Josy Bai Somu
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
Archana Gopinath
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Data visualization in Python
Data visualization in PythonData visualization in Python
Data visualization in Python
Marc Garcia
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
File Handling in C++
File Handling in C++File Handling in C++
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 

What's hot (20)

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
 
Function in c
Function in cFunction in c
Function in c
 
Exceptions in python
Exceptions in pythonExceptions in python
Exceptions in python
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Files in c++
Files in c++Files in c++
Files in c++
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Data visualization in Python
Data visualization in PythonData visualization in Python
Data visualization in Python
 
File in C language
File in C languageFile in C language
File in C language
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 

Viewers also liked

Python: Access Control
Python: Access ControlPython: Access Control
Python: Access Control
Damian T. Gordon
 
Python: Third-Party Libraries
Python: Third-Party LibrariesPython: Third-Party Libraries
Python: Third-Party Libraries
Damian T. Gordon
 
Python: The Iterator Pattern
Python: The Iterator PatternPython: The Iterator Pattern
Python: The Iterator Pattern
Damian T. Gordon
 
Python: Design Patterns
Python: Design PatternsPython: Design Patterns
Python: Design Patterns
Damian T. Gordon
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
Damian T. Gordon
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
Damian T. Gordon
 
Object-Orientated Design
Object-Orientated DesignObject-Orientated Design
Object-Orientated Design
Damian T. Gordon
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
Damian T. Gordon
 
Python: Manager Objects
Python: Manager ObjectsPython: Manager Objects
Python: Manager Objects
Damian T. Gordon
 
Python: Polymorphism
Python: PolymorphismPython: Polymorphism
Python: Polymorphism
Damian T. Gordon
 
Python: Migrating from Procedural to Object-Oriented Programming
Python: Migrating from Procedural to Object-Oriented ProgrammingPython: Migrating from Procedural to Object-Oriented Programming
Python: Migrating from Procedural to Object-Oriented Programming
Damian T. Gordon
 
The Extreme Programming (XP) Model
The Extreme Programming (XP) ModelThe Extreme Programming (XP) Model
The Extreme Programming (XP) Model
Damian T. Gordon
 

Viewers also liked (12)

Python: Access Control
Python: Access ControlPython: Access Control
Python: Access Control
 
Python: Third-Party Libraries
Python: Third-Party LibrariesPython: Third-Party Libraries
Python: Third-Party Libraries
 
Python: The Iterator Pattern
Python: The Iterator PatternPython: The Iterator Pattern
Python: The Iterator Pattern
 
Python: Design Patterns
Python: Design PatternsPython: Design Patterns
Python: Design Patterns
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
 
Object-Orientated Design
Object-Orientated DesignObject-Orientated Design
Object-Orientated Design
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Python: Manager Objects
Python: Manager ObjectsPython: Manager Objects
Python: Manager Objects
 
Python: Polymorphism
Python: PolymorphismPython: Polymorphism
Python: Polymorphism
 
Python: Migrating from Procedural to Object-Oriented Programming
Python: Migrating from Procedural to Object-Oriented ProgrammingPython: Migrating from Procedural to Object-Oriented Programming
Python: Migrating from Procedural to Object-Oriented Programming
 
The Extreme Programming (XP) Model
The Extreme Programming (XP) ModelThe Extreme Programming (XP) Model
The Extreme Programming (XP) Model
 

Similar to Creating Objects in Python

OOC in python.ppt
OOC in python.pptOOC in python.ppt
OOC in python.ppt
SarathKumarK16
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
ssuser419267
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
UmooraMinhaji
 
Python - Classes and Objects, Inheritance
Python - Classes and Objects, InheritancePython - Classes and Objects, Inheritance
Python - Classes and Objects, Inheritance
erchetanchudasama
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
Joshua Forman
 
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181
Mahmoud Samir Fayed
 
Python_Unit_2 OOPS.pptx
Python_Unit_2  OOPS.pptxPython_Unit_2  OOPS.pptx
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
The Ring programming language version 1.5.4 book - Part 38 of 185
The Ring programming language version 1.5.4 book - Part 38 of 185The Ring programming language version 1.5.4 book - Part 38 of 185
The Ring programming language version 1.5.4 book - Part 38 of 185
Mahmoud Samir Fayed
 
nested_Object as Parameter & Recursion_Later_commamd.pptx
nested_Object as Parameter  & Recursion_Later_commamd.pptxnested_Object as Parameter  & Recursion_Later_commamd.pptx
nested_Object as Parameter & Recursion_Later_commamd.pptx
Kongu Engineering College, Perundurai, Erode
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
Fwdays
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
SantoshVarshney3
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
 
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189
Mahmoud Samir Fayed
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
OSU Open Source Lab
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
Workhorse Computing
 
Python advance
Python advancePython advance
Python advance
Mukul Kirti Verma
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
sushil155005
 
Python classes objects
Python classes objectsPython classes objects
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
Sasidhar Kothuru
 

Similar to Creating Objects in Python (20)

OOC in python.ppt
OOC in python.pptOOC in python.ppt
OOC in python.ppt
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
 
Python - Classes and Objects, Inheritance
Python - Classes and Objects, InheritancePython - Classes and Objects, Inheritance
Python - Classes and Objects, Inheritance
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
 
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181
 
Python_Unit_2 OOPS.pptx
Python_Unit_2  OOPS.pptxPython_Unit_2  OOPS.pptx
Python_Unit_2 OOPS.pptx
 
The Ring programming language version 1.5.4 book - Part 38 of 185
The Ring programming language version 1.5.4 book - Part 38 of 185The Ring programming language version 1.5.4 book - Part 38 of 185
The Ring programming language version 1.5.4 book - Part 38 of 185
 
nested_Object as Parameter & Recursion_Later_commamd.pptx
nested_Object as Parameter  & Recursion_Later_commamd.pptxnested_Object as Parameter  & Recursion_Later_commamd.pptx
nested_Object as Parameter & Recursion_Later_commamd.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
Python advance
Python advancePython advance
Python advance
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
python within 50 page .ppt
python within 50 page .pptpython within 50 page .ppt
python within 50 page .ppt
 
Python classes objects
Python classes objectsPython classes objects
Python classes objects
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 

More from Damian T. Gordon

Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.
Damian T. Gordon
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
Damian T. Gordon
 
REST and RESTful Services
REST and RESTful ServicesREST and RESTful Services
REST and RESTful Services
Damian T. Gordon
 
Serverless Computing
Serverless ComputingServerless Computing
Serverless Computing
Damian T. Gordon
 
Cloud Identity Management
Cloud Identity ManagementCloud Identity Management
Cloud Identity Management
Damian T. Gordon
 
Containers and Docker
Containers and DockerContainers and Docker
Containers and Docker
Damian T. Gordon
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
Damian T. Gordon
 
Introduction to ChatGPT
Introduction to ChatGPTIntroduction to ChatGPT
Introduction to ChatGPT
Damian T. Gordon
 
How to Argue Logically
How to Argue LogicallyHow to Argue Logically
How to Argue Logically
Damian T. Gordon
 
Evaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONSEvaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONS
Damian T. Gordon
 
Evaluating Teaching: MERLOT
Evaluating Teaching: MERLOTEvaluating Teaching: MERLOT
Evaluating Teaching: MERLOT
Damian T. Gordon
 
Evaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson RubricEvaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson Rubric
Damian T. Gordon
 
Evaluating Teaching: LORI
Evaluating Teaching: LORIEvaluating Teaching: LORI
Evaluating Teaching: LORI
Damian T. Gordon
 
Designing Teaching: Pause Procedure
Designing Teaching: Pause ProcedureDesigning Teaching: Pause Procedure
Designing Teaching: Pause Procedure
Damian T. Gordon
 
Designing Teaching: ADDIE
Designing Teaching: ADDIEDesigning Teaching: ADDIE
Designing Teaching: ADDIE
Damian T. Gordon
 
Designing Teaching: ASSURE
Designing Teaching: ASSUREDesigning Teaching: ASSURE
Designing Teaching: ASSURE
Damian T. Gordon
 
Designing Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning TypesDesigning Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning Types
Damian T. Gordon
 
Designing Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of InstructionDesigning Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of Instruction
Damian T. Gordon
 
Designing Teaching: Elaboration Theory
Designing Teaching: Elaboration TheoryDesigning Teaching: Elaboration Theory
Designing Teaching: Elaboration Theory
Damian T. Gordon
 
Universally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some ConsiderationsUniversally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some Considerations
Damian T. Gordon
 

More from Damian T. Gordon (20)

Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.Universal Design for Learning, Co-Designing with Students.
Universal Design for Learning, Co-Designing with Students.
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
REST and RESTful Services
REST and RESTful ServicesREST and RESTful Services
REST and RESTful Services
 
Serverless Computing
Serverless ComputingServerless Computing
Serverless Computing
 
Cloud Identity Management
Cloud Identity ManagementCloud Identity Management
Cloud Identity Management
 
Containers and Docker
Containers and DockerContainers and Docker
Containers and Docker
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
 
Introduction to ChatGPT
Introduction to ChatGPTIntroduction to ChatGPT
Introduction to ChatGPT
 
How to Argue Logically
How to Argue LogicallyHow to Argue Logically
How to Argue Logically
 
Evaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONSEvaluating Teaching: SECTIONS
Evaluating Teaching: SECTIONS
 
Evaluating Teaching: MERLOT
Evaluating Teaching: MERLOTEvaluating Teaching: MERLOT
Evaluating Teaching: MERLOT
 
Evaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson RubricEvaluating Teaching: Anstey and Watson Rubric
Evaluating Teaching: Anstey and Watson Rubric
 
Evaluating Teaching: LORI
Evaluating Teaching: LORIEvaluating Teaching: LORI
Evaluating Teaching: LORI
 
Designing Teaching: Pause Procedure
Designing Teaching: Pause ProcedureDesigning Teaching: Pause Procedure
Designing Teaching: Pause Procedure
 
Designing Teaching: ADDIE
Designing Teaching: ADDIEDesigning Teaching: ADDIE
Designing Teaching: ADDIE
 
Designing Teaching: ASSURE
Designing Teaching: ASSUREDesigning Teaching: ASSURE
Designing Teaching: ASSURE
 
Designing Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning TypesDesigning Teaching: Laurilliard's Learning Types
Designing Teaching: Laurilliard's Learning Types
 
Designing Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of InstructionDesigning Teaching: Gagne's Nine Events of Instruction
Designing Teaching: Gagne's Nine Events of Instruction
 
Designing Teaching: Elaboration Theory
Designing Teaching: Elaboration TheoryDesigning Teaching: Elaboration Theory
Designing Teaching: Elaboration Theory
 
Universally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some ConsiderationsUniversally Designed Learning Spaces: Some Considerations
Universally Designed Learning Spaces: Some Considerations
 

Recently uploaded

PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 

Recently uploaded (20)

PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 

Creating Objects in Python

  • 3. The Point Class class MyFirstClass: pass # END Class
  • 4. The Point Class class MyFirstClass: pass # END Class “move along, nothing to see here”
  • 5. The Point Class class MyFirstClass: pass # END Class class <ClassName>: <Do stuff> # END Class
  • 6. >>> 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>
  • 7. >>> 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>
  • 8. >>> 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>
  • 9. >>> 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>
  • 10. >>> 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>
  • 11. >>> 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>
  • 12. >>> 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>
  • 13. >>> 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>
  • 14. >>> 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>
  • 15. The Point Class class Point: pass # END Class p1 = Point() p2 = Point()
  • 16. The Point Class class Point: pass # END Class p1 = Point() p2 = Point() Creating a class
  • 17. The Point Class class Point: pass # END Class p1 = Point() p2 = Point() Creating a class Creating objects of that class
  • 19. 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);
  • 20. 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
  • 21. Python: Object Attributes • In Python the general form of declaring an attribute is as follows (we call this dot notation): OBJECT. ATTRIBUTE = VALUE
  • 23. The Point Class class Point: def reset(self): self.x = 0 self.y = 0 # END Reset # END Class
  • 24. 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
  • 25. 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);
  • 26. 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
  • 27. 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
  • 28. Let’s try that again…
  • 29. 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);
  • 30. 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);
  • 31. 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)
  • 33. The Point Class class Point: def reset(self): self.x = 0 self.y = 0 # END Reset # END Class
  • 34. The Point Class • We can do this in a slightly different way, as follows:
  • 35. The Point Class class 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
  • 36. The Point Class class 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.
  • 37. The Point Class class 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.
  • 39. 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
  • 40. The Point Class • Let’s see what we have already:
  • 41. The Point Class class 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
  • 42. The Point Class • Now let’s add a new method in:
  • 43. The Point Class import 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
  • 44. The Point Class • Now let’s add some code to make it run:
  • 45. 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
  • 47. Initialising an Object • What if we did the following:
  • 48. Initialising an Object p1 = Point() p1.x = 5 print("P1-x, P1-y is: ", p1.x, p1.y);
  • 49. Initialising an Object p1 = Point() p1.x = 5 print("P1-x, P1-y is: ", p1.x, p1.y);
  • 50. >>> 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‘ >>>
  • 51. Initialising an Object • So what can we do?
  • 52. 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.
  • 53. 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.
  • 54. Initialising an Object • Python has a special name it uses for initialization methods. _ _ init _ _()
  • 55. 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
  • 56. Initialising an Object 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 When you create an object from this class, you are going to have to declare initial values for X and Y.
  • 57. 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)
  • 58. Initialising an Object • And if we forget to include the values, what happens?
  • 59. 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'
  • 60. Initialising an Object • But if we want to be lazy we can do the following:
  • 61. Initialising an Object def __init__(self, x=0, y=0): self.move(x,y) # END Init
  • 62. Initialising an Object def __init__(self, x=0, y=0): self.move(x,y) # END Init
  • 63. Initialising an Object • And then we can do: – p1 = Point() – p2 = Point(2,2)
  • 64. 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.
  • 65. 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.
  • 67. 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.
  • 68. 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.
  • 69. Documenting the Methods class 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
  • 70. 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’ self.move(0,0) # END Reset
  • 71. Initialising an Object • Now run the program, and then do: >>> >>> help (Point)
  • 72. 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 | ----------------------------------------------
  • 73. etc.