Introduce OOP in Python Tuan Vo Jan 2018
Remind the method upper() of a String ‘abc’ is data of s upper() is a method of s Note: all String can use method upper()
String is a Built-in Data Type String is called a Built- in Data Type String provides many methods (capitalize, center,count,decode,… )
The question: Does Python allow us to create a data type which can contains methods like String ?
The answer: The answer is “Yes”: Python allow us to create our data types which is called user-defined types. User- defined types might contains data and methods like String.
Introduce User- defined types in Python class Rectangle(object): width = None height = None def CalculateArea(self): return self.width * self.height box = Rectangle() box.width = 100.0 box.height = 200.0 print(box.CalculateArea()) Not worry about the syntax such as class or def CalculateArea(self) , or self Just concentrate on how it works, I’ll mention the syntax later.
Introduce User- defined types in Python A new type has been created with name: Rectangle Type Rectangle contains two data: width and height Type Rectangle contains a method : CalculateArea The main idea is to manage data and methods in one place. In this example , width and height of a Rectangle is managed along with method CalculateArea inside user- defined type Rectangle User-defined type such as Rectangle is called class in Python. Python provides much more functions for a class, will be mentioned in the next slides.
Introduce Class in Python Define a class Instance Objects
Define a class in Python Define the name for a class with key word: class Example class Rectangle: Define the data (properties or fields or attributes): Example width = None height = None Define the method: The first parameter of a method MUST be self. All properties used inside a method must be accessed via self. , such as self.width, self.height def CalculateArea(self): return self.width * self.height
Instance Objects What is object? => Object is an instance of a class or it’s simple a variable with type is the class class Rectangle: width = None height = None def CalculateArea(self): return self.width * self.height box = Rectangle()#box is an instance of class Rectangle , box is called an object box.width = 100.0 box.height = 200.0 print(box.CalculateArea()) box2 = Rectangle() #box2 is an instance of class Rectangle , box2 is called an object box2.width = 250 box2.height = 200.0 print(box2.CalculateArea())
Set of get value for attributes of an Object class Rectangle: width = None height = None def CalculateArea(self): return self.width * self.height box = Rectangle() box.width = 100.0 #set value of attribute width box.height = 200.0 #set value of attribute height print(box.width) #get value of attribute width print(box.height) #get value of attribute height The attributes of an object are sometime called properties or fields. They handle data for an object. Set value for an attribute: [NameOfObject].[NameOfAttribute] = [Value] Get value of an attribute: [NameOfObject].[NameOfAttribute] Example:
Call a method of an Object Note: Do not need to include self when calling the method of an instance of a class. Although the methods of a class always require the first parameter is self, but it’s not necessary to pass the parameter self to the method (this is syntax of Python ) box = Rectangle() box.width = 100.0 box.height = 200.0 print(box.CalculateArea()) It’s simple to call the method after the name of an instance and a dot. [NameOfObject].[NameOfMethod]([Parameters]) Example box.CalculateArea as below:
Summary: Class vs Object From now on, you can identify terms Class and Object. A class is a definition of a user-defined type A class defines its data (properties or fields, or attributes) and methods An object is an instance of a class. An object is a variable that can work in a program. A class can have many objects ( many instances ) The ability to put data and methods into a class is called Encapsulation. Encapsulation is the first characteristic of OOP. OOP stands for: Object-oriented programming The term object has been introduced. The idea of OOP is try to create User-defined types (or class ) to reflect the objects in real life when programming. Example if an programming is written to manage a school, then some classes might be created base on real life class Student: Name EnrollYear StudentCode class Course: StartDate EndDate class Subject: SubjectName
Characteristic of OOP Encapsulation is the first characteristic of OOP. There are two more characteristic: Inheritance and Polymorphism.
Introduce Inheritance in Python Inheritance is the ability to define a new class base on an existing class. The existing class is called parent class (or supper ), and the new class is called child class. See the example below: class Rectangle: width = None height = None def CalculateArea(self): return self.width * self.height class Cuboid(Rectangle): length = None def CalculateArea(self): #reuse attributes width and height from Rectangle return 2*self.width * self.height + 2*self.width* self.length + 2*self.height*self.length c = Cuboid() c.width = 10 #c reuses attribute width from Rectangle c.height = 20 #c reuses attribute height from Rectangle c.length = 5 print(c.CalculateArea())#the result will be 700
Introduce Inheritance in Python 1. The syntax to create new class: class [NameOfNewClass]([NameOfExistingClass]): Example: class Cuboid(Rectangle): 2. An object of the new class (child class) can use all public attributes of the existing class (supper/ parent class ) Example c = Cuboid(), c is an object of Cuboid and Cuboid is the child class of Rectangle, so c can use attributes width and height of Rectangle. 3. The child class can define a new method has the same name, same parameter(s) as the parent, this ability is called Overriding Methods. If an object of the child class calls the overriding method, then the method of the child class will execute not the method of the parent class. Example class Cuboid defines a new method CalculateArea which is defined in Rectangle, when calling c.CalculateArea() then the method CalculateArea of Cuboid is called not CalculateArea of Rectangle
Introduce Inheritance in Python 4. A class can be inherited from more than one class or a class might have more than one child classes. And a class can only inherit from one class. Example: Class Sharp has two child classes Rectangle and Circle class Sharp: def Draw(self): print('A Sharp is drawing') class Rectangle: width = None height = None def Draw(self): print('A Rectangle is drawing') def CalculateArea(self): return self.width * self.height class Circle: radius = None def Draw(self): print('A Circle is drawing') def CalculateArea(self): return self.radius * 3.14
Introduce Polymorphism in Python See the example below: class Sharp: def Draw(self): print('A Sharp is drawing') class Rectangle: width = None height = None def Draw(self): print('A Rectangle is drawing') def CalculateArea(self): return self.width * self.height class Circle: radius = None def Draw(self): print('A Circle is drawing') def CalculateArea(self): return self.radius * 3.14 s1 = Sharp() #s1 is declare as Sharp s1.Draw() #will print 'A Sharp is drawing' s1 = Rectangle() #s1 is now a Rectangle s1.Draw() #will print 'A Rectangle is drawing' s1 = Circle() #s1 is now a Circle s1.Draw() #will print 'A Circle is drawing'
Introduce Polymorphism in Python In the above example: - Rectangle and Circle inherit from Sharp - object s1 is first declared as an instance of Sharp - Then s1 become an instance of Rectangle - Then s1 become an instance of Circle The ability s1 can become Rectangle or Circle is called Polymorphism. In general: Polymorphism is an ability of an object of an parent class which can become instance of any of its child classes
Summary the characteristic of OOP in Python Encapsulation Inheritance Polymorphism You can see the most important characteristic is encapsulation: No encapsulation > No Inheritance No Inheritance > No Polymorphism They are all main characteristic in OOP. The next slides will introduce some more special functionalities supported in Python for OOP
Introduce Constructor in Class A constructor is a special method in class which is called when creating an object. A constructor method MUST be named __init__ A constructor method as the normal methods MUST contain the first parameter self A class can have only one constructor method class Rectangle: width = None height = None def __init__(self):#a constructor to set width and height to 0 self.width = 0 self.height = 0 def CalculateArea(self): return self.width * self.height box = Rectangle()#width and height will be set to 0 in constructor print(box.CalculateArea())#the result will be 0
Introduce Constructor in Class Example constructor method can receive parameter(s). class Rectangle: width = None height = None def __init__(self,w,h):#a constructor to set width and height when creating an object self.width = w self.height = h def CalculateArea(self): return self.width * self.height box = Rectangle(10,20)#set width to 10 and height to 20 when creating object print(box.CalculateArea())#the result will be 200
Introduce Destructor in Class A destructor is a special method in class which is called when destroying an object. A constructor method MUST be named __del__( self ) class Rectangle: width = None height = None def __del__(self):#a destructor print('A rectangle is being destroyed') def CalculateArea(self): return self.width * self.height box = Rectangle() box.width = 10 box.height = 20 print(box.CalculateArea())#the result will be 200 # string 'A rectangle is being destroyed' will be printed Content of file rectangle-4.py
Read more references to know more aspect of OOP in Python https://docs.python.org/3/tutorial/classes.html https://www.tutorialspoint.com/python3/python_classes_objects.htm https://www.python-course.eu/python3_object_oriented_programming.php http://greenteapress.com/thinkpython/html/index.html http://greenteapress.com/thinkpython/html/thinkpython016.html http://greenteapress.com/thinkpython/html/thinkpython017.html http://greenteapress.com/thinkpython/html/thinkpython018.html
Thank You Author email: tuanv2t@gmail.com

