Do we really need const?

Jan Claeys usenet at janc.be
Mon Sep 24 14:53:33 PDT 2007


Op Sun, 23 Sep 2007 15:48:08 +0900
schreef Bill Baxter <dnewsgroup at billbaxter.com>:

> 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.

Never do a word-by-word translation...  :)

Both mutable & immutable objects are passed as "objects" in python (the
technical details of how that happens, e.g. using pointers, is merely
an implementation detail, but it's obviously very close to what happens
with call-by-reference in other languages) and it's only at the time of
assignment that behaviour between them differs.  It's the same outside
of a function/method and has nothing to do with the different methods of
parameter passing in other languages.


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

Well, Python has something 'const'-like, only it's linked to the
object's type, and not defined by the way it's called.  ;)

And if you want to "call by value", there is always the 'copy' module:
<http://docs.python.org/lib/module-copy.html>
(Could be used in a decorator too...)


-- 
JanC



More information about the Digitalmars-d mailing list