<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">Thanks for the constructive response!</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">this is good to know, however it still forces me make such a dynamic placeholder explicit for every single operator.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">Still, I could put everything in a Mixin class so that it is in fact useful to clean up code.<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">However, in the long run, such placeholders seem like an unnecessary overhead to me.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">I myself thought whether one can insert the methods into the class itself using __new__ or something else concerning meta-classes. My background is however not good enough in order to seriously explore this direction.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000">Any further suggestions?</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:#000000"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 4 December 2015 at 14:50, Amaury Forgeot d'Arc <span dir="ltr"><<a href="mailto:amauryfa@gmail.com" target="_blank">amauryfa@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><span class=""><div dir="ltr">Le ven. 4 déc. 2015 à 14:21, Stephan Sahm <<a href="mailto:Stephan.Sahm@gmx.de" target="_blank">Stephan.Sahm@gmx.de</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Dear all,</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">I just stumbled upon a very weird behaviour of python 2 and python 3. At least I was not able to find a solution.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><b>The point is to dynamically define __add__, __or__ and so on via __getattr__</b> (for example by deriving them from __iadd__ or similar in a generic way).<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">However this very intuitive idea is currently NOT POSSIBLE because * - * / & | and so on just bypass this standard procedure.<br></div></div></blockquote><div><br></div></span><div>It is possible if you use indirection, and another name for the dynamic methods:</div><div><br></div><div>class C:</div><div>    def __add__(self, other):</div><div>        try:</div><div>            method = self.dynamic_add</div><div>        except AttributeError:</div><div>            return NotImplemented</div><div>        else:</div><div>            return method(other)</div><div><br></div><div>obj = C()</div><div>print(obj + 1)  # raises TypeError</div><div>obj.dynamic_add = lambda other: 42 + other</div><div>print(obj + 1)  # 43</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">I found two stackoverflow contributions stating this:</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><a href="http://stackoverflow.com/questions/11629287/python-how-to-forward-an-instances-method-call-to-its-attribute" target="_blank">http://stackoverflow.com/questions/11629287/python-how-to-forward-an-instances-method-call-to-its-attribute</a></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><a href="http://stackoverflow.com/questions/33393474/lazy-evaluation-forward-operations-to-deferred-value" target="_blank">http://stackoverflow.com/questions/33393474/lazy-evaluation-forward-operations-to-deferred-value</a></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Neither the mentioned posts, nor I myself can see any reason why this is the way it is, nor how the operators are actually implemented to maybe bypass this unintuitive behaviour.</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Any help? Comments? Ides?</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">best,</div><div class="gmail_default" style="font-family:tahoma,sans-serif;color:rgb(0,0,0)">Stephan</div></div></span>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a></blockquote></div></div>
</blockquote></div><br></div>