SlideShare a Scribd company logo
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
BS GIS Instructor: Inzamam Baig
Lecture 13
Fundamentals of Programming
Object Oriented Programming
Python is a multi-paradigm programming language
Objects are the basic units of object-oriented programming
Object
Real-world objects share two characteristics:
They all have state and behavior
Dogs have state (name, color, breed, hungry) and behavior (barking,
fetching, wagging tail)
Bicycles also have state (current gear, current pedal cadence, current
speed) and behavior (changing gear, changing pedal cadence, applying
brakes).
Identifying the state and behavior for real-world objects is a great way to
begin thinking in terms of object-oriented programming
State
Current GearCurrent Speed
Current Pedal Candence
Behaviour
Change GearApply Brake
Changing Pedal Candence
An object has two characteristics:
attributes
behavior
Class
A class is a like a template for an object.
The example for class of person can be :
class Person:
pass
Object
An object (instance) is an instantiation of a class
When class is defined, only the description for the object is defined
Therefore, no memory or storage is allocated
obj = Person()
Here, obj is an object of class Person.
class Person:
# class attribute
employer = 'Karakoram International University'
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Person class
student = Person("Zeeshan", 10)
teacher = Person("Fatima", 15)
# access the class attributes
print("student is employee of {}".format(student.employer))
print("teacher is employee of {}".format(teacher.employer))
# access the instance attributes
print("{} is {} years old".format(student.name, student.age))
print("{} is {} years old".format(teacher.name, teacher.age))
Methods
Methods are functions defined inside the body of a class
They are used to define the behaviors of an object
class Student:
# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instance method
def study(self, book_name):
return "{} is studying {}".format(self.name, book_name)
def playing(self):
return "{} is now playing".format(self.name)
# instantiate the object
zeeshan = Student("Zeeshan", 10)
# call our instance methods
print(zeeshan.study("Programming"))
print(zeeshan.playing())
Self
Self keyword is used to represent an instance of the class
Constructor
Class functions that begin with double underscore __ are called
special functions as they have special meaning
Of one particular interest is the __init__() function. This special
function gets called whenever a new object of that class is instantiated
This type of function is also called constructors in Object Oriented
Programming (OOP)
We normally use it to initialize all the variables
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
Deleting Attributes
Any attribute of an object can be deleted anytime, using the del
statement
del zeeshan.name
class Student:
# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
def get_name(self):
return self.name
# instantiate the object
zeeshan = Student("Zeeshan", 10)
del zeeshan.name
student_name = zeeshan.get_name()
Deleting Object
del zeeshan
Deleting objects in Python removes the name binding
The object however continues to exist in memory and if no other
name is bound to it, it is later automatically destroyed
This automatic destruction of unreferenced objects in Python is
also called garbage collection
Student Object
name = zeeshan,
age = 10
Student Object
name = zeeshan,
age = 10
zeeshan zeeshan
zeeshan = Student('Zeeshan',
10)
del zeeshan
Class Methods
class methods take a cls parameter that points to the class
the class method only has access to this cls argument, it can't
modify object instance state
class methods can still modify class state that applies across all
instances of the class
@classmethod
def classmethod(cls):
pass
Static Methods
This type of method takes neither a self nor a cls parameter
static method can neither modify object state nor class state
Inheritance
Inheritance enables us to define a class that takes all the
functionality from a parent class and allows us to add more
The new class is called derived (or child) class and the one
from which it inherits is called the base (or parent) class
Inheritance models is a relationship
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Base
Derived
Extends
class Person(object):
# Constructor
def __init__(self, name):
self.name = name
def get_name(self):
return self.name
class Student(Person):
def is_registred(self):
return True
class Teacher(Person):
def is_employeed(self):
return True
zeeshan = Student("Zeeshan")
print(zeeshan.get_name(), zeeshan.is_registred())
adnan = Teacher("Adnan")
print(adnan.get_name(), adnan.is_employeed())
Is Instance
isinstance(zeeshan, Student)
Is Subclass
issubclass(Student, Person)
super()
class Person(object):
# Constructor
def __init__(self, name):
self.name = name
def get_name(self):
return self.name
class Student(Person):
def __init__(self, name, gpa):
super().__init__(name)
self.gpa = gpa
def is_registred(self):
return True
Multiple Inheritance
A class can be derived from more than one base class in Python,
similar to C++. This is called multiple inheritance
In multiple inheritance, the features of all the base classes are
inherited into the derived class
class Base1:
pass
class Base2:
pass
class MultiDerived(Base1, Base2):
pass
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Player:
def __init__(self, sports_name):
self.sports_name = sports_name
class Student(Person, Player):
def __init__(self, name, age, sports_name):
Person.__init__(self, name, age)
Player.__init__(self, sports_name)
s = Student('Adnan', 10, 'Cricket')
Every class in Python is derived from the object class. It is the
most base type in Python.
So technically, all other classes, either built-in or user-defined,
are derived classes and all objects are instances of the object
class
# Output: True
print(issubclass(list,object))
# Output: True
print(isinstance(5.5,object))
# Output: True
print(isinstance("Hello",object))
Special Methods
Repr
def __repr__(self):
return '{} {}'.format(self.name, self.gpa)
Special Methods
Str
def __str__(self):
return '{} {}'.format(self.fullname(), self.email())
Special Methods
Add
int.__add__(1,2)
str.__add__('BS','GIS')
def __init__(self, second_instance):
return self.gpa + second_instance.gpa

More Related Content

What's hot

PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
1.6 oo traits
1.6 oo traits1.6 oo traits
1.6 oo traits
futurespective
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
Nikhil Agrawal
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Manish Pandit
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
Synapseindiappsdevelopment
 
10 classes
10 classes10 classes
10 classes
Naomi Boyoro
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
Jazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with EclipseJazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with Eclipse
Peter Friese
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
Damian T. Gordon
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
Ramasubbu .P
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
Denis Ristic
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
sinhacp
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
VINOTH R
 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in php
selvabalaji k
 
Object concepts
Object conceptsObject concepts
Object concepts
Venkatesh Boyina
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 

What's hot (20)

PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
1.6 oo traits
1.6 oo traits1.6 oo traits
1.6 oo traits
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
10 classes
10 classes10 classes
10 classes
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Jazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with EclipseJazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with Eclipse
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in php
 
Object concepts
Object conceptsObject concepts
Object concepts
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 

Similar to Python Lecture 13

Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptx
VishwanathanS5
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
Ghulam Abbas Khan
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
java
javajava
java
jent46
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Language
ale8819
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Oop scala
Oop scalaOop scala
Oop scala
AnsviaLab
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
OOP Basic Concepts
OOP Basic ConceptsOOP Basic Concepts
OOP Basic Concepts
AmeerHamza237
 
Ch2
Ch2Ch2
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
Sasidhar Kothuru
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
Sasidhar Kothuru
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
Joshua Forman
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
Sonu WIZIQ
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
talha ijaz
 

Similar to Python Lecture 13 (20)

Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptx
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
java
javajava
java
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Language
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Oop scala
Oop scalaOop scala
Oop scala
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
OOP Basic Concepts
OOP Basic ConceptsOOP Basic Concepts
OOP Basic Concepts
 
Ch2
Ch2Ch2
Ch2
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 

More from Inzamam Baig

Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
Inzamam Baig
 
Python Lecture 12
Python Lecture 12Python Lecture 12
Python Lecture 12
Inzamam Baig
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
Inzamam Baig
 
Python Lecture 10
Python Lecture 10Python Lecture 10
Python Lecture 10
Inzamam Baig
 
Python Lecture 9
Python Lecture 9Python Lecture 9
Python Lecture 9
Inzamam Baig
 
Python Lecture 7
Python Lecture 7Python Lecture 7
Python Lecture 7
Inzamam Baig
 
Python Lecture 6
Python Lecture 6Python Lecture 6
Python Lecture 6
Inzamam Baig
 
Python Lecture 5
Python Lecture 5Python Lecture 5
Python Lecture 5
Inzamam Baig
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
Inzamam Baig
 
Python Lecture 3
Python Lecture 3Python Lecture 3
Python Lecture 3
Inzamam Baig
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
Inzamam Baig
 
Python Lecture 1
Python Lecture 1Python Lecture 1
Python Lecture 1
Inzamam Baig
 
Python Lecture 0
Python Lecture 0Python Lecture 0
Python Lecture 0
Inzamam Baig
 

More from Inzamam Baig (13)

Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
 
Python Lecture 12
Python Lecture 12Python Lecture 12
Python Lecture 12
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Python Lecture 10
Python Lecture 10Python Lecture 10
Python Lecture 10
 
Python Lecture 9
Python Lecture 9Python Lecture 9
Python Lecture 9
 
Python Lecture 7
Python Lecture 7Python Lecture 7
Python Lecture 7
 
Python Lecture 6
Python Lecture 6Python Lecture 6
Python Lecture 6
 
Python Lecture 5
Python Lecture 5Python Lecture 5
Python Lecture 5
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
Python Lecture 3
Python Lecture 3Python Lecture 3
Python Lecture 3
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
 
Python Lecture 1
Python Lecture 1Python Lecture 1
Python Lecture 1
 
Python Lecture 0
Python Lecture 0Python Lecture 0
Python Lecture 0
 

Recently uploaded

Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
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
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
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
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
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
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
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
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
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
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
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
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 

Python Lecture 13

  • 1. Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License. BS GIS Instructor: Inzamam Baig Lecture 13 Fundamentals of Programming
  • 2. Object Oriented Programming Python is a multi-paradigm programming language Objects are the basic units of object-oriented programming
  • 3. Object Real-world objects share two characteristics: They all have state and behavior Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail) Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming
  • 4.
  • 7. An object has two characteristics: attributes behavior
  • 8.
  • 9.
  • 10. Class A class is a like a template for an object. The example for class of person can be : class Person: pass
  • 11. Object An object (instance) is an instantiation of a class When class is defined, only the description for the object is defined Therefore, no memory or storage is allocated obj = Person() Here, obj is an object of class Person.
  • 12. class Person: # class attribute employer = 'Karakoram International University' # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Person class student = Person("Zeeshan", 10) teacher = Person("Fatima", 15) # access the class attributes print("student is employee of {}".format(student.employer)) print("teacher is employee of {}".format(teacher.employer)) # access the instance attributes print("{} is {} years old".format(student.name, student.age)) print("{} is {} years old".format(teacher.name, teacher.age))
  • 13. Methods Methods are functions defined inside the body of a class They are used to define the behaviors of an object
  • 14. class Student: # instance attributes def __init__(self, name, age): self.name = name self.age = age # instance method def study(self, book_name): return "{} is studying {}".format(self.name, book_name) def playing(self): return "{} is now playing".format(self.name) # instantiate the object zeeshan = Student("Zeeshan", 10) # call our instance methods print(zeeshan.study("Programming")) print(zeeshan.playing())
  • 15. Self Self keyword is used to represent an instance of the class
  • 16. Constructor Class functions that begin with double underscore __ are called special functions as they have special meaning Of one particular interest is the __init__() function. This special function gets called whenever a new object of that class is instantiated This type of function is also called constructors in Object Oriented Programming (OOP) We normally use it to initialize all the variables
  • 17. class Person: def __init__(self, name, age): self.name = name self.age = age
  • 18. Deleting Attributes Any attribute of an object can be deleted anytime, using the del statement del zeeshan.name
  • 19. class Student: # instance attributes def __init__(self, name, age): self.name = name self.age = age def get_name(self): return self.name # instantiate the object zeeshan = Student("Zeeshan", 10) del zeeshan.name student_name = zeeshan.get_name()
  • 20. Deleting Object del zeeshan Deleting objects in Python removes the name binding The object however continues to exist in memory and if no other name is bound to it, it is later automatically destroyed This automatic destruction of unreferenced objects in Python is also called garbage collection
  • 21. Student Object name = zeeshan, age = 10 Student Object name = zeeshan, age = 10 zeeshan zeeshan zeeshan = Student('Zeeshan', 10) del zeeshan
  • 22. Class Methods class methods take a cls parameter that points to the class the class method only has access to this cls argument, it can't modify object instance state class methods can still modify class state that applies across all instances of the class
  • 24. Static Methods This type of method takes neither a self nor a cls parameter static method can neither modify object state nor class state
  • 25. Inheritance Inheritance enables us to define a class that takes all the functionality from a parent class and allows us to add more The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class Inheritance models is a relationship
  • 26. class BaseClass: Body of base class class DerivedClass(BaseClass): Body of derived class
  • 28. class Person(object): # Constructor def __init__(self, name): self.name = name def get_name(self): return self.name class Student(Person): def is_registred(self): return True class Teacher(Person): def is_employeed(self): return True zeeshan = Student("Zeeshan") print(zeeshan.get_name(), zeeshan.is_registred()) adnan = Teacher("Adnan") print(adnan.get_name(), adnan.is_employeed())
  • 31. super() class Person(object): # Constructor def __init__(self, name): self.name = name def get_name(self): return self.name class Student(Person): def __init__(self, name, gpa): super().__init__(name) self.gpa = gpa def is_registred(self): return True
  • 32. Multiple Inheritance A class can be derived from more than one base class in Python, similar to C++. This is called multiple inheritance In multiple inheritance, the features of all the base classes are inherited into the derived class
  • 33. class Base1: pass class Base2: pass class MultiDerived(Base1, Base2): pass
  • 34.
  • 35. class Person: def __init__(self, name, age): self.name = name self.age = age class Player: def __init__(self, sports_name): self.sports_name = sports_name class Student(Person, Player): def __init__(self, name, age, sports_name): Person.__init__(self, name, age) Player.__init__(self, sports_name) s = Student('Adnan', 10, 'Cricket')
  • 36. Every class in Python is derived from the object class. It is the most base type in Python. So technically, all other classes, either built-in or user-defined, are derived classes and all objects are instances of the object class
  • 37. # Output: True print(issubclass(list,object)) # Output: True print(isinstance(5.5,object)) # Output: True print(isinstance("Hello",object))
  • 38. Special Methods Repr def __repr__(self): return '{} {}'.format(self.name, self.gpa)
  • 39. Special Methods Str def __str__(self): return '{} {}'.format(self.fullname(), self.email())
  • 40. Special Methods Add int.__add__(1,2) str.__add__('BS','GIS') def __init__(self, second_instance): return self.gpa + second_instance.gpa

Editor's Notes

  1. A programming paradigm is a style, or “way,” of programming.
  2. The super() builtin returns a proxy object (temporary object of the superclass) that allows us to access methods of the base class