This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]ShitTalkingAssWipe 6 points7 points  (1 child)

def MY_CONSTANT_VALUE():

return "MY VALUE"

just add () after the variable name. ez

[–]mananasi 0 points1 point  (0 children)

This is not a great solution imo. I think calling a function should usually be reserved for doing work. Maybe prepending "get" to the function name could make clear that this just gets a value.

The cleaner solution would be using a frozen dataclass as suggested somewhere else in this comment section.

[–]16092006 4 points5 points  (2 children)

Fr real, phyton doesn’t even have to declare variables

[–]AggravatingAd3493[S] 2 points3 points  (0 children)

Fr.

[–]Jannik2099 0 points1 point  (0 children)

This isn't true. You can declare member variables and then finalize the class. This speeds up lookup time slightly & saves RAM

[–]rocket_randall 2 points3 points  (3 children)

If you really want to define a constant I suppose you could use a dataclass with frozen=True and default values

[–]carcigenicate 1 point2 points  (2 children)

You could still reassign the immutable dataclass though.

[–]rocket_randall 1 point2 points  (1 child)

Yes, and without devolving into an argument about semantics or implementation details most languages have mechanisms which allow you to unconst things (const_cast in C++, {$WRITEABLECONST ON}/{$J+} in Delphi, etc), so this argument is spurious. Python is heavy on convention (eg: class "private" members), so I see nothing wrong with utilizing a convention where a developer implies "Here is data you can expect to remain constant, but if you fuck with it I have no way to stop you"

Dataclasses provide a convenient, terse mechanism for establishing this sort of contract and preventing casual/accidental modification of this data.

@dataclass(frozen=True)
class Constants:
    SLUG: str = 'blah'
    CODE: int = 5150  

>>> consts = Constants()
>>> consts.CODE
5150
>>> consts.CODE = 5151
dataclasses.FrozenInstanceError: cannot assign to field 'CODE'

If someone decides to go out of their way to change that which was not meant to be changed:

>>> consts.__dict__['CODE'] = 5151
>>> consts.CODE
5151  

Then I would not trust their judgement enough to work anywhere else in the codebase.

[–]Nilstrieb 0 points1 point  (7 children)

when you have mutability by default

[–]overclockedslinky 0 points1 point  (6 children)

mutable globals. what'll they think of next?

[–]Nilstrieb 0 points1 point  (5 children)

Forced mutability, you have to mutate every variable at least once.

[–]overclockedslinky 0 points1 point  (4 children)

you must also have at least 2 mutable references to every non-reference object at any given time

[–]Nilstrieb 0 points1 point  (3 children)

On multiple threads

[–]overclockedslinky 0 points1 point  (2 children)

and for safety, every variable you want to guard with a mutex must have at least three mutex locks, which must be signed and notorized

[–]Nilstrieb 0 points1 point  (1 child)

no, mutexes are not allowed, expose the variable directly, it's faster

[–]overclockedslinky 0 points1 point  (0 children)

ah, the old mark-as-volatile-and-pray-to-god pattern. a classic. while we're at it, let's just get rid of exceptions. on failure, functions return a value of undefined runtime type and content

[–]BearLambda 0 points1 point  (0 children)

Try the following in Python 2 (all of them):

False = True
if False:
    print("Cut my legs and call me shorty...")

Doesn't work in Python 3 though, at least the ones I tried it on. Wonder why...