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 (16)

heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
ssuser7319f8
 
Harga Pokok Proses metode rata-rata tertimbang
Harga Pokok Proses metode rata-rata tertimbangHarga Pokok Proses metode rata-rata tertimbang
Harga Pokok Proses metode rata-rata tertimbang
RAnie Chucute
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
Kwangshin Oh
 
Matriks eselon baris
Matriks eselon barisMatriks eselon baris
Matriks eselon baris
agung8463
 
Chapter 7 Run Time Environment
Chapter 7   Run Time EnvironmentChapter 7   Run Time Environment
Chapter 7 Run Time Environment
Radhakrishnan Chinnusamy
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
Koteswari Kasireddy
 
Harga pokok proses lanjutan 1
Harga pokok proses lanjutan 1Harga pokok proses lanjutan 1
Harga pokok proses lanjutan 1
jhumanangshare
 
Manfaat analisis biaya volume-laba
Manfaat analisis biaya volume-labaManfaat analisis biaya volume-laba
Manfaat analisis biaya volume-laba
budieto
 
Greedy method
Greedy method Greedy method
Greedy method
Dr Shashikant Athawale
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
Horngren Chapter06
Horngren Chapter06Horngren Chapter06
Horngren Chapter06
Shanty Dewi
 
Python-Encapsulation.pptx
Python-Encapsulation.pptxPython-Encapsulation.pptx
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
Decrease and Conquer in analysis of algorithms.pptx
Decrease and Conquer in analysis of algorithms.pptxDecrease and Conquer in analysis of algorithms.pptx
Decrease and Conquer in analysis of algorithms.pptx
ArunachalamSelva
 
mencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di Cmencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di C
kir yy
 
Tahap instalasi-postgresql-di-windows
Tahap instalasi-postgresql-di-windowsTahap instalasi-postgresql-di-windows
Tahap instalasi-postgresql-di-windows
Ally Florez
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
ssuser7319f8
 
Harga Pokok Proses metode rata-rata tertimbang
Harga Pokok Proses metode rata-rata tertimbangHarga Pokok Proses metode rata-rata tertimbang
Harga Pokok Proses metode rata-rata tertimbang
RAnie Chucute
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
Kwangshin Oh
 
Matriks eselon baris
Matriks eselon barisMatriks eselon baris
Matriks eselon baris
agung8463
 
Harga pokok proses lanjutan 1
Harga pokok proses lanjutan 1Harga pokok proses lanjutan 1
Harga pokok proses lanjutan 1
jhumanangshare
 
Manfaat analisis biaya volume-laba
Manfaat analisis biaya volume-labaManfaat analisis biaya volume-laba
Manfaat analisis biaya volume-laba
budieto
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
Horngren Chapter06
Horngren Chapter06Horngren Chapter06
Horngren Chapter06
Shanty Dewi
 
Decrease and Conquer in analysis of algorithms.pptx
Decrease and Conquer in analysis of algorithms.pptxDecrease and Conquer in analysis of algorithms.pptx
Decrease and Conquer in analysis of algorithms.pptx
ArunachalamSelva
 
mencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di Cmencari nilai minimum menggunakan fungsi rekursif di C
mencari nilai minimum menggunakan fungsi rekursif di C
kir yy
 
Tahap instalasi-postgresql-di-windows
Tahap instalasi-postgresql-di-windowsTahap instalasi-postgresql-di-windows
Tahap instalasi-postgresql-di-windows
Ally Florez
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 

Viewers also liked (13)

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: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
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
 
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: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
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
 
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
 
Ad

Similar to Creating Objects in Python (20)

object oriented porgramming using Java programming
object oriented porgramming using Java programmingobject oriented porgramming using Java programming
object oriented porgramming using Java programming
afsheenfaiq2
 
Module IV_updated(old).pdf
Module IV_updated(old).pdfModule IV_updated(old).pdf
Module IV_updated(old).pdf
R.K.College of engg & Tech
 
Python 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptxPython 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
Module-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptxModule-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
OOC in python.ppt
OOC in python.pptOOC in python.ppt
OOC in python.ppt
SarathKumarK16
 
Python - Classes and Objects, Inheritance
Python - Classes and Objects, InheritancePython - Classes and Objects, Inheritance
Python - Classes and Objects, Inheritance
erchetanchudasama
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
UmooraMinhaji
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
ssuser419267
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf
1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf
1_7a6f85d03f132dcd9d7592bc4643be1c_MIT6_0001F16_Lec8.pdf
ID Bilişim ve Ticaret Ltd. Şti.
 
Module 4.pptx
Module 4.pptxModule 4.pptx
Module 4.pptx
charancherry185493
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
Raghunath A
 
