[Tutor] Question on how to do exponents

Steven D'Aprano steve at pearwood.info
Tue Feb 7 19:31:07 CET 2012


Sarma Tangirala wrote:

> Is is better to use pow() against **?


Advantages of **

- it is shorter to type x**y vs pow(x, y)
- being an operator, it is slightly faster than calling a function
- you can't monkey-patch it

Disadvantages of **

- being an operator, you can't directly use it as a function-object
- it can't take three arguments
- hard to google for "**"
- you can't monkey-patch it

Advantages of pow()

- it is a function, so you can pass it around as an object
- three argument form
- easy to call help(pow) to see documentation
- easy to google for "pow"
- can be monkey-patched

Disadvantages of pow()

- a tiny bit slower due to the function call
- slightly longer to type
- can be monkey-patched


Weigh up the advantages and disadvantages of each, and make the call which is 
better for you.

(My preference is to use the ** operator.)



-- 
Steven


More information about the Tutor mailing list