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>I hope I understood correctly.</p>

<p>Shortly, there is not one. There are two things you are after which you do not come by in python often. <code>const</code>s and copy constructors.</p>

<p>It's a design decision, python is a different language. Arguments are always passed by reference.</p>

<h3><code>const</code> correctness</h3>

<p>It's up to user not to mess up object, and I think it doesn't happen very often. Personally I like const correctness in c++ but I never caught myself missing it in python. It's a dynamic and scripting language so there is no point in looking at the micro optimizations which could be done under argument constness assertion.</p>

<h3>Copying object</h3>

<p>... you don't do it too much in python. In my opinion it's a design decision to offload memory management onto user, because it's hard to come by with a good standard way, e.g. shallow copies vs. deep copies. I guess if you assume, you don't need it that much, there is no point in providing a way for each object (like c++ does), but only for those which do need it.</p>

<p>Therefore, there is no unified pythonic way. There are at least few ways to do it in standard: </p>

<ol>
<li>List <code>copied = original[:]</code>.</li>
<li>Some objects provide <code>copy</code> method, like <code>dict</code>.</li>
<li>Some objects explicitly provide a constructor (like in c++), <code>dict</code> and <code>list</code> do so, so you can write <code>copied = list(original)</code>.</li>
<li>There is a module <a href="https://docs.python.org/2/library/copy.html#module-copy" rel="nofollow"><code>copy</code></a>, for which you can provide custom methods <code>__copy__</code> and <code>__deepcopy__</code>. It also has advantage that another standard library work with it - <code>pickle</code> for serialization.</li>
</ol>

<p>The most c++-like way is option 3. - to implement a constructor so it accepts invocation which returns a copy of its argument (when the argument is of the same type). But it might need a bit crafty implementation of ctor, because you cannot do type overloading.</p>

<p>Option 2. is the same, but refactored into a function.</p>

<p>So probably best way is to provide explicit <code>__copy__</code>/<code>copy</code> methods, and if you're nice support ctor invocation which will call it.</p>

<p>You then as a developer of object can ensure const correctness, and provide a user with an easy explicit way to request a copy.</p>

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