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

Lecture_7#_Inheritance_continues

The document covers advanced concepts in Object-Oriented Programming using Python, focusing on inheritance, access modifiers, and creating custom iterable classes. It explains the use of private and protected attributes, as well as the implementation of iterator methods like __iter__ and __next__. Additionally, it includes an exercise to create an iterable MyShoppingList class and details on using the iter() and next() functions.

Uploaded by

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

Lecture_7#_Inheritance_continues

The document covers advanced concepts in Object-Oriented Programming using Python, focusing on inheritance, access modifiers, and creating custom iterable classes. It explains the use of private and protected attributes, as well as the implementation of iterator methods like __iter__ and __next__. Additionally, it includes an exercise to create an iterable MyShoppingList class and details on using the iter() and next() functions.

Uploaded by

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

Object Oriented Programming

Using
Python

Teemu Matilainen
teemu.matilainen@savo
nia.fi
Lecture 7#

• Inheritance continues…
• You understand how to use iterators
• You know how to use comprehensions within own classes
• You are able to restrict the visibility of inherited attributes in a subclass
• You are able to restrict the visibility of inherited methods in a subclass
Inheritance … Thesis
class… Override…

The constructor within the


Thesis class invokes the
constructor in the base class
Book with the specified
arguments for name and
author.

In addition, the constructor in


the derived class assigns a
value to the attribute ‘author’.

Derived class can override


method in the base class!
Access modifiers within
base class
NoteBook with private
attributes
NoteBook with protected
attributes
Access
modifiers:
- Private
• If a trait is defined as
private in the base class,
it is not directly
accessible in any derived
classes. Let's take a look
at an example. In the
Notebook class below
the notes are stored in a
list, and the list attribute
is private:
Access
modifiers:
- Private
• If the integrity of the class is key,
making the list attribute notes
private makes sense. The class
provides the client with suitable
methods for adding and browsing
notes, after all. This approach
becomes problematic if we
define a new class NotebookPro,
which inherits the Notebook
class. The private list attribute is
not accessible to the client, but
neither is it accessible to the
derived classes. If we try to
access it, as in the find_notes
method below, we get an error:
Access
modifiers:
- Private
- Protected
• Many object oriented
programming languages
have a feature, usually a
special keyword, for
protecting traits. This
means that a trait should
be hidden from the
clients of the class, but
kept accessible to its
subclasses. Python in
general abhors
keywords, so no such
feature is directly
available in Python.
Instead, there is a
convention of marking
Access
modifiers:
- Protected
Access
modifiers:
Table

Below we have a table for the visibility of attributes with different access modifiers:

Access modifier Example Visible to client Visible to derived class

Public self.name yes yes

Protected self._name no yes

Private self.__name no no
We use for statement to iterate through different data
structures, objects and collections of items. A typical
use:

Iteration?
Creating custom iterable classes is achievable as well.
This becomes beneficial when the primary function of
the class revolves around managing a group of items.
For instance, the Bookshelf class demonstrated earlier
would be well-suited for this, allowing a for loop to
conveniently navigate through the books on the shelf. A
One more similar scenario applies to a student register, where the
ability to iterate through the collection of students
operator would be advantageous.

example and
Iteration

To enable a class to be iterable, it is necessary to


implement the iterator methods __iter__ and
__next__. We will delve into the details of these
methods in the upcoming example.
One more
operator
example and
Iteration
The __iter__ method initializes the iteration variable
or variables. In this context, a simple counter
containing the index of the current item in the list is
sufficient. Additionally, the __next__ method is also
One more required, responsible for returning the next item in
the iterator. In the aforementioned example, this
operator method returns the item at index n from the list
example and within the Antique_store object, while also
Iteration incrementing the iterator variable.

Upon traversing all objects, the __next__ method


triggers the StopIteration exception. While the
process is akin to raising other exceptions, Python
automatically handles this particular exception. Its
purpose is to notify the code invoking the iterator
(e.g., a for loop) that the iteration has concluded.
One more
operator
example and
Iteration
Exercise

Create a MyShoppingList
class and adjust the class
so that it is iterable and
can thus be used as
follows:
Exercise…

Create a MyShoppingList
class and adjust the class
so that it is iterable and
can thus be used as
follows:
Exercise…

Create a MyShoppingList
class and adjust the class
so that it is iterable and
can thus be used as
follows:
iter()

• The iter() function is used to obtain an iterator from an object that


implements the iteration protocol.

• If the object has a __iter__() method, the iter() function calls this method to
get the iterator.
• If the object doesn't have a __iter__() method but has a __getitem__()
method, iter() creates an iterator that accesses elements using integer
indices.
• If neither method is present, iter() raises a TypeError.

next()
• The next() function is used to retrieve the next item from an iterator.
• It takes an iterator as its first argument and an optional default value as its
second argument.
• If the iterator has more items, next() returns the next item; otherwise, it
raises a StopIteration exception if no default value is provided.
• If a default value is provided, next() returns the default value when the
iterator is exhausted.
Simple Iteration

You might also like