Boost C++ Libraries: Ticket #1181: [boost.python] can modify enum value https://svn.boost.org/trac10/ticket/1181 <p> I use boost 1.34.0, python 2.5.1 User can modify the enum value, But enum should be a const value(read only). </p> <div class="wiki-code"><div class="code"><pre><span class="k">enum</span> <span class="n">my_enum</span><span class="p">{</span><span class="n">my_value</span><span class="p">};</span> <span class="k">struct</span> <span class="n">my_s</span><span class="p">{</span> <span class="k">enum</span><span class="p">{</span> <span class="n">my_value2</span> <span class="p">};};</span> <span class="kt">void</span> <span class="n">my_export</span> <span class="p">{</span> <span class="n">enum_</span><span class="o">&lt;</span> <span class="n">my_enum</span> <span class="o">&gt;</span><span class="p">(</span><span class="s">&quot;my_enum&quot;</span><span class="p">)</span> <span class="p">.</span><span class="n">value</span><span class="p">(</span><span class="s">&quot;my_value&quot;</span><span class="p">,</span> <span class="n">my_value</span><span class="p">);</span> <span class="n">scope</span><span class="o">*</span> <span class="n">my_s_scope</span> <span class="o">=</span> <span class="k">new</span> <span class="n">scope</span><span class="p">(</span><span class="n">class_</span><span class="o">&lt;</span> <span class="n">my_s</span><span class="o">&gt;</span><span class="p">(</span><span class="s">&quot;my_s&quot;</span><span class="p">,</span> <span class="n">init</span><span class="o">&lt;</span> <span class="o">&gt;</span><span class="p">()));</span> <span class="n">scope</span><span class="p">().</span><span class="n">attr</span><span class="p">(</span><span class="s">&quot;my_value2&quot;</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">my_value2</span><span class="p">;</span> <span class="k">delete</span> <span class="n">my_s_scope</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><div class="wiki-code"><div class="code"><pre><span class="c1">#in python</span> <span class="n">module</span><span class="o">.</span><span class="n">my_enum</span><span class="o">.</span><span class="n">my_value</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">print</span> <span class="n">module</span><span class="o">.</span><span class="n">my_enum</span><span class="o">.</span><span class="n">my_value</span> <span class="n">module</span><span class="o">.</span><span class="n">my_s</span><span class="o">.</span><span class="n">my_value2</span> <span class="o">=</span> <span class="mi">2</span> <span class="k">print</span> <span class="n">module</span><span class="o">.</span><span class="n">my_s</span><span class="o">.</span><span class="n">my_value2</span> <span class="c1"># we can modify the const value.</span> <span class="c1"># output </span> <span class="c1"># 1</span> <span class="c1"># 2</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1181 Trac 1.4.3 Dave Abrahams Thu, 26 Jun 2008 21:15:31 GMT description changed https://svn.boost.org/trac10/ticket/1181#comment:1 https://svn.boost.org/trac10/ticket/1181#comment:1 <ul> <li><strong>description</strong> modified (<a href="/trac10/ticket/1181?action=diff&amp;version=1">diff</a>) </li> </ul> <p> Please try to use the <a class="wiki" href="https://svn.boost.org/trac10/wiki/WikiFormatting">WikiFormatting</a> rules to create readable tickets. </p> Ticket Dave Abrahams Thu, 26 Jun 2008 21:25:54 GMT status changed https://svn.boost.org/trac10/ticket/1181#comment:2 https://svn.boost.org/trac10/ticket/1181#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> This seems reasonable enough, since we can use properties to make read-only attributes, but it will take some doing. As described in <a class="ext-link" href="http://www.python.org/download/releases/2.2/descrintro/#property"><span class="icon">​</span>http://www.python.org/download/releases/2.2/descrintro/#property</a>, "The get method won't be called when the property is accessed as a class attribute <code>(C.x)</code> instead of as an instance attribute <code>(C().x)</code>." This will remain a low-priority until I (or someone else -- <strong>hint, hint</strong>) figures out how to appropriately "override the <code>__get__</code> operation for properties used as a class attribute" (in pure python code) as described on that page. </p> Ticket troy d. straszheim Sun, 11 Oct 2009 00:27:51 GMT owner, status changed https://svn.boost.org/trac10/ticket/1181#comment:3 https://svn.boost.org/trac10/ticket/1181#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Dave Abrahams</span> to <span class="trac-author">troy d. straszheim</span> </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> </ul> <p> I have a fix for this locally, but it needs some cleanup/refactoring yet. It borrows some of the internals of class_ (specifically the metatype) to implement readonly class properties. </p> Ticket