Note that there are some explanatory texts on larger screens.

plurals
  1. POPython subclass tuple object with ability to reinstantiate self internally
    primarykey
    data
    text
    <p>I understand the concept of mutable v. immutable objects in Python, no problem. While any immutable object's intrinsic value cannot be modified directly, any instance of an immutable object can be reinstantiated with different values. What I would like to do is build an internal function on a subclass of tuple that can in a controlled fashion, reassign it's own value. This could be basic functionality that I just can't seem to find and would appreciate any assistance.</p> <p>For example, here is what I'd like to be able to do, but this obviously doesn't work. </p> <pre><code>class myTuple(tuple): def __new__(self): initialValue = [1, 2, 3] return super(myTuple, self).__new__(self, initialValue) def resetMyself(self): newValue = [4, 5, 6] self = tuple(newValue) </code></pre> <p>With the following results...</p> <pre><code>&gt;&gt;&gt; foo = myTuple() &gt;&gt;&gt; print foo (1, 2, 3) &gt;&gt;&gt; foo.resetMyself() &gt;&gt;&gt; print foo (4, 5, 6) </code></pre> <p>From reading a larger number of responses to questions like this on this site, I know some of you may have the tendency to respond with "Why would you want to do this?" but let's save the response space with more direct answers, including possibly "You cannot do that no way, no how," if that's really the case.</p> <p>Thanks very much all!</p> <p>EDIT, THANKS FOR THE ANSWER BELOW, HERE IS WHAT I ENDED UP WITH...</p> <pre><code>class semiImmutableList(list): def __setitem__(self, *args): raise TypeError("'semiImmutableList' object doesn't support item assignment") __setslice__ = __setitem__ def __delitem__(self, *args): raise TypeError("'semiImmutableList' object doesn't support item deletion") __delslice__ = __delitem__ def append(self, *args): raise AttributeError("'semiImmutableList' object has no attribute 'append'") def extend(self, *args): raise AttributeError("'semiImmutableList' object has no attribute 'extend'") def insert(self, *args): raise AttributeError("'semiImmutableList' object has no attribute 'insert'") def remove(self, *args): raise AttributeError("'semiImmutableList' object has no attribute 'remove'") def pop(self, *args): raise AttributeError("'semiImmutableList' object has no attribute 'pop'") def __init__(self): x = [1, 2, 3] super(semiImmutableList, self).__init__(x) def resetMyself(self): super(semiImmutableList,self).append(5) </code></pre> <p>Any improvements/adjustments to the above that you can see please post. Seems like the duplication of AttributeError raises could be combined?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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