Do we really need const?

Bill Baxter dnewsgroup at billbaxter.com
Sat Sep 22 23:48:08 PDT 2007


Jan Claeys wrote:
> Op Mon, 17 Sep 2007 15:36:35 +0900
> schreef Bill Baxter <dnewsgroup at billbaxter.com>:
> 
>> I've certainly gotten used to the lack of const in Python
> 
> Python has immutable objects (e.g. strings & tuples).   :)

Actually I remembered one time when lack of const did bite me in Python. 
  I was translating some Matlab code I wrote to Python/NumPy 
(http://www.scipy.org/).

In Matlab all calls are call-by-value, and all assignments are 
assign-by-value (both use COW under the covers to make performance 
reasonable).  In Python they're call-by-reference and 
assign-by-reference for most anything more complicated than an 'int'. 
The matrix class I was using in the Python code definitely fell into the 
by-reference category.

After I got the port basically working there were quite a few bugs that 
turned out to be a result of functions I translated from Matlab 
modifying their arguments.

If Python had const, I don't think I would have had those problems.

On the other hand, the NumPy matrix class itself has a read-only flag. 
If I were porting to Python again I'd probably try to write a decorator 
to use during debugging that sets that readonly flag before entering the 
function and unsets it on return.

--bb



More information about the Digitalmars-d mailing list