[Numpy-discussion] How the a**T trick works

Paul F Dubois paul at pfdubois.com
Fri Mar 8 08:46:04 CST 2002


I confess I admire the a**T suggestion for a notation for transpose(a).
The question was raised as to whether this really works and an empirical
proof was offered that it does. Here is how it works.

In a**T the first thing tried is to ask a.__pow__ if it can manage with
T as an argument. The array says, hmm, T is an instance of a class that
I never heard of, and it doesn't have an __array__ attribute to call in
order to make it an array. I pass. Then T's class' __rpow__ is given a
turn, and asked if it can do the job with a as an argument, which it
can.

This is how 2**a works, or 2 - a, too. The int type disavows any
knowledge of what to do, so the other operand gets a chance to save the
day. So this is a "trick" we use every day.





More information about the Numpy-discussion mailing list