Question about math.pi is mutable

Bartc bc at freeuk.com
Sat Nov 7 06:23:59 EST 2015


On 07/11/2015 03:43, Ben Finney wrote:
> Bartc <bc at freeuk.com> writes:
>
>> Is there no way then in Python to declare:
>>
>>     pi = 3.141519     # etc
>>
>> and make it impossible to override?
>
> No, and it would be a bad thing if that were something a library author
> could forbid.
>
> Python assumes the programmers using it are consenting adults. Doing
> harmful things is difficult but not forbidden.

But surely it can't hurt to ensure certain values can't be changed 
accidentally or maliciously?

Some dynamic languages (OK, I'm thinking of mine) simply allow you to write:

    const pi = 3.14159

Then this can't be changed, because it would be the equivalent of writing:

    3.14159 = 0

What would be the harm in this?

It's a very simple idea, yet many people and languages either don't like 
it or can't grasp it. Not even C has the feature, if you don't count 
using the crude #define for the purpose.

In the case of Python, it could have allowed 'constant folding', so that 
math.pi*2 need not be evaluated at runtime.

(Python however does make it quite difficult to add features like this. 
Mostly because such names are not visible across modules when compiling.

Also it is still possible to write math = some_other_module.)

-- 
Bartc



More information about the Python-list mailing list