Typesafe enum pattern in Python
For a language that is supposed to be pure elegance, some basic Java constructs sure do look ugly in Python. Here’s an attempt at the typesafe enum pattern in Python. Not exactly beautiful:
class Suit:
def __init__(self,name):
self.name = name
def __str__(self):
return self.name
Suit.CLUBS = Suit('Clubs')
Suit.HEARTS = Suit('Hearts')
Suit.SPADES = Suit('Spades')
Suit.DIAMONDS = Suit('Diamonds')
Suit.__init__ = None
print Suit.CLUBS
print Suit.HEARTS
print Suit.SPADES
print Suit.DIAMONDS
This produces:
Clubs
Hearts
Spades
Diamonds
Someone please direct me to an example of the preferred way to implement enums in Python, cuz this obviously isn’t it.
I’m guessing you would want to employ tuples to emulate this somehow, given that they are immutable, but I can’t think pythonically enough to imagine what that would look like.
Python is so frustrating… on the one hand I love its minimalistic syntax. I find its lack of curlies and block indentation highly preferable of the C-style trash that Java has been burdened with. But IMHO, Python has two major shortcomings:
Python is missing important language features.
- Interface inheritance (as opposed to implementation-inheritance)
- Abstract classes
- Access modifiers (true private, protected, package-private access… none of this double underscore bullsh*t. That’s fugly.)
- Static/Class methods without an obvious kludge (again, this staticmethod(meth) syntax is fugly)
- Language-level support for thread exclusion (think
synchronizedkeyword in java) - Static typing — I realize this one is highly debatable, but if Python at least offered the option, it might be taken more seriously for large systems programming. Adding this would, however highlight another feature Python lacks — Parameterized Types. Did anything ever come of this?
- Enums or a reasonable equivalent? Java has just gotten true enums, but at least there was the typesafe enum pattern before that. The above example doesn’t strike me as very usable (or pythonic).
- You can’t give a
tryblock both anexceptand afinallyblock. You have to nest them. What’s up with that?
Python can be too surprising.
Python allows the programmer to write code that IMO is too clever. The following features contribute to this:
- Properties. Member variable access should look like member variable access. Method invocation should look like method invocation. These should look different. Period. Why force other devs to look at your source code just to discover something that should be obvious? Is the added confusion really worth those 3 or 4 extra keystrokes? Sheesh.
- __getattr__, __setattr__ This is supposed to improve clarity?
- Multiple Implementation inheritance Resolving this is just too confusing.
I’ll leave operator overloading off of the above list, as I realize my distaste for it is probably too influenced by my Java experience, and it’s a debatably elegant language feature.
About this entry
You’re currently reading “Typesafe enum pattern in Python,” an entry on Todd Ditchendorf’s Blog.
- Published:
- 05.08.05 / 8pm
- Category:
- Python
No comments
Jump to comment form | comments rss [?] | trackback uri [?]