© Joannah Nanjekye 2017
Joannah NanjekyePython 2 and 3 Compatibilityhttps://doi.org/10.1007/978-1-4842-2955-2_9

9. Custom Behavior of Classes

Joannah Nanjekye
(1)
Kampala, Uganda
 
In Python, class method names that start and end with __ are referred to as special methods because they allow us to customize the way Python will use our classes. In this chapter, we look at some of these methods and learn how to achieve compatibility in both Python 2 and 3. We first look at the custom iterator methods (__iter__ and __next__) and then later discuss the __str__, and __nonzero__ methods.

Custom Iterators

Iterators in Python and other languages are objects that can be iterated. Iterators are all over Python. They have been subconsciously implemented in ...

Get Python 2 and 3 Compatibility: With Six and Python-Future Libraries now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.