Lecture 6 python oop (ewurc)
Lecture 6 python oop (ewurc)Lecture 6 python oop (ewurc)
Lecture 6 python oop (ewurc)
Al-Mamun Riyadh (Mun)
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.pptLecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.pptLecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
Python classes objects
Python classes objectsPython classes objects
Python classes objects
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
object oriented porgramming using Java programming
object oriented porgramming using Java programmingobject oriented porgramming using Java programming
object oriented porgramming using Java programming
afsheenfaiq2
 
Python 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptxPython 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
Module-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptxModule-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
 
Python - Classes and Objects, Inheritance
Python - Classes and Objects, InheritancePython - Classes and Objects, Inheritance
Python - Classes and Objects, Inheritance
erchetanchudasama
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
UmooraMinhaji
 
08-classes-objects.ppt
08-classes-objects.ppt08-classes-objects.ppt
08-classes-objects.ppt
ssuser419267
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
Raghunath A
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.pptLecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.pptLecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
Ad

More from Damian T. Gordon (20)

Introduction to Prompts and Prompt Engineering
Introduction to Prompts and Prompt EngineeringIntroduction to Prompts and Prompt Engineering
Introduction to Prompts and Prompt Engineering
Damian T. Gordon
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
TRIZ: Theory of Inventive Problem Solving
TRIZ: Theory of Inventive Problem SolvingTRIZ: Theory of Inventive Problem Solving
TRIZ: Theory of Inventive Problem Solving
Damian T. Gordon
 
Some Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAISome Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAI
Damian T. Gordon
 
Some Common Errors that Generative AI Produces
Some Common Errors that Generative AI ProducesSome Common Errors that Generative AI Produces
Some Common Errors that Generative AI Produces
Damian T. Gordon
 
The Use of Data and Datasets in Data Science
The Use of Data and Datasets in Data ScienceThe Use of Data and Datasets in Data Science
The Use of Data and Datasets in Data Science
Damian T. Gordon
 
A History of Different Versions of Microsoft Windows
A History of Different Versions of Microsoft WindowsA History of Different Versions of Microsoft Windows
A History of Different Versions of Microsoft Windows
Damian T. Gordon
 
Writing an Abstract: A Question-based Approach
Writing an Abstract: A Question-based ApproachWriting an Abstract: A Question-based Approach
Writing an Abstract: A Question-based Approach
Damian T. Gordon
 
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
 
A CheckSheet for Inclusive Software Design
A CheckSheet for Inclusive Software DesignA CheckSheet for Inclusive Software Design
A CheckSheet for Inclusive Software Design
Damian T. Gordon
 
A History of Versions of the Apple MacOS
A History of Versions of the Apple MacOSA History of Versions of the Apple MacOS
A History of Versions of the Apple MacOS
Damian T. Gordon
 
68 Ways that Data Science and AI can help address the UN Sustainability Goals
68 Ways that Data Science and AI can help address the UN Sustainability Goals68 Ways that Data Science and AI can help address the UN Sustainability Goals
68 Ways that Data Science and AI can help address the UN Sustainability Goals
Damian T. Gordon
 
Copyright and Creative Commons Considerations
Copyright and Creative Commons ConsiderationsCopyright and Creative Commons Considerations
Copyright and Creative Commons Considerations
Damian T. Gordon
 
Exam Preparation: Some Ideas and Suggestions
Exam Preparation: Some Ideas and SuggestionsExam Preparation: Some Ideas and Suggestions
Exam Preparation: Some Ideas and Suggestions
Damian T. Gordon
 
Studying and Notetaking: Some Suggestions
Studying and Notetaking: Some SuggestionsStudying and Notetaking: Some Suggestions
Studying and Notetaking: Some Suggestions
Damian T. Gordon
 
The Growth Mindset: Explanations and Activities
The Growth Mindset: Explanations and ActivitiesThe Growth Mindset: Explanations and Activities
The Growth Mindset: Explanations and Activities
Damian T. Gordon
 
Hyperparameter Tuning in Neural Networks
Hyperparameter Tuning in Neural NetworksHyperparameter Tuning in Neural Networks
Hyperparameter Tuning in Neural Networks
Damian T. Gordon
 
Early 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and ArtistsEarly 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and Artists
Damian T. Gordon
 
An Introduction to Generative Artificial Intelligence
An Introduction to Generative Artificial IntelligenceAn Introduction to Generative Artificial Intelligence
An Introduction to Generative Artificial Intelligence
Damian T. Gordon
 
An Introduction to Green Computing with a fun quiz.
An Introduction to Green Computing with a fun quiz.An Introduction to Green Computing with a fun quiz.
An Introduction to Green Computing with a fun quiz.
Damian T. Gordon
 
