title: SWIG and C++ enum class
body: <p><strong>Edit</strong>: <a href="https://stackoverflow.com/a/58841057/12075306">this</a> answer solves the problem, but at the end I propose a more modular solution. Check it out :)</p>
  
  <p>I know from <a href="https://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum">two</a> <a href="http://www.cplusplus.com/forum/general/147341/" rel="nofollow noreferrer">discussions</a> that <code>enum class</code> should be chosen over <code>enum</code> for the sake of type safety. For this reason I'm using it in my projects, but when wrapping them with SWIG, I'm getting something that I don't quite like.</p>
  
  <p>Given the simple enumeration</p>
  
  <pre><code>enum class type {
      A, B, C, D, E, F, G, H, I
  };
  </code></pre>
  
  <p>in a <code>.hpp</code> file, say <code>my_types.hpp</code>, and the <code>my_types.i</code> file:</p>
  
  <pre><code>%module my_types
  %{
  #include "my_types.hpp"
  %}
  %include "my_types.hpp"
  </code></pre>
  
  <p>the generated python file contains contains</p>
  
  <pre><code>type_A = _my_types.type_A
  type_B = _my_types.type_B
  type_C = _my_types.type_C
  type_D = _my_types.type_D
  type_E = _my_types.type_E
  type_F = _my_types.type_F
  type_G = _my_types.type_G
  type_H = _my_types.type_H
  type_I = _my_types.type_I
  </code></pre>
  
  <p>which means that when using it in Python I will have to do</p>
  
  <pre><code>import my_types as mt
  mt.type_A
  </code></pre>
  
  <p>to use them, which.... well, in this case is kind of okay, since the word <code>type</code> is not that long, but for longer words it won't look as good, and, also, to me, it does not make much sense to have the name of the enumeration at the beginning of each enumerated value.</p>
  
  <p>Is there any way to rename them (perhaps using <code>%rename</code>) so that using them can be as simple as this?</p>
  
  <pre><code>import my_types as mt
  mt.A
  </code></pre>
  
  <p>I don't know how to use <code>%rename</code> in this case, or it can be even used for this purpose. Any help, including ideas on how to do the same differently (maybe without enumerations), will be appreciated. Thank you all.</p>
  
  <p><strong>EDIT</strong>: some comments point out that having the enumeration's name in front of the name of each enumerated does make sense. I would like to elaborate my reasons why I said it does not (to me). If we consider the case of a module called just the same as the enumeration then the code looks a bit redundant. Since <code>types</code> is already a Python's module, a better example would be colours:</p>
  
  <pre><code>enum class colours {
      Blue, Red
  };
  </code></pre>
  
  <p>and the wrapped code would be used, as suggested, like this:</p>
  
  <pre><code>import colours
  Blue = colours.colours_Blue
  Red = colours.colours_Red
  </code></pre>
  
  <p>It seems to me that writing <code>colours</code> to refer to <code>Blue</code>, <code>Red</code> is unnecessary since the module <code>colours</code> only contains one "enumeration". Wouldn't it be better to have this instead?</p>
  
  <pre><code>import colours
  Blue = colours.Blue
  Red = colours.Red
  </code></pre>
  
  <p>An answer has already explained on how to achieve this. I'll look at it as soon as I can. Thanks a lot!</p>
  
  <p><strong>Edit</strong>: <a href="https://stackoverflow.com/a/58841057/12075306">this</a> answer solves the problem. I would like to make a few changes to it and propose a slightly more "modular" solution:</p>
  
  <pre><code>%pythoncode %{
  __enum_name = "colours"            # your enumeration's name
  __enum = __import__(__enum_name)   # import module
  __scope_name = __enum_name + "_"   # scope's name (= "colours_")
  __scope_length = len(__scope_name) # length
  for name in dir(__enum):
      if name.find(__scope_name) == 0:
          setattr(__enum, name[__scope_length:], getattr(__enum, name))
          delattr(__enum, name) # optional
  del name, __enum_name, __enum, __scope_name, __scope_length
  %}
  </code></pre>
  
  <p>I think this allows for easier copy and paste for your projects where you have tenths of enumerations :). Again, based on <a href="https://stackoverflow.com/a/58841057/12075306">this</a> answer.</p>