Follow

Is this line of code written in python explicit enough for you to understand?

(CONSTANT, OTHER_CONSTANT) = range(2)

@OTheB Enums are good, but if I don't intend to use any features of enums, I tend to avoid them in python: if you want to use value, you have to add ".value" postfix a lot, which is not that beautiful :)

@alexcleac
If it's more than 2, like:

(
MODE_FOO,
MODE_BAR,
MODE_FOOBAR,
MODE_BAZ
) = range(4)

I think it's not too hard to understand, especially if you use this pattern throughout the code - it's easy to get used to.

But the maintainability benefits are not that big - you need to adjust the range size every time you add a new item anyway.

Also, if it didn't start at zero, then it'd be too confusing IMO.

@Wolf480pl I like this one over simple declarations of constants because there is just one assigning happening, and all the other is just declaration of some global constants.

@alexcleac range() returns a tuple? I may have learned something today.

@trevdev It returns not a tuple, but a generator, but you can destruct it into tuple :D

@alexcleac
Why range(2)?
CONSTANT will be 0 and OTHER_CONSTANT will be 1
Why the parentheses?
(C, OC) will evaluate the same result as C, OC:
(0, 1)

@manuelcaeiro Yeah, parentheses are optional in single line, but for more than one, they make it a bit more readable

Sign in to participate in the conversation
Mastodon for Tech Folks

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!