Introduce oop in python

  • 1.
    Introduce OOP inPython Tuan Vo Jan 2018
  • 2.
    Remind the methodupper() of a String ‘abc’ is data of s upper() is a method of s Note: all String can use method upper()
  • 3.
    String is aBuilt-in Data Type String is called a Built- in Data Type String provides many methods (capitalize, center,count,decode,… )
  • 4.
    The question: Does Pythonallow us to create a data type which can contains methods like String ?
  • 5.
    The answer: The answeris “Yes”: Python allow us to create our data types which is called user-defined types. User- defined types might contains data and methods like String.
  • 6.
    Introduce User- definedtypes in Python class Rectangle(object): width = None height = None def CalculateArea(self): return self.width * self.height box = Rectangle() box.width = 100.0 box.height = 200.0 print(box.CalculateArea()) Not worry about the syntax such as class or def CalculateArea(self) , or self Just concentrate on how it works, I’ll mention the syntax later.
  • 7.
    Introduce User- definedtypes in Python A new type has been created with name: Rectangle Type Rectangle contains two data: width and height Type Rectangle contains a method : CalculateArea The main idea is to manage data and methods in one place. In this example , width and height of a Rectangle is managed along with method CalculateArea inside user- defined type Rectangle User-defined type such as Rectangle is called class in Python. Python provides much more functions for a class, will be mentioned in the next slides.
  • 8.
    Introduce Class inPython Define a class Instance Objects
  • 9.
    Define a classin Python Define the name for a class with key word: class Example class Rectangle: Define the data (properties or fields or attributes): Example width = None height = None Define the method: The first parameter of a method MUST be self. All properties used inside a method must be accessed via self. , such as self.width, self.height def CalculateArea(self): return self.width * self.height
  • 10.
    Instance Objects What isobject? => Object is an instance of a class or it’s simple a variable with type is the class class Rectangle: width = None height = None def CalculateArea(self): return self.width * self.height box = Rectangle()#box is an instance of class Rectangle , box is called an object box.width = 100.0 box.height = 200.0 print(box.CalculateArea()) box2 = Rectangle() #box2 is an instance of class Rectangle , box2 is called an object box2.width = 250 box2.height = 200.0 print(box2.CalculateArea())
  • 11.
    Set of getvalue for attributes of an Object class Rectangle: width = None height = None def CalculateArea(self): return self.width * self.height box = Rectangle() box.width = 100.0 #set value of attribute width box.height = 200.0 #set value of attribute height print(box.width) #get value of attribute width print(box.height) #get value of attribute height The attributes of an object are sometime called properties or fields. They handle data for an object. Set value for an attribute: [NameOfObject].[NameOfAttribute] = [Value] Get value of an attribute: [NameOfObject].[NameOfAttribute] Example:
  • 12.
    Call a methodof an Object Note: Do not need to include self when calling the method of an instance of a class. Although the methods of a class always require the first parameter is self, but it’s not necessary to pass the parameter self to the method (this is syntax of Python ) box = Rectangle() box.width = 100.0 box.height = 200.0 print(box.CalculateArea()) It’s simple to call the method after the name of an instance and a dot. [NameOfObject].[NameOfMethod]([Parameters]) Example box.CalculateArea as below:
  • 13.
    Summary: Class vsObject From now on, you can identify terms Class and Object. A class is a definition of a user-defined type A class defines its data (properties or fields, or attributes) and methods An object is an instance of a class. An object is a variable that can work in a program. A class can have many objects ( many instances ) The ability to put data and methods into a class is called Encapsulation. Encapsulation is the first characteristic of OOP. OOP stands for: Object-oriented programming The term object has been introduced. The idea of OOP is try to create User-defined types (or class ) to reflect the objects in real life when programming. Example if an programming is written to manage a school, then some classes might be created base on real life class Student: Name EnrollYear StudentCode class Course: StartDate EndDate class Subject: SubjectName
  • 14.
    Characteristic of OOP Encapsulationis the first characteristic of OOP. There are two more characteristic: Inheritance and Polymorphism.
  • 15.
    Introduce Inheritance inPython Inheritance is the ability to define a new class base on an existing class. The existing class is called parent class (or supper ), and the new class is called child class. See the example below: class Rectangle: width = None height = None def CalculateArea(self): return self.width * self.height class Cuboid(Rectangle): length = None def CalculateArea(self): #reuse attributes width and height from Rectangle return 2*self.width * self.height + 2*self.width* self.length + 2*self.height*self.length c = Cuboid() c.width = 10 #c reuses attribute width from Rectangle c.height = 20 #c reuses attribute height from Rectangle c.length = 5 print(c.CalculateArea())#the result will be 700
  • 16.
    Introduce Inheritance inPython 1. The syntax to create new class: class [NameOfNewClass]([NameOfExistingClass]): Example: class Cuboid(Rectangle): 2. An object of the new class (child class) can use all public attributes of the existing class (supper/ parent class ) Example c = Cuboid(), c is an object of Cuboid and Cuboid is the child class of Rectangle, so c can use attributes width and height of Rectangle. 3. The child class can define a new method has the same name, same parameter(s) as the parent, this ability is called Overriding Methods. If an object of the child class calls the overriding method, then the method of the child class will execute not the method of the parent class. Example class Cuboid defines a new method CalculateArea which is defined in Rectangle, when calling c.CalculateArea() then the method CalculateArea of Cuboid is called not CalculateArea of Rectangle
  • 17.
    Introduce Inheritance inPython 4. A class can be inherited from more than one class or a class might have more than one child classes. And a class can only inherit from one class. Example: Class Sharp has two child classes Rectangle and Circle class Sharp: def Draw(self): print('A Sharp is drawing') class Rectangle: width = None height = None def Draw(self): print('A Rectangle is drawing') def CalculateArea(self): return self.width * self.height class Circle: radius = None def Draw(self): print('A Circle is drawing') def CalculateArea(self): return self.radius * 3.14
  • 18.
    Introduce Polymorphism inPython See the example below: class Sharp: def Draw(self): print('A Sharp is drawing') class Rectangle: width = None height = None def Draw(self): print('A Rectangle is drawing') def CalculateArea(self): return self.width * self.height class Circle: radius = None def Draw(self): print('A Circle is drawing') def CalculateArea(self): return self.radius * 3.14 s1 = Sharp() #s1 is declare as Sharp s1.Draw() #will print 'A Sharp is drawing' s1 = Rectangle() #s1 is now a Rectangle s1.Draw() #will print 'A Rectangle is drawing' s1 = Circle() #s1 is now a Circle s1.Draw() #will print 'A Circle is drawing'
  • 19.
    Introduce Polymorphism inPython In the above example: - Rectangle and Circle inherit from Sharp - object s1 is first declared as an instance of Sharp - Then s1 become an instance of Rectangle - Then s1 become an instance of Circle The ability s1 can become Rectangle or Circle is called Polymorphism. In general: Polymorphism is an ability of an object of an parent class which can become instance of any of its child classes
  • 20.
    Summary the characteristicof OOP in Python Encapsulation Inheritance Polymorphism You can see the most important characteristic is encapsulation: No encapsulation > No Inheritance No Inheritance > No Polymorphism They are all main characteristic in OOP. The next slides will introduce some more special functionalities supported in Python for OOP
  • 21.
    Introduce Constructor inClass A constructor is a special method in class which is called when creating an object. A constructor method MUST be named __init__ A constructor method as the normal methods MUST contain the first parameter self A class can have only one constructor method class Rectangle: width = None height = None def __init__(self):#a constructor to set width and height to 0 self.width = 0 self.height = 0 def CalculateArea(self): return self.width * self.height box = Rectangle()#width and height will be set to 0 in constructor print(box.CalculateArea())#the result will be 0
  • 22.
    Introduce Constructor inClass Example constructor method can receive parameter(s). class Rectangle: width = None height = None def __init__(self,w,h):#a constructor to set width and height when creating an object self.width = w self.height = h def CalculateArea(self): return self.width * self.height box = Rectangle(10,20)#set width to 10 and height to 20 when creating object print(box.CalculateArea())#the result will be 200
  • 23.
    Introduce Destructor inClass A destructor is a special method in class which is called when destroying an object. A constructor method MUST be named __del__( self ) class Rectangle: width = None height = None def __del__(self):#a destructor print('A rectangle is being destroyed') def CalculateArea(self): return self.width * self.height box = Rectangle() box.width = 10 box.height = 20 print(box.CalculateArea())#the result will be 200 # string 'A rectangle is being destroyed' will be printed Content of file rectangle-4.py
  • 24.
    Read more referencesto know more aspect of OOP in Python https://docs.python.org/3/tutorial/classes.html https://www.tutorialspoint.com/python3/python_classes_objects.htm https://www.python-course.eu/python3_object_oriented_programming.php http://greenteapress.com/thinkpython/html/index.html http://greenteapress.com/thinkpython/html/thinkpython016.html http://greenteapress.com/thinkpython/html/thinkpython017.html http://greenteapress.com/thinkpython/html/thinkpython018.html
  • 25.
    Thank You Author email:tuanv2t@gmail.com