why_doesn_t_my_operator_work.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Why doesn't my *= operator work?</title>
  5. <link rel="stylesheet" href="../boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Boost.Python">
  8. <link rel="up" href="../faq.html" title="Chapter&#160;5.&#160;Frequently Asked Questions (FAQs)">
  9. <link rel="prev" href="how_do_i_debug_my_python_extensi.html" title="How do I debug my Python extensions?">
  10. <link rel="next" href="does_boost_python_work_with_mac_.html" title="Does Boost.Python work with Mac OS X?">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table>
  14. <hr>
  15. <div class="spirit-nav">
  16. <a accesskey="p" href="how_do_i_debug_my_python_extensi.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="does_boost_python_work_with_mac_.html"><img src="../images/next.png" alt="Next"></a>
  17. </div>
  18. <div class="section">
  19. <div class="titlepage"><div><div><h3 class="title">
  20. <a name="faq.why_doesn_t_my_operator_work"></a><a class="link" href="why_doesn_t_my_operator_work.html" title="Why doesn't my *= operator work?">Why doesn't my <code class="computeroutput"><span class="special">*=</span></code> operator work?</a>
  21. </h3></div></div></div>
  22. <p>
  23. <span class="bold"><strong>Q:</strong></span> <span class="emphasis"><em>I have exported my class to
  24. python, with many overloaded operators. it works fine for me except the
  25. <code class="computeroutput"><span class="special">*=</span></code> operator. It always tells
  26. me "can't multiply sequence with non int type". If I use <code class="computeroutput"><span class="identifier">p1</span><span class="special">.</span><span class="identifier">__imul__</span><span class="special">(</span><span class="identifier">p2</span><span class="special">)</span></code>
  27. instead of <code class="computeroutput"><span class="identifier">p1</span> <span class="special">*=</span>
  28. <span class="identifier">p2</span></code>, it successfully executes my
  29. code. What's wrong with me?</em></span>
  30. </p>
  31. <p>
  32. <span class="bold"><strong>A:</strong></span> There's nothing wrong with you. This
  33. is a bug in Python 2.2. You can see the same effect in Pure Python (you can
  34. learn a lot about what's happening in Boost.Python by playing with new-style
  35. classes in Pure Python).
  36. </p>
  37. <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">class</span> <span class="identifier">X</span><span class="special">(</span><span class="identifier">object</span><span class="special">):</span>
  38. <span class="special">...</span> <span class="identifier">def</span> <span class="identifier">__imul__</span><span class="special">(</span><span class="identifier">self</span><span class="special">,</span> <span class="identifier">x</span><span class="special">):</span>
  39. <span class="special">...</span> <span class="identifier">print</span> <span class="char">'imul'</span>
  40. <span class="special">...</span>
  41. <span class="special">&gt;&gt;&gt;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">X</span><span class="special">()</span>
  42. <span class="special">&gt;&gt;&gt;</span> <span class="identifier">x</span> <span class="special">*=</span> <span class="number">1</span>
  43. </pre>
  44. <p>
  45. To cure this problem, all you need to do is upgrade your Python to version
  46. 2.2.1 or later.
  47. </p>
  48. </div>
  49. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  50. <td align="left"></td>
  51. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2015 David
  52. Abrahams, Stefan Seefeld<p>
  53. Distributed under the Boost Software License, Version 1.0. (See accompanying
  54. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  55. </p>
  56. </div></td>
  57. </tr></table>
  58. <hr>
  59. <div class="spirit-nav">
  60. <a accesskey="p" href="how_do_i_debug_my_python_extensi.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="does_boost_python_work_with_mac_.html"><img src="../images/next.png" alt="Next"></a>
  61. </div>
  62. </body>
  63. </html>