Currently tackling pset6 similarities more, and I'm trying to figure out what the purpose of enumeration is for in the Operation class.

Here is the code taken from the helpers.py file:

class Operation(Enum):

    DELETED = 1
    INSERTED = 2

    def __str__(self):
        return str(self.name.lower())

I understand that we are instructed to use one of Operation.DELETED, Operation.INSERTED or Operation.SUBSTITUTED for the operation value of the tuple. But what is the purpose creating an Operation class for it and enumerating it?

  • I am trying to figure this out too. Did you make any progress? – whackdev Apr 22 '18 at 0:32

Since Python is a dynamically-typed language, I don't think enums have more purpose than readability. Instead of passing "magic" numbers like 1, 2 and 3 here and there, you write Operation.INSERTED, and that's it.


It makes it also much easier to recognise the values:

>>> test = Operation.INSERTED
>>> test
<Operation.INSERTED: 2>
>>> test.name

Somewhere in the documentation, it says:

The other motivating factor, however, was the ability to use enumerations to improve various error messages emitted by the standard library. One of the significant downsides of magic integers is the fact that they often result in cryptic error messages, unless the library authors take special care to translate numeric values back to their string equivalents when creating the error message. This limitation also applies to logging messages and value introspection when debugging. The additional name information in the representation of enumeration values provides that easier interpretation of otherwise generic values for free.

And to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .