Question about math.pi is mutable

Thomas 'PointedEars' Lahn PointedEars at web.de
Fri Nov 6 17:26:49 EST 2015


Bartc wrote:
^^^^^
Please fix.

> On 06/11/2015 02:33, wangq at travelsky.com wrote:
>>      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.

“pi” is the name of an attribute of the module object referred to by “math”.
 
> (Perhaps math.pi would be better off as a function.)

Perhaps not.  Calling a function includes an overhead that one does not want 
in already costly floating-point calculations.

In my opinion, mathematical constants should be implemented as constants 
(non-overwritable, non-deletable, throwing exceptions when attempting to do 
either) but apparently the authors of math.py disagree.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list