Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I change in Python the return/input type of a list that is implemented as an class attribute?
    primarykey
    data
    text
    <p><strong>EDIT (complete rephrase of the problem as the original version (see "original version", later) is misleading):</strong></p> <p>Here is the setting: I have a object which has a list of objects of type <code>&lt;class 'One'&gt;</code>. I would like to access this list but rather work with objects of type <code>&lt;class 'Two'&gt;</code> which is an enriched version of <code>&lt;class 'One'&gt;</code>.</p> <p>Background (1):</p> <ul> <li><code>One</code> could be an object that can be stored easily via a ORM. The ORM would handle the list depending on the data model</li> <li><code>Two</code> would be an object like <code>One</code> but enriched by many features or the way it can be accessed</li> </ul> <p>Background (2):</p> <ul> <li>I try to solve a SQLAlchemy related question that I asked <a href="https://stackoverflow.com/questions/8940802/how-to-integrate-sqlalchemy-and-a-subclassed-numpy-ndarray-smoothly-and-in-a-pyt">here</a>. So, the answer to the present question could be also a solution to that question changing return/input type of SQLAlchemy-lists.</li> </ul> <p>Here is some code for illustration:</p> <pre><code>import numpy as np class One(object): """ Data Transfere Object (DTO) """ def __init__(self, name, data): assert type(name) == str assert type(data) == str self.name = name self.data = data def __repr__(self): return "%s(%r, %r)" %(self.__class__.__name__, self.name, self.data) class Two(np.ndarray): _DTO = One def __new__(cls, name, data): dto = cls._DTO(name, data) return cls.newByDTO(dto) @classmethod def newByDTO(cls, dto): obj = np.fromstring(dto.data, dtype="float", sep=',').view(cls) obj.setflags(write=False) # Immutable obj._dto = dto return obj @property def name(self): return self._dto.name class DataUI(object): def __init__(self, list_of_ones): for one in list_of_ones: assert type(one) == One self.list_of_ones = list_of_ones if __name__ == '__main__': o1 = One('first object', "1, 3.0, 7, 8,1") o2 = One('second object', "3.7, 8, 10") my_data = DataUI ([o1, o2]) </code></pre> <p>How to implement a <code>list_of_twos</code> which operates on <code>list_of_ones</code> but provides the user a list with elements of type <code>Two</code>:</p> <pre><code>type (my_data.list_of_twos[1]) == Two &gt;&gt;&gt; True my_data.list_of_twos.append(Two("test", "1, 7, 4.5")) print my_data.list_of_ones[-1] &gt;&gt;&gt; One('test', '1, 7, 4.5') </code></pre> <hr> <p><strong>Original version of the question:</strong></p> <p>Here is an illustration of the problem:</p> <pre><code>class Data(object): def __init__(self, name, data_list): self.name = name self.data_list = data_list if __name__ == '__main__': my_data = Data ("first data set", [0, 1, 1.4, 5]) </code></pre> <p>I would like to access <code>my_data.data_list</code> via another list (e.g. <code>my_data.data_np_list</code>) that handles list-elements as a different type (e.g. as numpy.ndarray):</p> <pre><code>&gt;&gt;&gt; my_data.data_np_list[1] array(1) &gt;&gt;&gt; my_data.data_np_list.append(np.array(7)) &gt;&gt;&gt; print my_data.data_list [0, 1, 1.4, 5, 7] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    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