0% found this document useful (0 votes)
5 views

Lecture_2#_Classes_and_Objects (1)

The document provides an overview of Object-Oriented Programming (OOP) concepts using Python, including classes, objects, attributes, methods, encapsulation, inheritance, and polymorphism. It explains how to define a class, create instances, and implement methods, along with practical exercises for creating classes such as 'Cat' and 'ShoppingList.' The document emphasizes the importance of these concepts in structuring and organizing code in a modular and reusable manner.

Uploaded by

Dong Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture_2#_Classes_and_Objects (1)

The document provides an overview of Object-Oriented Programming (OOP) concepts using Python, including classes, objects, attributes, methods, encapsulation, inheritance, and polymorphism. It explains how to define a class, create instances, and implement methods, along with practical exercises for creating classes such as 'Cat' and 'ShoppingList.' The document emphasizes the importance of these concepts in structuring and organizing code in a modular and reusable manner.

Uploaded by

Dong Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Object Oriented Programming

Using
Python

Teemu Matilainen
teemu.matilainen@savonia.fi
Lecture 2#

You realize what a class is


You are able to define a class
You realize what an object is
You are able to create instances of a class
You realize what an attribute is
You are able to create attributes into a class
You realize what a method is

Classes and objects

In object-oriented programming (OOP), classes and objects are fundamental concepts.


They provide a way to structure and organize code in a more modular and reusable
manner. Let's explore the concepts of classes and objects in Python.
Classes:

A class is a blueprint or a template (abstract: dog) for creating objects. It defines a set
of attributes (data) and methods (functions) that the objects of the class will have. In
Python, a class is created using the class keyword.
Simple class:

Now we create a simple skeleton of a class. Class does not do anything… yet.

The provided code indicates to Python that a class called


Simple_class is being defined. Although the class currently lacks any
functionality, we can still instantiate an object based on it.

Now, consider a program in which two variables, 'name' and 'age,' are
added to a Simple_class object. Any variables associated with an
object are referred to as its attributes, specifically, data attributes or,
at times, instance variables.

These attributes linked to an object can be retrieved through the


object
Simple
Class
continues:

Output:
In this
example:
• Dog is a class with
attributes name and
age, and a method bark.
• The init method is
a special method called
the constructor, which
initializes the object's
attributes when an
instance is created.
• my_dog is an instance
(object) of the Dog class.
Exercise:

• Create a class cat with the name and age.


Cat class has a method Miau and parameter
what makes the cat miau as many time as
the input argument defines.
One
solution:

Output
Objects:

An object is an instance of a class. It is a concrete realization of the class, with its own
unique state (attribute values). Objects can perform actions (methods) defined by the
class. In the example above, my_dog is an object of the Dog class.
In Python, an object is an instance of a class, and a class is a blueprint for creating
objects. Objects can have associated methods, which are functions that are defined
within the class and can operate on the object's attributes.

Methods are essentially functions that are bound to the object and can access and
modify its state.
Instantiation:
The process of creating an object from a class is called instantiation. In the example,
my_dog = Dog(name=’Nino', age=3) is an instantiation of the Dog class.

Encapsulation:
Encapsulation is the bundling of data and methods that operate on the data within a single unit,
i.e., a class. The attributes and methods of a class are encapsulated within that class, providing
a way to control access and modification.

Inheritance:
Inheritance is a mechanism where a new class (subclass or derived class) can inherit attributes
and methods from an existing class (base class or parent class). It promotes code reuse and
extensibility.

Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common base
class. It enables flexibility and extensibility in code.
These concepts collectively form the foundation of object-oriented programming and are
widely used in Python and many other programming languages.
• Compile-Time Polymorphism
(Static Binding):

• Achieved through method


overloading and operator
overloading.
• Python does not support method
overloading in the same way as
some statically-typed languages
like Java or C++, but you can
achieve similar behavior using
default argument values or
variable-length argument lists.

• Example of method overloading in


Python (using default argument
values):
Encapsulation
example:

• In this example, Dog is a class


with two methods: bark and
celebrate_birthday. The
• init method is a special
method called the constructor,
which is executed when a new
instance of the class is created.
• The bark method is a simple
method that prints a message
using the name
• attribute of the Dog object.
• The celebrate_birthday
method increments the
age attribute by 1 and
prints a message.
When we create an instance of the Dog class (my_dog), we can call its methods and
access its attributes. The methods operate on the object's state, and each method has
access to the object's attributes through the self parameter.
Understanding objects and methods is essential in object-oriented programming, as it
allows you to model and encapsulate behavior within classes, leading to more
organized and modular code.
Function to
Display Object
Information:

• Functions that work


with objects in Python
can interact with the
attributes and methods
of those objects. Here
are some examples of
functions that work with
objects:
Function to
Calculate
Area of a
Rectangle
Object:
Function to
Compare
Objects:
Later in the
program you
can still change
the initial
values of the
data attributes:
These examples illustrate how functions can take objects as
parameters, access their attributes, and invoke their methods
to perform various actions. This is a common pattern in
object-oriented programming, promoting modularity and
code organization.
Working with an object of type e.g. date you may have noticed that there is a slight
difference between how the variables contained in the object are accessed, as opposed
to how the methods attached to the objects are used:
• Assignment 3#:

• Write a function named


list_all_years(dates: list) which
takes a list of date type objects
as its argument. The function
should return a new list, which
contains the years in the
original list in chronological
order, from earliest to latest.

• An example of the function in


action:
• Assignment 4#:

• This ShoppingList class


has methods for adding
items, removing items,
getting the count of
unique items, getting
the total units, and
displaying the current
shopping list etc.
• Create a ShoppingList
class which has several
methods item_count,
add_item, unit_count
etc.

• Here is partially
created shopping list
class. Fill the gaps…

You might also like