Change the underlying data representation with the descriptor protocol
<p>Suppose I have an existing class, for example doing some mathematical stuff:</p>

<pre><code>class Vector:

    def __init__(self, x, y):
        self.x = y
        self.y = y

    def norm(self):
        return math.sqrt(math.pow(self.x, 2) + math.pow(self.y, 2))
</code></pre>

<p>Now, for some reason, I'd like to have that Python does not store the members <code>x</code> and <code>y</code> like any variable. I'd rather want that Python internally stores them as strings. Or that it stores them into a dedicated buffer, maybe for interoperability with some C code. So (for the string case) I build the following <em>descriptor</em>:</p>

<pre><code>class MyStringMemory(object):

    def __init__(self, convert):
        self.convert = convert

    def __get__(self, obj, objtype):
        print('Read')
        return self.convert(self.prop)

    def __set__(self, obj, val):
        print('Write')
        self.prop = str(val)

    def __delete__(self, obj):
        print('Delete')
</code></pre>

<p>And I wrap the existing vector class in a new class where members <code>x</code> and <code>y</code> become <code>MyStringMemory</code>:</p>

<pre><code>class StringVector(Vector):

    def __init__(self, x, y):
        self.x = x
        self.y = y

    x = MyStringMemory(float)
    y = MyStringMemory(float)
</code></pre>

<p>Finally, some driving code:</p>

<pre><code>v = StringVector(1, 2)
print(v.norm())
v.x, v.y = 10, 20
print(v.norm())
</code></pre>

<p>After all, I replaced the internal representation of <code>x</code> and <code>y</code> to be strings without any change in the original class, but still with its full functionality.</p>

<p>I just wonder: <strong>Will that concept work universally or do I run into serious pitfalls?</strong> As I said, the main idea is to store the data into a specific buffer location that is later on accessed by a C code.</p>

<p><strong>Edit:</strong> The intention of what I'm doing is as follows. Currently, I have a nicely working program where some physical objects, all of type <code>MyPhysicalObj</code> interact with each other. The code <em>inside</em> the objects is vectorized with Numpy. Now I'd also like to vectorize some code <em>over all</em> objects. For example, each object has an <code>energy</code> that is computed by a complicated vectorized code per-object. Now I'd like to sum up all energies. I can iterate over all objects and sum up, but that's slow. So I'd rather have that property <code>energy</code> for each object automatically stored into a globally predefined buffer, and I can just use <code>numpy.sum</code> over that buffer.</p>

<p>If you need a generic convertor('convert') like you did, this is the way to go.</p>

<p>The biggest downside will be <strong>performance</strong> when you will need to create a lot of instances( I assumed you might, since the class called <code>Vector</code>). This will be slow since python class initiation is slow.</p>

<p>In this case you might consider using <a href="https://docs.python.org/3/library/collections.html#collections.namedtuple" rel="nofollow">namedTuple</a> you can see the docs have a similar scenario as you have.</p>

<p>As a side note: If that possible, why not creating a dict with the string representation of x and y on the init method? and then keep using the x and y as normal variables without all the converting</p>

python