Introduction to Prompts and Prompt Engineering
Introduction to Prompts and Prompt EngineeringIntroduction to Prompts and Prompt Engineering
Introduction to Prompts and Prompt Engineering
Damian T. Gordon
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
TRIZ: Theory of Inventive Problem Solving
TRIZ: Theory of Inventive Problem SolvingTRIZ: Theory of Inventive Problem Solving
TRIZ: Theory of Inventive Problem Solving
Damian T. Gordon
 
Some Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAISome Ethical Considerations of AI and GenAI
Some Ethical Considerations of AI and GenAI
Damian T. Gordon
 
Some Common Errors that Generative AI Produces
Some Common Errors that Generative AI ProducesSome Common Errors that Generative AI Produces
Some Common Errors that Generative AI Produces
Damian T. Gordon
 
The Use of Data and Datasets in Data Science
The Use of Data and Datasets in Data ScienceThe Use of Data and Datasets in Data Science
The Use of Data and Datasets in Data Science
Damian T. Gordon
 
A History of Different Versions of Microsoft Windows
A History of Different Versions of Microsoft WindowsA History of Different Versions of Microsoft Windows
A History of Different Versions of Microsoft Windows
Damian T. Gordon
 
Writing an Abstract: A Question-based Approach
Writing an Abstract: A Question-based ApproachWriting an Abstract: A Question-based Approach
Writing an Abstract: A Question-based Approach
Damian T. Gordon
 
Using GenAI for Universal Design for Learning
Using GenAI for Universal Design for LearningUsing GenAI for Universal Design for Learning
Using GenAI for Universal Design for Learning
Damian T. Gordon
 
A CheckSheet for Inclusive Software Design
A CheckSheet for Inclusive Software DesignA CheckSheet for Inclusive Software Design
A CheckSheet for Inclusive Software Design
Damian T. Gordon
 
A History of Versions of the Apple MacOS
A History of Versions of the Apple MacOSA History of Versions of the Apple MacOS
A History of Versions of the Apple MacOS
Damian T. Gordon
 
68 Ways that Data Science and AI can help address the UN Sustainability Goals
68 Ways that Data Science and AI can help address the UN Sustainability Goals68 Ways that Data Science and AI can help address the UN Sustainability Goals
68 Ways that Data Science and AI can help address the UN Sustainability Goals
Damian T. Gordon
 
Copyright and Creative Commons Considerations
Copyright and Creative Commons ConsiderationsCopyright and Creative Commons Considerations
Copyright and Creative Commons Considerations
Damian T. Gordon
 
Exam Preparation: Some Ideas and Suggestions
Exam Preparation: Some Ideas and SuggestionsExam Preparation: Some Ideas and Suggestions
Exam Preparation: Some Ideas and Suggestions
Damian T. Gordon
 
Studying and Notetaking: Some Suggestions
Studying and Notetaking: Some SuggestionsStudying and Notetaking: Some Suggestions
Studying and Notetaking: Some Suggestions
Damian T. Gordon
 
The Growth Mindset: Explanations and Activities
The Growth Mindset: Explanations and ActivitiesThe Growth Mindset: Explanations and Activities
The Growth Mindset: Explanations and Activities
Damian T. Gordon
 
Hyperparameter Tuning in Neural Networks
Hyperparameter Tuning in Neural NetworksHyperparameter Tuning in Neural Networks
Hyperparameter Tuning in Neural Networks
Damian T. Gordon
 
Early 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and ArtistsEarly 20th Century Modern Art: Movements and Artists
Early 20th Century Modern Art: Movements and Artists
Damian T. Gordon
 
An Introduction to Generative Artificial Intelligence
An Introduction to Generative Artificial IntelligenceAn Introduction to Generative Artificial Intelligence
An Introduction to Generative Artificial Intelligence
Damian T. Gordon
 
An Introduction to Green Computing with a fun quiz.
An Introduction to Green Computing with a fun quiz.An Introduction to Green Computing with a fun quiz.
An Introduction to Green Computing with a fun quiz.
Damian T. Gordon
 

Recently uploaded (20)

Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad LevelLDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDM & Mia eStudios
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Fatman Book HD Pdf by aayush songare.pdf
Fatman Book  HD Pdf by aayush songare.pdfFatman Book  HD Pdf by aayush songare.pdf
Fatman Book HD Pdf by aayush songare.pdf
Aayush Songare
 
Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad LevelLDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDM & Mia eStudios
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Fatman Book HD Pdf by aayush songare.pdf
Fatman Book  HD Pdf by aayush songare.pdfFatman Book  HD Pdf by aayush songare.pdf
Fatman Book HD Pdf by aayush songare.pdf
Aayush Songare
 

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.