Question about math.pi is mutable
Bartc
bc at freeuk.com
Fri Nov 6 14:30:54 EST 2015
On 06/11/2015 16:12, Lorenzo Sutton wrote:
>
>
> On 06/11/2015 13:30, Bartc wrote:
>> On 06/11/2015 02:33, 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")
>>
>> Python isn't C.
>>
>> Your attachment isn't visible, but it can be demonstrated easily:
>>
>> import math
>>
>> math.pi=0
>>
>> print (math.pi)
>>
>> In Python, presumably 'pi' is just another variable, and variables can
>> be written to.
>>
>> (Perhaps math.pi would be better off as a function.)
>
> Still nothing stops you from doing:
>
> math.sin = 0
>
> If you really want to?
Well, you might notice something is wrong when you try and do math.sin(x)!
But I see your point. Even a function can be set to another function, or
to one that returns a nonsense value.
Is there no way then in Python to declare:
pi = 3.141519 # etc
and make it impossible to override?
--
Bartc
More information about the Python-list
mailing list