cltl python course: object oriented programming (1/3)
TRANSCRIPT
CLTL Python Course
Object Oriented Programming in Python1st session
April 24th 2013
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
Main points of the Object Oriented Programming
● Objects, classes, attributes, methods ...
● Reusability
● Data abstraction
● Encapsulation / data hiding
● Natural way of thinking
● Inheritance
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 !
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
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
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
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
Python: Manipulating data
● The class methods manipulate attributes of the objects
● These methods access to the data through the self parameter
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
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
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
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)
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 :)
Classes and objects
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 :)
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
– ....
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
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?