oop and scripting in python - dieearmano/python/pdf/python-03-oop.pdf · oop and scripting in...

32
Giuliano Armano 1 OOP and Scripting in Python OOP and Scripting in Python Part 2 - OOP Features DIEE - Università degli Studi di Cagliari Giuliano Armano – DIEE Univ. di Cagliari

Upload: trinhxuyen

Post on 20-Jul-2018

315 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

OOP and Scripting in PythonOOP and Scripting in Python

Part 2 - OOP Features

DIEE - Università degli Studi di Cagliari

Giuliano Armano – DIEE Univ. di Cagliari

Page 2: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Part 2 – OOP FeaturesPart 2 – OOP Features

Page 3: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Python: OOP Features

Ø Classes, Methods, and InstancesØ Methods Dispatching and BindingØ InheritanceØ PolymorphismØ Operators HandlingØ Exception handling

Page 4: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Classes, Methods, and Instances

Part 2 – OOP Features: Classes, Methods, and Instances

Page 5: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Classes, Methods, and Instances

Ø Encapsulation (= class construct) YESYES Ø Information hiding ~NONO

Page 6: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10
Page 7: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10
Page 8: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Methods Dispatching and Binding

Part 2 – OOP Features: Methods

Page 9: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Method Dispatching and Binding

Ø Method dispatching (single vs. multiple) SINGLESINGLE Ø Method binding (static vs. dynamic) DYNAMICDYNAMIC

Page 10: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Method Dispatching

>>> class Point:... def __init__(self,x=0,y=0):... self.x = x... self.y = y... def distance(self,p):... return sqrt( (self.x-p.x)**2 + (self.y-p.y)**2 )...

>>> p1 = Point(1,2)>>> p2 = Point(10,20)>>> p1.distance(p2)20.124611797498108>>> Point.distance(p1,p2)20.124611797498108>>>

Page 11: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Method Binding

>>> class Point:... def __init__(self,x=0,y=0):... self.x, self.y = x,y... def distance(self,p):... return sqrt((self.x-p.x)**2+(self.y-p.y)**2)...

>>> class CPoint(Point):... def __init__(self,x=0,y=0,color=0):... Point.__init__(self,x,y)... self.color = color...

Page 12: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Method Binding

>>> from math import *>>> p1 = CPoint()>>> p2 = Cpoint(2,2)>>>>>> print p1.distance(p2)2.82842712475>>>>>> CPoint.distance(p1,p2)2.82842712475>>>>>> Point.distance(p1,p2)2.82842712475

Page 13: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Method Binding

>>> class Blob:... def foo(self):... print 'This is Blob'...

>>> class Blob1:... def foo(self):... print 'This is Blob-one'...

Page 14: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Method Binding

>>> def foo(x):... x.foo()...

>>> a = Blob()>>> b = Blob1()>>> >>> foo(a)This is Blob>>>>>> foo(b)This is Blob-one>>>

Page 15: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Inheritance

Part 2 – OOP Features: Inheritance

Page 16: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Inheritance

Ø Interfaces NONO Ø Constructors inheritance NONO Ø Multiple inheritance YESYES

Page 17: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10
Page 18: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10
Page 19: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10
Page 20: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Polymorphism

Part 2 – OOP Features: Polymorphism

Page 21: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Polymorphism

Ø Universaln Parametric Class NONOn By Inclusion YESYES

Ø Ad-Hocn Overloading NONOn Coercion ~YES~YES

Page 22: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Inclusion Polymorphism

>>> class B:... def method1(self):... print 'method1 of B'...

>>> class D(B):... def method1(self):... print 'method1 of D'...

>>> d = D()>>> d.method1()method1 of D

Page 23: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Coercion/Conversion

Ø Conversion: >>> a = 10>>> b = float(a)>>> b10.0

Ø Coercion: >>> x = 1>>> y = 2.3>>> print x+y3.3>>>

Page 24: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Operators Handling

Part 2 – OOP Features: Exceptions Handling

Page 25: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Comparison Operators

__lt__(a, b) # a < b__le__(a, b) # a = b__eq__(a, b) # a == b__ne__(a, b) # a != b__ge__(a, b) # a = b__gt__(a, b) # a > b

Page 26: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Logical Operators

__and__(a, b) # a and b__or__(a, b) # a or b__xor__(a, b) # a xor b__not__(a, b) # not a

Page 27: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Arithmetic Operators

__add__(a, b) # a + b__sub__(a, b) # a - b__mul__(a, b) # a * b__div__(a, b) # a / b__abs__(a) # abs(a)__mod__(a, b) # a % b

Page 28: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Operators Redefinition (an example)

Ø All operators can be redefined like C++ does ...

>>> class Blob:... def __init__(self,x=0):... self.x = x... def __add__(self,y):... return self.x + y...

>>> a = Blob()>>> print a.__add__(1)1>>> print a+11

Page 29: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Exception Handling

Part 2 – OOP Features: Exceptions Handling

Page 30: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Exception Handling: Structure

try:do_something()do_something_else()

except anExceptionClass, anInstance:handle_exception(anInstance)

except anotherExceptionClass: # instance is optionalhandle_another_exception()

else: # catch-all is optionaldo_else()

For more information see the Python reference manual.

Page 31: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Exception Handling: An Example

>>> class Blob:... def __init__(self,value=0):... self.value = value... def divide(self,y):... if (y == 0): raise BlobException... else: return self.value / y...

Page 32: OOP and Scripting in Python - Dieearmano/Python/pdf/PYTHON-03-OOP.pdf · OOP and Scripting in Python Part 2 - OOP Features ... Inheritance Part 2 – OOP Features: ... print b.divide(-10

Giuliano Armano 1

Exception Handling: An Example

>>> try:... b = Blob(1)... print b.divide(10)... print b.divide(-10)... print b.divide(0)... except BlobException:... print "A Blob Exception has been raised"...

0-1A Blob Exception has been raised>>>