Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sounds like you want to know if your class should be an <a href="http://en.wikipedia.org/wiki/Immutable_object" rel="nofollow">immutable object</a>. The idea is that, once created, an immutable object can't/should't/would't be changed.</p> <p>On <a href="http://en.wikipedia.org/wiki/Immutable_object#Python" rel="nofollow">Python</a>, built-in types like <code>int</code> or <code>tuple</code> instances are immutable, enforced by the language:</p> <pre><code>&gt;&gt;&gt; a=(1, 2, 3, 1, 2, 3) &gt;&gt;&gt; a[0] = 9 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: 'tuple' object does not support item assignment </code></pre> <p>As another example, every time you add two integers a new instance is created:</p> <pre><code>&gt;&gt;&gt; a=5000 &gt;&gt;&gt; b=7000 &gt;&gt;&gt; d=a+b &gt;&gt;&gt; d 12000 &gt;&gt;&gt; id(d) 42882584 &gt;&gt;&gt; d=a+b &gt;&gt;&gt; id(d) 42215680 </code></pre> <p>The <a href="http://docs.python.org/library/functions.html#id" rel="nofollow"><code>id()</code> function</a> returns the address of the <code>int</code> object <code>12000</code>. And every time we add <code>a+b</code> a new <code>12000</code> object instance is created.</p> <p>User defined immutable classes must be enforced manually, or simply done as a convention with a source code comment:</p> <pre><code>class X(object): """Immutable class. Don't change instance variables values!""" def __init__(self, *args): self._some_internal_value = ... def some_operation(self, arg0): new_instance = X(arg0 + ...) new_instance._some_internal_operation(self._some_internal_value, 42) return new_instance def _some_internal_operation(self, a, b): """...""" </code></pre> <p>Either way, it's OK to create a new instance for every operation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload