python pathlib operator ‘/’ – how does it do it?

The Path class has a __truediv__ method that returns another Path. You can do the same with your own classes:

>>> class WeirdThing(object):
        def __truediv__(self, other):
            return 'Division!'

>>> WeirdThing() / WeirdThing()
'Division!'

Leave a Comment