From my understanding, a constant is a value which is assigned only once and cannot change at runtime, whereas variables have mutable values which are unpredictable by nature.

My question is, to what extent can it be said that a value is known? Can values which are only assigned once but have some intrinsic "randomness" to them — for example, a UUID which is only set once per process, ever be called constant? Does any form of randomness disqualify something as a constant, even if the assigned value follows a completely predictable pattern?

This is a rather silly semantic question, but I found clashing opinions in other accepted answers while researching this topic [link]:

Variables are not called variables because their values may be changed, but because their value isn't known in advance – when writing the code, they are placeholders for values that will be determined later. In contrast, the value of a constant is known when the program is written.

A constant, in any programming language, is an identifier whose value is not meant to be changed once it is set. It does not matter when this value is learned so long as the constant can not be accessed with anything but this one value.

I take "it does not matter when this value is learned" to imply that it could apply to a mutable value generated at runtime, so long as that value is immutable throughout the program's execution (such as the UUID example).

For the purpose of facilitating comprehension by others, is it always bad practice to flag a pseudo-random integer as a constant by writing it in upper-case? Particularly in languages that don't define a constant type to begin with (e.g. Python), is it detrimental to do so?

  • "Can values which are only assigned once but have some intrinsic "randomness" to them — for example, a UUID which is only set once per process, ever be called constant?" - they most certainly can. The nature of the value does not matter at all, doesn't even matter what the value actually is, a constant is just something that can't be changed throughout the execution of the program (every mention can be substituted with the exact same value). From the perspective of writing programs, we're mostly interested in named constants, precisely because we don't care about the actual value. Commented Nov 19, 2022 at 3:46

2 Answers 2


These terms like “constant” or “variable” have no clear unambiguous definition. They might have specific meaning though in the context of a particular programming language or runtime.

For example, C++ has the concept of a constexpr that will compute the value at compile time. Since there is no compile-time I/O, the value would be deterministic.

Many languages (e.g. Java) have the concept of something like a final static variable that is assigned once during the initialization phase of a program, and never changed for the remaining lifetime of a process. It's not a compile-time constant, it may or may not be deterministic, but it will not change.

Python doesn't really have a concept of constants in the sense that their value would be computed at compile time. We only have variables that are assigned during the execution of the program. However, we have conventions like PEP-8 to uppercase variables that are used as constants, facilities like typing.Final to tell the type checker (if used) that a variable should not be reassigned, and tools like properties/descriptors to produce a runtime error when assignment is attempted.

Whether you call a variable a “constant” should depend more on what you're trying communicate. Your random UUID example might confuse some people, so I would avoid referring to such a variable as a “constant”.

When I wrote “the value of a constant is known when the program is written”, I was not trying to start a philosophical discussion about the nature of knowledge, but to help the OP of that Q&A understand that variables do not have to involve mutation – calling an assign-once local variable containing an I/O result a “constant” would be confusing.

  • There are things like strlen("hello") in C, which is 5 and will never be anything other than 5, but legally it is not a constant.
    – gnasher729
    Commented Nov 19, 2022 at 18:20
  • @gnasher729 Sure, but the question and the answer are not talking about the semantics of a particular language feature, but about the semantics of the concept being expressed/communicated by the programmer. Commented Nov 19, 2022 at 18:35

No, because UUIDs cannot be identifiers.

A UUID is not a constant, because constants are identifiers. As a UUID can start out with a numeric, you won't be able to use a UUID as an identifier, e.g.

const 123e4567-e89b-12d3-a456-426614174000 = 0;

This can't work because of the "1" in initial position, so the UUID itself is not a suitable constant. The content of a UUID could be assigned to a constant, that is defined as a constant. This happens often:

const myconst = "123e4567-e89b-12d3-a456-426614174000";

  • Pi is not a constant?
    – Bossie
    Commented Nov 21, 2022 at 5:35
  • So, by "identifier" I assume you mean an identifier in your favorite programming language. Why would you want to use a guid for that anyway? Constants are not defined by their ability to be used as names; they're defined by their quality of being, well, constant: unchanging. Commented Apr 15, 2023 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.