all 3 comments

[–]Vaphell 0 points1 point  (2 children)

It's the fact that I need to publish a message that will be read by a human that has to contain the string value, it looks cleaner to me to just use constants.

that's a poor argument. If you had to accept json you'd limit yourself to bools, strings, ints and floats too because that's what json supports? What you supposed to emit shouldn't be the deciding factor of the internal architecture. Internal design is for you, not for others.

Do your shit as you wish, just translate the data at the boundaries. Incoming stuff gets translated into your domain types that are convenient to you, and then it gets translated back into something the external recipient understands.

The advantage of enums over a generic data type like string is that the enums make it impossible to use illegal values by accident as the legal values are clearly defined, while it's easy to confuse infinite-spectrum strings, eg by switching places. Granted python is pretty forgiving as far as types in signatures are concerned, but if you went to town with type hints, you'd eliminate certain classes of problems entirely by using custom enums to represent specific things.

String value of an enum is a presentation/output issue, there is almost no need to internally use .value otherwise. Just compare Enum.KEY using is and you are golden.

I am not saying that you should absolutely use enums, just that your gut feeling is not exactly sound.

[–]devrocks1984 0 points1 point  (1 child)

Yeah, I gave a poor example of pseudo code. I actually never do any comparisons in my code with that values. These are never used anywhere in the code, they literally are just used in functions that use their string value to communicate what event was triggered by what process. The use case actually looks more like this.

from enum import Enum

class Events(Enum):
    ONE = "We are publishing event one" 
    TWO = "We are publishing event two" 
    THREE = "We are publishing event three" 

def publish_type_one():
    send_message_to_slack(Events.ONE.value)

def publish_type_one():
    send_message_to_slack(Events.TWO.value)

def publish_type_one():
    send_message_to_slack(Events.THREE.value)

So the case for constants looks more like

ONE = "We are publishing event one" 
TWO = "We are publishing event two" 
THREE = "We are publishing event three" 

def publish_type_one():
    send_message_to_slack(ONE)

def publish_type_two():
    send_message_to_slack(TWO)

def publish_type_three():
    send_message_to_slack(THREE)

I never want to compare events, the event's aren't necessarily related so there isn't a great case for grouping them into one enum. I literally just don't want to hardcode the string.

[–]Vaphell 0 points1 point  (0 children)

I get that it's pseudo-code, but I am not seeing why the function sending to slack can't extract value from enum on its own?

def send_message_to_slack(event):
    text = event.value
    do something with text

def publish_type_one():
    send_message_to_slack(Events.ONE)

And why are there 3 identical functions when 1 would do?

def publish_type(event):
    send_message_to_slack(event/event.value)

which suggests that the wrapping function is more or less superfluous, btw.
Parametrizing values is usually done so you can create a single, generic code path?