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

Lecture_5#_Objects_as_attributes (1)

The document discusses Object Oriented Programming in Python, focusing on operator overloading and special functions that begin with double underscores. It explains how to implement various operators and comparison functions, as well as the benefits of operator overloading, such as enhanced readability and code reusability. Additionally, it covers the use of objects as attributes and introduces class methods, emphasizing their role in referencing the class itself.

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)
2 views

Lecture_5#_Objects_as_attributes (1)

The document discusses Object Oriented Programming in Python, focusing on operator overloading and special functions that begin with double underscores. It explains how to implement various operators and comparison functions, as well as the benefits of operator overloading, such as enhanced readability and code reusability. Additionally, it covers the use of objects as attributes and introduces class methods, emphasizing their role in referencing the class itself.

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/ 25

Object Oriented Programming

Using
Python

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

• You realize operator overloading


• You realize what are objects as attributes
• You understand the use of Class methods
Python Special Functions

Class functions that begin with double underscore __ are


called special functions in Python.

Function Description

__init__() initialize the attributes of the object


__str__() returns a string representation of the object
__len__() returns the length of the object
__add__() adds two objects
__call__() call objects of the class like a normal function
Python Special Functions

Similarly, we can overload other operators as well. The special


function that we need to implement is tabulated below.

Reference: https://www.programiz.com/python-programming/operator-overloading

Operator Expression Internally

Addition p1 + p2 p1.__add__(p2)
Subtractionp1 - p2 p1.__sub__(p2)
Multiplication p1 * p2 p1.__mul__(p2)
Power p1 ** p2 p1.__pow__(p2)
Division p1 / p2 p1.__truediv__(p2)
Floor Division p1 // p2 p1.__floordiv__(p2)
Remainder (modulo) p1 % p2 p1.__mod__(p2)
Bitwise Left Shift p1 << p2 p1.__lshift__(p2)
Bitwise Right Shift p1 >> p2 p1.__rshift__(p2)
Bitwise AND p1 & p2 p1.__and__(p2)
Bitwise OR p1 | p2 p1.__or__(p2)
Bitwise XOR p1 ^ p2 p1.__xor__(p2)
Bitwise NOT ~p1 p1.__invert__()
Python Special Functions…

Similarly, the special functions that we need to implement, to


overload other comparison operators are tabulated below.

Reference: https://www.programiz.com/python-programming/operator-overloading

Operator Expression Internally

Less than p1 < p2 p1.__lt__(p2)


Less than or equal to p1 <= p2 p1.__le__(p2)
Equal to p1 == p2 p1.__eq__(p2)
Not equal top 1 != p2 p1.__ne__(p2)
Greater than p1 > p2 p1.__gt__(p2)
Greater than or equal to p1 >= p2 p1.__ge__(p2)
How to
implement
the operator
How to
implement
the
operator…
How to
implement
the
operator…
How to
implement the
operator…

Let’s add
__eq__ to
Engine class.
One more
operator
example…
What is a
__call__
operator?
What is a
__call__
operator?

This can be useful in situations where you


want instances of your class to be callable,
providing a more function-like behavior for
objects.
Benefits of Operator Overloading:

Operator overloading offers several advantages, including:

Enhanced Code Readability:

It enhances code readability by enabling the utilization of familiar operators,


contributing to a more intuitive understanding of the code.

Consistent Object Behavior:

Ensures that instances of a class behave consistently with both built-in types and
other user-defined types, promoting a uniform and predictable experience.

Simplified Code Writing:

Streamlines code writing, particularly for intricate data types, making the
codebase more straightforward and easier to comprehend.

Code Reusability:

Facilitates code reuse through the implementation of a single operator method,


which can be employed for multiple operators, promoting efficiency in
development.
Objects as attributes

We've previously explored instances of classes with lists as


attributes. Since there are no restrictions preventing us from
incorporating mutable objects as attributes in our classes,
we can seamlessly employ instances of our own classes as
attributes within other classes we've created. In the
upcoming examples, we'll introduce the Book, Member, and
BorrowedBook classes. The BorrowedBook class utilizes the
first two classes. The class definitions are intentionally brief
and straightforward to emphasize the practice of utilizing
instances of our own classes as attributes.
Let’s define the
class Book in a
file named
book.py:
Next, the
class
Member
• Finally, we create the class
BorrowedBook. This class uses the
other two classes, they have to be
imported before they can be used:
• Here is an
example of a
main function
which will add
some
borrowed
books to a list:
What does these dots mean in a print(book.member.name)?

- book is an instance of the class BorrowedBook


- member refers to an attribute of the BorrowedBook object, which is an object of type Member
- the attribute name in the Member object contains the name of the member
When do we need import statement?

An import statement is only necessary when using


code which is defined outside the current file (or
Python interpreter session). This includes
situations where we want to use something defined
in the Python standard library.
Create an Author class

A list of
objects as
an attribute
of an
object???

Now we add new


methods to a Book
class and then we
will use the created
Author class
An
example
of our
class in
action:
Using nested loops and nested
object lists…
What is Class Method?
We know already how to use:

- Instance method
- Static methods
- …and also, Dynamic method

A class method is declared using the


@classmethod decorator, where the first
parameter is consistently named cls. The
usage of cls is analogous to the self
parameter, with the distinction being
that cls references the class itself, while
self refers to an instance of the class.
Neither parameter needs to be explicitly
provided in the argument list when
calling the function; Python
automatically assigns the appropriate
values.
Class Method
continues?

Copying an object…

You might also like