Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Special Methods in Python, Slides of Advanced Computer Programming

True and false values.​​ In fact, all objects in Python have a truth value. By default, objects are considered to be true, but the special __bool__ method can be ...

Typology: Slides

2021/2022

Uploaded on 09/27/2022

linden
linden 🇬🇧

4.4

(8)

217 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Special Methods in Python
The built-in mathematical operators can be extended in much the same way as repr;
there are special method names corresponding to Python operators for arithmetic,
logical, and sequence operations.
To make our code more legible, we would perhaps like to use the + and * operators
directly when adding and multiplying complex numbers. Adding the following methods to
both of our complex number classes will enable these operators to be used, as well as
the add and mul functions in the operator module:
>>> ComplexRI.__add__ = lambda self, other:
add_complex(self, other)
>>> ComplexMA.__add__ = lambda self, other:
add_complex(self, other)
>>> ComplexRI.__mul__ = lambda self, other:
mul_complex(self, other)
>>> ComplexMA.__mul__ = lambda self, other:
mul_complex(self, other)
Now, we can use infix notation with our user-defined classes.
>>> ComplexRI(1, 2) + ComplexMA(2, 0)
ComplexRI(3.0, 2.0)
>>> ComplexRI(0, 1) * ComplexRI(0, 1)
ComplexMA(1.0, 3.141592653589793)
True and false values. We saw previously that numbers in Python have a truth value;
more specifically, 0 is a false value and all other numbers are true values. In fact, all
objects in Python have a truth value. By default, objects are considered to be true, but
the special __bool__ method can be used to override this behavior. If an object defines
the __bool__ method, then Python calls that method to determine its truth value.
As an example, suppose we want the complex number 0 + 0 * i to be false. We can
define the __bool__ method for both our complex number implementations.
pf3
pf4

Partial preview of the text

Download Special Methods in Python and more Slides Advanced Computer Programming in PDF only on Docsity!

Special Methods in Python

The built-in mathematical operators can be extended in much the same way as repr; there are special method names corresponding to Python operators for arithmetic, logical, and sequence operations.

To make our code more legible, we would perhaps like to use the + and * operators directly when adding and multiplying complex numbers. Adding the following methods to both of our complex number classes will enable these operators to be used, as well as the add and mul functions in the operator module:

>>> ComplexRI.add = lambda self, other:

add_complex(self, other)

>>> ComplexMA.add = lambda self, other:

add_complex(self, other)

>>> ComplexRI.mul = lambda self, other:

mul_complex(self, other)

>>> ComplexMA.mul = lambda self, other:

mul_complex(self, other)

Now, we can use infix notation with our user-defined classes.

>>> ComplexRI(1, 2) + ComplexMA(2, 0)

ComplexRI(3.0, 2.0)

>>> ComplexRI(0, 1) * ComplexRI(0, 1)

ComplexMA(1.0, 3.141592653589793)

True and false values. We saw previously that numbers in Python have a truth value; more specifically, 0 is a false value and all other numbers are true values. In fact, all objects in Python have a truth value. By default, objects are considered to be true, but the special bool method can be used to override this behavior. If an object defines the bool method, then Python calls that method to determine its truth value.

As an example, suppose we want the complex number 0 + 0 * i to be false. We can define the bool method for both our complex number implementations.

>>> ComplexRI.bool = lambda self: self.real != 0 or

self.imag != 0

>>> ComplexMA.bool = lambda self: self.magnitude != 0

We can call the bool constructor to see the truth value of an object, and we can use any object in a boolean context.

>>> bool(ComplexRI(1, 2))

True

>>> bool(ComplexRI(0, 0))

False

>>> if not ComplexMA(0, 1):

print("complex number is true")

complex number is true

Sequence length. We have seen that we can call the len function to determine the length of a sequence.

>>> len('Go Bears!')

The len function invokes the len method of its argument to determine its length. All built-in sequence types implement this method.

>>> 'Go Bears!'.len()

Python uses a sequence's length to determine its truth value, if it does not provide a bool method. Empty sequences are false, while non-empty sequences are true.

>>> bool('')

False

>>> bool([])

False

>>> bool('Go Bears!')

True

Callable objects. In Python, functions are first-class objects, so they can be passed around as data and have attributes like any other object. Python also allows us to define

the left operand, then checks for an radd method on the value of the right operand. If either is found, that method is invoked with the value of the other operand as its argument.