I would like to propose adding a `.manhattan_distance()`

method to the `complex`

type, implemented as `abs(self.real) + abs(self.imag)`

. While the implementation is trivial, I believe that this useful and commonly reimplemented code.

I’ve been doing Advent of Code this year, and I’ve already had a few occasions to use a favourite trick: using `complex`

as a 2d vector or coordinate type.

`complex`

works really well for this purpose; It supports vector addition and subtraction, and multiplication or division by a scalar. It is immutable and hashable so can be used well with sets and dicts, and even has a literal syntax, making it easy to say, `UP, DOWN, LEFT, RIGHT = -1j, 1j, -1, 1`

. It’s easy to get the length of the vector (`abs()`

), and it’s even occasionally useful to be able to convert it to a polar representation (which is in `cmath`

).

Just about the only thing that seems to come up regularly in these sorts of challenges which isn’t built in to either the type, or the `cmath`

module is finding the Manhattan distance between two coordinates (or equivalently the Manhattan distance along a vector). It’s trivial code, but I’m pretty sure I’ve implemented it a number of times, and I’m sure many others have too.

The obvious objection is that `complex`

is for representing complex numbers, and operations that don’t make sense on complex numbers don’t belong on it. I don’t know just how common it is to (ab)use `complex`

as a generic 2d vector type, but it seems just fine to me, and I think it should probably be encouraged.