# How To Write Pythonic Objects

### Write objects that function the same as those in Python's standard libraries.

Join the DZone community and get the full member experience.

Join For Free## Introduction

Before we start exploring how to write a Pythonic Object, let us start by making it clear what I mean by that term. It is not about *PEP8 *and respecting its rules to write beautiful pythonic code; rather it's about writing objects that make maximum use of the concepts of the Python data model, so they can be used as naturally as the Python standard library objects.

The idea is to inject Python ADN in our user-defined objects to make them mutate and behave as native Python objects. To do so, we will implement a `Vector`

class to represent a multidimensional vector.

The code below represents the `Vector`

class with its minimal implementation. A `Vector`

is represented by its coordinates.

```
from array import array
class Vector:
__arrayType = "d"
def __init__(self, coordinates):
self.__coordinates = array(self.__arrayType, coordinates)
if __name__ == "__main__":
v = Vector([1, 2, 3])
print(v)
#<__main__.Vector object at 0x0082F610>
v1 = Vector((1, 2, 3))
print(v1)
#<__main__.Vector object at 0x0317FC88>
```

The Vector coordinates are stored in a float array; notice the `__arrayType = "d"`

that imposes the type of elements within the array to *floats*. We can pass any iterable to the constructor of `Vector`

since the constructor of the array uses as an internal container that accepts any *iterable *(tuples, lists, etc.).

You may also like: Class Attribute vs. Instance Attribute In Python: What You Might Have Missed.

## A Pythonic Representation

When we print a `Vector`

object itself, note that we get its reference (memory address with CPython) and not its coordinates. Let us change that by implementing the `__str__`

method within our class to have a more friendly output like for example `(x, y, z, ..)`

.

```
from array import array
class Vector:
....
def __str__(self):
return str(tuple(self.__coordinates))
....
if __name__ == "__main__":
v = Vector([1, 2, 3])
print(v)
#(1.0, 2.0, 3.0)
```

Automatically, when the `print`

is called with a Vector object, the* __str__ *method is executed to get the string to be printed. Notice that we used the string representation of a tuple created from the array

*.*

* __str__ *is not the only the method the Python data model uses to print objects;

*is also used to provide a representation of the object more oriented for debugging purposes. This representation can be evaluated to create the same object with the*

`__repr__`

`eval`

function.For further details about the differences and the use cases of `__str__ `

and * __repr__ *you can refer to the Python: __str__( ) vs. __repr__( ) article.

```
from array import array
import reprlib
class Vector:
....
def __repr__(self):
s = reprlib.repr(self.__coordinates)
return "{}({})".format(self.__class__.__name__, s[s.index('['):-1])
...
if __name__ == "__main__":
v = Vector([1, 2, 3])
s = repr(v)
print(s)
#Vector([1.0, 2.0, 3.0])
v1 = eval(s)
print(v1)
#(1.0, 2.0, 3.0)
v2 = Vector(range(100))
print(repr(v2))
#Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
```

In the previous code block, notice that the returned value of `repr`

* *when used with `eval`

* *permits to create a new `Vector`

. The use of `reprlib`

* *allows us not to print all the elements of the array in case it contains too many elements and replace them with.... like the vector `v2`

.

## A Pythonic Iteration

To ensure that we can loop on our vectors and that we can unpack them, we need to make them iterables. To do so, the `__iter__`

method must be added to our class.

```
class Vector:
...
def __iter__(self):
return iter(self.__coordinates)
...
if __name__ == "__main__":
v = Vector([1, 2, 3])
for i in v:
print(i)
#1.0
#2.0
#3.0
t = tuple(v)
print(t)
#(1.0, 2.0, 3.0)
```

## A Pythonic Length Calculation

In order to have the capability to get the number of coordinates within our vector by assigning our objects to the `len()`

function, the * __len__ *method must be added to our class.

```
from array import array
import reprlib
class Vector:
...
def __len__(self):
return len(self.__coordinates)
...
if __name__ == "__main__":
v = Vector([1, 2, 3, 4])
print(len(v))
#4
```

## A Pythonic Comparaison

Without adapting our class to support comparison, the* == *operator applied to two vector objects compare their references. To alter this behaviour, the

`__eq__`

method must be implemented. For our example, two vectors are equal if and only if they have the same coordinates and with the same order.```
from array import array
import reprlib
class Vector:
...
def __len__(self):
return len(self.__coordinates)
def __iter__(self):
return iter(self.__coordinates)
def __eq__(self, other):
if len(self) == len(other):
for i, j in zip(self, other):
if i != j:
return False
return True
else:
return False
...
if __name__ == "__main__":
v = Vector([0, 1, 2, 3, 4])
v1 = Vector((1, 2, 3, 4, 5))
v2 = Vector(range(5))
print(v == v2)
#True
print(v == v1)
#False
```

Let us take some time to analyze the new* *`__eq__`

method:

It uses the

`__len__`

method by calling the`len()`

It uses the

`__iter__`

method by passing`self`

and other parameters to the zip function, which accepts an iterable as parameters.

## Pythonic Absolute value

For this example, we use the ` __abs__`

method to return the Euclidean norm of a vector defined by the below expression:

*Euclidean Distance*

```
from array import array
from math import sqrt
import reprlib
class Vector:
...
def __abs__(self):
return sqrt(sum((x**2 for x in self)))
...
if __name__ == "__main__":
v = Vector([0, 1, 2, 3, 4])
a = abs(v)
print(a)
#5.477225575051661
```

## Pythonic Boolean Evaluation

With our current implementation of the `Vector`

class, we have the below behaviour when we evaluate the boolean value of our vectors.

```
if __name__ == "__main__":
v = Vector([])
print(bool(v))
#False
v1 = Vector([1, 2, 3])
print(bool(v1))
#True
```

Without the `__bool__`

* *method in our class, the call to the `bool()`

function refers to the `__len__`

method. If the length is equal to 0, then the object evaluates to false — otherwise, it evaluates to true.

Let us change this behaviour by implementing a method to have it return True if the vector Euclidean norm is different from 0 and false otherwise.

```
from array import array
from math import sqrt
import reprlib
class Vector:
...
def __abs__(self):
return sqrt(sum((x**2 for x in self)))
def __bool__(self):
return bool(abs(self))
...
if __name__ == "__main__":
v = Vector([])
print(bool(v))
#False
v1 = Vector([1, 2, 3])
print(bool(v1))
#True
v2 = Vector([0, 0])
print(bool(v2))
# False
```

## Pythonic Slicing

The slicing in Python aims to get a subset from an initial set by indicating the index of an element to retrieve it or by indicating a slice.

A slice of an object returns another object of the same type. The method `__getitem__`

is the one to be updated to give our vector objects this ability.

```
from array import array
from math import sqrt
import reprlib
class Vector:
...
def __getitem__(self, item):
if isinstance(item, int):
return self.__coordinates[item]
elif isinstance(item, slice):
return self.__class__(self.__coordinates[item])
else:
raise IndexError("{} indexes must be integers".format(type(self).__name__))
...
if __name__ == "__main__":
v = Vector([1, 2, 3, 4])
v1 = v[1]
print(v1)
# 2.0
v2 = v[0:3]
print(type(v2))
# <class '__main__.Vector'>
print(v2)
# (1.0, 2.0, 3.0)
```

Note that by using a slice, the returned object is also a `Vector`

object.

## Conclusion

The Dunder methods that we implemented in this tutorial are not the only ones that can be used. Others, like `__bytes__`

, `__hash__`

, `__getatrr__`

, and `___format__`

can be used to alter the behaviour of the user defined objects. These methods are not all to be implemented every time you define a new class, it depends on your needs and this is the beauty of the Python data model.

## Further Reading

Opinions expressed by DZone contributors are their own.

Comments