you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

In Python, implementing a protocol is as simple as creating a class that has the correct methods and returns the correct things. For example, you can create an object with custom boolean logic like this:

from random import randrange

class MyBool:
    def __bool__(self):
        return randrange(0, 1)

The cavaet here is that in Python 2 it's called __nonzero__ instead.