cltl python course: object oriented programming (1/3)

19
CLTL Python Course Object Oriented Programming in Python 1 st session April 24 th 2013

Upload: ruben-izquierdo-bevia

Post on 15-Jul-2015

92 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CLTL python course: Object Oriented Programming (1/3)

CLTL Python Course

Object Oriented Programming in Python1st session

April 24th 2013

Page 2: CLTL python course: Object Oriented Programming (1/3)

What is Object Oriented Programming ?

● Change of paradigm Procedural vs. Data oriented● Procedural programming

– Focus on functions or procedures

– Usually process and data mixed in the code

– Python basic datatypes (lists, dicts, tuples...)● Data Oriented programming

– Focus is the data

– Process and data well defined and separated

– Datatypes as complex as we need

Page 3: CLTL python course: Object Oriented Programming (1/3)

Main points of the Object Oriented Programming

● Objects, classes, attributes, methods ...

● Reusability

● Data abstraction

● Encapsulation / data hiding

● Natural way of thinking

● Inheritance

Page 4: CLTL python course: Object Oriented Programming (1/3)

Classes + Objects

● What is a Class ?

– Concept

– Data type

– Template

– ....● Objects are instances of a class

CLASS = Person OBJECT = Ruben

● We can define our own classes !

Page 5: CLTL python course: Object Oriented Programming (1/3)

Examples of Classes

PERSON

● Attributes

● First name● Last name● Mail address● Birth date

● Methods

● Send a mail● Ask if the person is

over 18

BANK ACCOUNT

● Attributes

● PERSON● Number● Balance

● Methods

● Withdrawal● Transfer● Deposit

Page 6: CLTL python course: Object Oriented Programming (1/3)

Examples of Objects

PERSON

● John Smith

● Laura Ingells

● Marc van Gaal

BANK ACCOUNT

● Account 12341234

● Account 73763234

● Account 87388234

All these objects have the ATTRIBUTES and METHODS defined by the class

There is no any SEMANTIC in this, we defined the CLASS

Page 7: CLTL python course: Object Oriented Programming (1/3)

Python: Classes and objects

● Define a class in python:

● Define a property for the class (indentation)

● Create an objet of this class

● Access to a property or method of the object

Page 8: CLTL python course: Object Oriented Programming (1/3)

Python: Class Methods

● Are basically like a function but defined within a class

● They can be called through objects of the class

● ALL the class methods ALWAYS have as first parameter the object itself, and it's called self

Page 9: CLTL python course: Object Oriented Programming (1/3)

Python: Manipulating data

● The class methods manipulate attributes of the objects

● These methods access to the data through the self parameter

Page 10: CLTL python course: Object Oriented Programming (1/3)

Python: Type of Methods

● 'Getter' methods

– To get some information (attribute) from the object● 'Setter' methods

– To set some information (attribute) to the object● Processing methods

– To do some stuff● Special methods

– Overloading methods

– The __init__ method

Page 11: CLTL python course: Object Oriented Programming (1/3)

Python: __init__ method

● Always with that name _ _ init _ _ it's the constructor of the class

● Used for define and initialize attributes

● It's called AUTOMATICALLY when we create the object

Page 12: CLTL python course: Object Oriented Programming (1/3)

Python: __init__ method II

● Useful ! --> __init__ can get as many extra parameters as we need

● Exercise --> Create a method for the function call print_pet, that prints the data of the pet

Page 13: CLTL python course: Object Oriented Programming (1/3)

Classes and objects. Advantages

● Code more clean, easy to reuse and debug and well structured

● Data encapsulation

– You can make parts of your data “private”

– Variables starting with __ are private

● Data abstraction

– Data access through the methods, no matter how you store the data internally

– Class Person:● Attribute age as INTEGER or DATE● Method is_over_18(...) --> True/False

● Reusability of code

– Classes can be stored in one file and used used in N different files (import statement)

Page 14: CLTL python course: Object Oriented Programming (1/3)

Classes and objects

● Highly recommended when you deal with complex data (and even with simple data)

– You could represent the 'pet' as a tuple

– Now you decide to include more attributes for your pet

– Now you want to store also the information of the father, which is another PET

– Now ... impossible to manage anymore :)

Page 15: CLTL python course: Object Oriented Programming (1/3)

Classes and objects

Page 16: CLTL python course: Object Oriented Programming (1/3)

Inheritance

● One class can inherit from another class (more than one)

– General class --> parent / base class

– Specialized class --> subclass

● Similar to hyperonym relation in WordNet :)

Page 17: CLTL python course: Object Oriented Programming (1/3)

Inheritance II

● Subclasses can:

– Reuse data/methods from base classes

– Extend the functionality of base classes

– Modify the behaviour of base classes

● Advantages of inheritance:

– Reuse of code

– Logic way of structure our classes

– Code more clean and easy to debug

– ....

Page 18: CLTL python course: Object Oriented Programming (1/3)

Inheritance III

● How can we use inheritance in python:

● Our example:

● Both dog and pet are classes

● Dog is a subclass of pet

● All that a pet can do, also a dog can do it

● NOT all that a dog can do, a pet can do it

Page 19: CLTL python course: Object Oriented Programming (1/3)

Inheritance IV. Exercise :)

● Extend the pet class with a method to print the name of the dog

● Create one object of 'pet' and one of 'dog'

● Which methods and data has any of the objects available?