Need a Constant in Python? Enums Can Come in Useful
Briefly

Need a Constant in Python? Enums Can Come in Useful
"Python doesn't have constants. You probably learnt this early on when learning Python. Unlike many other programming languages, you can't define a constant in Python. All variables are variable! "Ah, but there are immutable types." Sure, you can have an object that doesn't change throughout its lifetime. But you can't have a reference to it that's guaranteed not to change. The identifier (variable name) you use to refer to this immutable type can easily switch to refer to something else."
""How about using all caps for the identifier. Doesn't that make it a constant?" No, it doesn't. That's just a convention you use to show your intent as a programmer that an identifier refers to a value that shouldn't change. But nothing prevents that value from changing. Here's an all-uppercase identifier that refers to an immutable object: The identifier is all caps. The object is a tuple, which is immutable."
Python does not provide built-in constants and any variable name can be reassigned. Immutable types create objects that do not change, but identifiers referencing those objects can be rebound to different values. Using all-uppercase identifiers is merely a convention to indicate intent and does not enforce immutability or prevent reassignment. Tuples illustrate immutable objects but do not stop reassignment of the name that points to them. Developers can adopt other tools and patterns to mimic constant behavior; one such tool is the Enum type for representing named constant values.
Read at Thepythoncodingstack
Unable to calculate read time
[
|
]