Dynamic Language
Lecture 6
Object Oriented Programming in
Python
Mohamed Mead
What is an Object?
• A software item that contains variables and methods
• Object Oriented Design focuses on
– Encapsulation:
dividing the code into a public interface, and a private implementation
of that interface
– Polymorphism:
the ability to overload standard operators so that they have
appropriate behavior based on their context
– Inheritance:
the ability to create subclasses that contain specializations of their
parents
OOP, Defining a Class
• Declaring a class:
class name:
statements
– Example:
class Point:
x = 0
y = 0
# main
p1 = Point()
p1.x = 2
p1.y = -5
Classes
Classes describe data and provide methods to manipulate
that data
Classes model the behavior of objects in the "real" world.
Methods implement the behaviors of these types of
objects.
Member variables hold (current) state.
Classes enable us to implement new data types in Python.
Classes
Classes
1. The class is made up of attributes and methods.
2. Attributes and methods are simply defined as normal
variables and functions.
3. As noted in the corresponding docstring, the __init__()
method is called the initializer. It's equivalent to the
constructor in other object oriented languages, and is
the method that is first run when you create a new
object, or new instance of the class.
4. Attributes that apply to the whole class are defined
first, and are called class attributes.
Classes
5. Attributes that apply to a specific instance of a class
(an object) are called instance attributes. They are
generally defined inside __init__(); this is not
necessary, but it is recommended (since attributes
defined outside of __init__() run the risk of being
accessed before they are defined).
Classes
6. Every method, included in the class definition passes
the object in question as its first parameter. The word
self is used for this parameter (usage of self is actually
by convention, as the word self has no inherent meaning
in Python, but this is one of Python's most respected
conventions, and you should always follow it).
Classes
7. Those used to object-oriented programming in other
languages may be surprised by a few things. One is that
Python has no real concept of private elements, so
everything, by default, imitates the behavior of the
C++/Java public keyword.
Classes
8. Some of the class's methods have the following form:
__functionname__(self, other_stuff). All such methods
are called "magic methods" and are an important part of
classes in Python. For instance, operator overloading in
Python is implemented with magic methods.
Magic methods in Python are the special methods which
add "magic" to your class. Magic methods are not meant to
be invoked directly by you, but the invocation happens
internally from the class on a certain action.
Classes
Now let's make a few instances of our Person class!
>>> # Instances
>>> kelly = Person("Kelly")
>>> joseph = Person("Joseph")
>>> john_doe = Person("John Doe")
We currently have three Person objects, kelly, joseph, and
john_doe.
We can access the attributes of the class from each
instance using the dot operator . Note again the
difference between class and instance attributes:
Classes
>>> kelly.species
'Homo Sapiens'
>>> john_doe.species
'Homo Sapiens'
>>> joseph.species
'Homo Sapiens'
>>> kelly.name
'Kelly'
>>> joseph.name
'Joseph'
Classes
We can execute the methods of the
class using the same dot operator .:
>>> # Methods
>>> john_doe.__str__()
'John Doe'
>>> print(john_doe)
'John Doe'
>>> john_doe.rename("John")
'Now my name is John'