Pythonic way around Enums
<p>What is the <em>pythonic</em> way to tell the caller of a function what values a given parameter supports?</p>

<p>He is an example for PyQt (for GUI). Say I have a checkbox, </p>

<pre><code>class checkbox(object):
    ....
    def setCheckState(self, value):
        ....
</code></pre>

<p>Here, <code>setCheckState()</code> should only expect <strong>checked</strong> or <strong>unchecked</strong>.</p>

<p>PyQt uses a built-in enumeration (i.e. <code>Qt.Checked</code> or <code>Qt.Unchecked</code>), but this is awful. I am constantly in the documentation looking for the enum for the object I am working with.</p>

<p>Obviously PyQt is written in an <em>unpythonic</em> C++ sytle. How <em>should</em> this or a similar problem be handled in Python? According to <a href="https://www.python.org/dev/peps/pep-0435/" rel="nofollow">PEP 435</a>, enums seem to be a recent addition to the language and for very specific applications, so I would assume there is/was a better way to handle this?</p>

<p>I want to make the code I write easy to use when my functions require specific parameter values--almost like a combobox for functions.</p>

<p>That will do the trick</p>

<pre><code>import collections

def create_enum(container, start_num, *enum_words):
    return collections.namedtuple(container, enum_words)(*range(start_num, start_num + len(enum_words)))

Switch = create_enum('enums', 1, 'On', 'Off')
</code></pre>

<p><em>Switch</em> is your enum:</p>

<pre><code>In [20]: Switch.On
Out[20]: 1

In [21]: Switch.Off
Out[21]: 2
</code></pre>

<p>OK, I got the error of my ways - I mixed up representation with value.</p>

<p>Nevertheless, if you want to enumerate a larger range - in my <strong>fake</strong> approach you don't have to add values manually. Of course, if you have sequential numbers.</p>

<p>And I hate extra typing :-)</p>

python python-2.7 function parameters enums