Note that there are some explanatory texts on larger screens.

plurals
  1. POPython General Wrapper class for any data
    text
    copied!<p>I want to write a general wrapper class for any data. Primarily I am trying to make a function that will automatically store data in the hard drive (kind of an auto-swap) if it hasn't been used in a while.</p> <p>I have been programming with python for years now, but I just can't find a "pythonic" way of doing this. I eventually resorted to a script that would write every possible method for me -- but even that ended up not working. Check out the below.</p> <p>I can't even get this simple code to work -- why???</p> <pre><code>class adder(object): def __init__(self, value): self.value = value def __add__(self, other): print 'adding' return self.value.__add__(other) def __radd__(self, other): print 'radding' return self.value.__radd__(other) a = adder(10) b = adder(12) print a + b </code></pre> <p>OUTPUT</p> <pre><code>adding Traceback (most recent call last): File "/home/user/Projects/CloudformDesign/PythonCloudform/cloudtb/playground/adding_classes.py", line 42, in &lt;module&gt; print a + b TypeError: unsupported operand type(s) for +: 'adder' and 'adder' </code></pre> <p>I am of course aware that I could not use the <strong>add</strong> and <strong>radd</strong> functions (just use the + syntax). I did this to see what in the world was happening: here is the results</p> <pre><code>class adder2(object): def __init__(self, value): self.value = value def __add__(self, other): print 'a2' return self.value + other def __radd__(self, other): print 'ra2' return other + self.value a = adder(adder2(10)) b = adder(adder2(12)) print a + b </code></pre> <p>Output:</p> <pre><code>adding a2 radding ra2 22 </code></pre> <p>So clearly it is going into the "adding" function, then going into the second classe's adding function, then going into the first class's "radding" function, and then finally going into the second classes "radding" function -- what in the WORLD is happening??? <strong>Furthermore, THIS works when the other didn't</strong> -- I am so confused.</p> <p>Also, if you know of any code that has done this, please let me know!</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