Question about math.pi is mutable

Chris Angelico rosuav at gmail.com
Fri Nov 6 05:30:16 EST 2015


On Fri, Nov 6, 2015 at 1:33 PM, wangq at travelsky.com <wangq at travelsky.com> wrote:
> Hello, python-list guys:
>
>     I am a newbie of python from Beijing. China.
>     I have a question about "math.pi".
>     As you can see in the attachment, why i can modify "math.pi"?
>     (in "mathmodule.c" "pi" is a "static const double")
>
> Thank you in advance!
>
> Best Wishes!

Simply because, in Python, virtually everything can be changed. You're
welcome to go in and say "math.pi = 3" if you like... and then you
accept the consequences of that.

There are times when you want to change these sorts of 'constants'.
For example, math.pi is a float; but if you're working with
decimal.Decimal everywhere, it might be useful to replace it with a
corresponding Decimal value:

math.pi = decimal.Decimal("3.14159265358979323")

And that's perfectly legal.

ChrisA



More information about the Python-list mailing list