I understand how __truediv__ works, per this answer.
But in this case:
>>> from pathlib import Path
>>> 'foo' / Path('bar')
PosixPath('foo/bar')
Surely __truediv__ is called on str, because 'foo' is the left operand to /, in that case how can it return a Path?
Here's a toy path – call it a ploth. It implements
__rtruediv__and__truediv__, so you can use it for division either way around. Sincestrdoes not implement either__rtruediv__or__truediv__,Plothgets to do that. (I'm only deriving fromstrto get a convenient constructor and__repr__().)This prints out:
From Python's docs on
__rtruediv__: