What is the python equivalent of c++ const-reference copy-constructor arguments
<p>One of the great things of C++ is the usage of <code>const</code>-reference arguments â using these type of argument, you're pretty much guaranteed objects wonât be accidentally modified, and there wonât be side effects.</p>

<p>Question is, whatâd be the Python equivalent to such arguments?</p>

<p>For instance, letâs say you have this C++ method:</p>

<pre><code>void Foo::setPosition(const QPoint &amp;position) {
    m_position = position;
}
</code></pre>

<p>And you want to âtranslateâ it to Python like this:</p>

<pre><code>def set_position(self, position):
    self.position = position
</code></pre>

<p>Doing this will potentially yield a lot of trouble, and many subtle bugs could appear as well. Question is, whatâs the âPython equivalentâ way of C++ methods which use const references arguments (copy constructor)?</p>

<p>Last time I caught a bug because a I had a bad âC++ -> Python translationâ; I fixed this with something like:</p>

<pre><code>my_python_instance.set_position(QPoint(pos))
</code></pre>

<p>â¦ said otherwise, my other choice was to clone the object from the callerâ¦ which Iâm pretty much sure is not the right way to go.</p>

<p>Write a decorator on the function.  Have it serialize the data on the way in, serialize it on the way out, and confirm the two are equal.  If they are not, generate an error.</p>

<p>Python type checking is mostly runtime checks that arguments satisfy some predicate.  <code>const</code> in C++ is a type check on the behaviour of the function: so you have to do a runtime check on the behaviour of the function to be equivalent.</p>

<p>You could also only do such checks when doing unit testing or in a debug build, "prove" it correct, then remove checks on "release" mode.</p>

<p>Alternatively, you could write a static analyzer that checks for const violations using the inspect module, and decorating the immutability of arguments you lack source for, I suppose.  Probably be just as easy to write your own language variant that supports <code>const</code> tho.  As in nigh impossible.</p>

python c++ language-lawyer copy-constructor const-correctness