I’m wondering where I find the source to show how the operator ** is implemented in Python. Can someone point me in the right direction?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The python grammar definition (from which the parser is generated using pgen), look for ‘power’: Gramar/Gramar
The python ast, look for ‘ast_for_power’: Python/ast.c
The python eval loop, look for ‘BINARY_POWER’: Python/ceval.c
Which calls PyNumber_Power (implemented in Objects/abstract.c):
Essentially, invoke the pow slot. For long objects (the only default integer type in 3.0) this is implemented in the long_pow function Objects/longobject.c, for int objects (in the 2.x branches) it is implemented in the int_pow function Object/intobject.c
If you dig into long_pow, you can see that after vetting the arguments and doing a bit of set up, the heart of the exponentiation can be see here:
Which uses algorithms discussed in Chapter 14.6 of the Handbook of Applied Cryptography which describes efficient exponentiation algorithms for arbitrary precision arithmetic.