Showing content from http://mail.python.org/pipermail/python-dev/attachments/20150514/7a2f76bf/attachment.html below:
I expect you can make something that behaves like list by defining __mul__ and __rmul__ and returning NotImplemented.<br><br>On Thursday, May 14, 2015, Stefan Richthofer <<a href="mailto:Stefan.Richthofer@gmx.de">Stefan.Richthofer@gmx.de</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">>>Should I be worried?<br>
<br>
You mean should *I* be worried ;)<br>
<br>
Stuff like this is highly relevant for JyNI, so thanks very much for clarifying this<br>
subtle behavior. It went onto my todo-list right now to ensure that JyNI will emulate<br>
this behavior as soon as I am done with gc-support. (Hopefully it will be feasible,<br>
but I can only tell in half a year or so since there are currently other priorities.)<br>
Still, this "essay" potentially will save me a lot of time.<br>
<br>
So, everybody please feel encouraged to post things like this as they come up. Maybe<br>
there could be kind of a pitfalls-page somewhere in the docs collecting these things.<br>
<br>
Best<br>
<br>
Stefan<br>
<br>
<br>
> Gesendet: Freitag, 15. Mai 2015 um 02:45 Uhr<br>
> Von: "Nathaniel Smith" <<a href="javascript:;" onclick="_e(event, 'cvml', 'njs@pobox.com')">njs@pobox.com</a>><br>
> An: "Python Dev" <<a href="javascript:;" onclick="_e(event, 'cvml', 'python-dev@python.org')">python-dev@python.org</a>><br>
> Betreff: [Python-Dev] Python-versus-CPython question for __mul__ dispatch<br>
><br>
> Hi all,<br>
><br>
> While attempting to clean up some of the more squamous aspects of<br>
> numpy's operator dispatch code [1][2], I've encountered a situation<br>
> where the semantics we want and are using are possible according to<br>
> CPython-the-interpreter, but AFAICT ought not to be possible according<br>
> to Python-the-language, i.e., it's not clear to me whether it's<br>
> possible even in principle to implement an object that works the way<br>
> numpy.ndarray does in any other interpreter. Which makes me a bit<br>
> nervous, so I wanted to check if there was any ruling on this.<br>
><br>
> Specifically, the quirk we are relying on is this: in CPython, if you do<br>
><br>
>Â Â [1, 2] * my_object<br>
><br>
> then my_object's __rmul__ gets called *before* list.__mul__,<br>
> *regardless* of the inheritance relationship between list and<br>
> type(my_object). This occurs as a side-effect of the weirdness<br>
> involved in having both tp_as_number->nb_multiply and<br>
> tp_as_sequence->sq_repeat in the C API -- when evaluating "a * b",<br>
> CPython tries a's nb_multiply, then b's nb_multiply, then a's<br>
> sq_repeat, then b's sq_repeat. Since list has an sq_repeat but not an<br>
> nb_multiply, this means that my_object's nb_multiply gets called<br>
> before any list method.<br>
><br>
> Here's an example demonstrating how weird this is. list.__mul__ wants<br>
> an integer, and by "integer" it means "any object with an __index__<br>
> method". So here's a class that list is happy to be multiplied by --<br>
> according to the ordinary rules for operator dispatch, in the example<br>
> below Indexable.__mul__ and __rmul__ shouldn't even get a look-in:<br>
><br>
> In [3]: class Indexable(object):<br>
>Â Â ...:Â Â Â def __index__(self):<br>
>Â Â ...:Â Â Â Â Â return 2<br>
>Â Â ...:<br>
><br>
> In [4]: [1, 2] * Indexable()<br>
> Out[4]: [1, 2, 1, 2]<br>
><br>
> But, if I add an __rmul__ method, then this actually wins:<br>
><br>
> In [6]: class IndexableWithMul(object):<br>
>Â Â ...:Â Â Â def __index__(self):<br>
>Â Â ...:Â Â Â Â Â return 2<br>
>Â Â ...:Â Â Â def __mul__(self, other):<br>
>Â Â ...:Â Â Â Â Â return "indexable forward mul"<br>
>Â Â ...:Â Â Â def __rmul__(self, other):<br>
>Â Â ...:Â Â Â Â Â return "indexable reverse mul"<br>
><br>
> In [7]: [1, 2] * IndexableWithMul()<br>
> Out[7]: 'indexable reverse mul'<br>
><br>
> In [8]: IndexableWithMul() * [1, 2]<br>
> Out[8]: 'indexable forward mul'<br>
><br>
> NumPy arrays, of course, correctly define both __index__ method (which<br>
> raises an array on general arrays but coerces to int for arrays that<br>
> contain exactly 1 integer), and also defines an nb_multiply slot which<br>
> accepts lists and performs elementwise multiplication:<br>
><br>
> In [9]: [1, 2] * np.array(2)<br>
> Out[9]: array([2, 4])<br>
><br>
> And that's all great! Just what we want. But the only reason this is<br>
> possible, AFAICT, is that CPython 'list' is a weird type with<br>
> undocumented behaviour that you can't actually define using pure<br>
> Python code.<br>
><br>
> Should I be worried?<br>
><br>
> -n<br>
><br>
> [1] <a href="https://github.com/numpy/numpy/pull/5864" target="_blank">https://github.com/numpy/numpy/pull/5864</a><br>
> [2] <a href="https://github.com/numpy/numpy/issues/5844" target="_blank">https://github.com/numpy/numpy/issues/5844</a><br>
><br>
> --<br>
> Nathaniel J. Smith -- <a href="http://vorpus.org" target="_blank">http://vorpus.org</a><br>
> _______________________________________________<br>
> Python-Dev mailing list<br>
> <a href="javascript:;" onclick="_e(event, 'cvml', 'Python-Dev@python.org')">Python-Dev@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
> Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/stefan.richthofer%40gmx.de" target="_blank">https://mail.python.org/mailman/options/python-dev/stefan.richthofer%40gmx.de</a><br>
><br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="javascript:;" onclick="_e(event, 'cvml', 'Python-Dev@python.org')">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" target="_blank">https://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
</blockquote><br><br>-- <br>--Guido van Rossum (on iPad)<br>
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4