<p>A lot of languages have some form of constant variables - hell, even JavaScript has them.</p>
<p>Yet Python doesn't. What possible benefits does this have for the Python language?</p>

<language-design><python>
What benefits does Python gain from not having constants?
CC BY-SA 4.0
2023-05-18T06:46:08.633Z
My biggest pet peeve with Python & Lua actually
CPlus
<p>I'm not sure that Python specifically doesn't have them because of any concrete advantages to not having them. That is, there may be such advantages (one is discussed in <a href="https://languagedesign.stackexchange.com/a/498/89">mousetail's answer</a>), but I don't think these advantages are what motivated Python's design decision in this case.</p>
<p>I suspect the main reason Python doesn't have them is simply because constants aren't needed to do anything new; any program which uses constants can be written without them, and will have the same behaviour (barring pathological cases where a program intentionally tries to assign to a constant and then catch the exception at runtime). Like access modifiers and type annotations, they can be used to check the correctness of a program but they don't change the program's behaviour, so many dynamic languages including Python and its contemporaries either don't have them, or added them significantly later (like <code>const</code> was eventually added to Javascript).</p>
<p>This is similar to the principle that &quot;we're all consenting adults&quot;, which is a part of Python's design philosophy: the language itself doesn't need to stop you from doing things that you don't want to do. This principle can certainly be criticised, and many other languages don't adopt it, but for better or worse it is something that Python's design is based on.</p>

CC BY-SA 4.0
2023-05-18T07:14:04.830Z
2023-05-18T07:14:04.830Z
kaya3
<h2>Monkey Patching</h2>
<p>A core feature of Python is that absolutely everything is accessible and modifiable. Even things like closure bound variables, which are still truly private even in JavaScript, are accessible and mutable in python.</p>
<p>A major benefit of this system is you can modify the behavior of any code, even code inside third party modules or constants there in. This functionally is used extensively for things like unit testing when you may want to isolate some functionality but control the rest. Everything being mutable allows you to do that</p>
<p>If Python added constant variables, it would mean some things would not be patch-able like is conventional in Python, removing a major benefit from Python.</p>
<p>Similar to how private variables are marked with <code>_</code> in Python, constants are marked with ALL_CAPS. It's up to the programmer to choose to actually not modify them or not.</p>
<p>Also Python doesn't have variable declarations at all so it would be a much bigger step to add them, compared to JavaScript which already had <code>var</code> when <code>const</code> was introduced.</p>

CC BY-SA 4.0
2023-05-18T06:49:57.153Z
2023-05-18T12:15:54.837Z
2023-05-18T12:15:54.837Z
mousetail 'he-him'
# What benefits does Python gain from not having constants?
Posted by: username_0 on 2023-05-18
A lot of languages have some form of constant variables - hell, even JavaScript has them.

Yet Python doesn't. What possible benefits does this have for the Python language?
**Tags:** language-design python
> **Comments:**
> **username_1**: My biggest pet peeve with Python & Lua actually  
---
## 2 answers
### Answer by username_2 on 2023-05-18. Score: 12
I'm not sure that Python specifically doesn't have them because of any concrete advantages to not having them. That is, there may be such advantages (one is discussed in [mousetail's answer](https://languagedesign.stackexchange.com/a/498/89)), but I don't think these advantages are what motivated Python's design decision in this case.

I suspect the main reason Python doesn't have them is simply because constants aren't needed to do anything new; any program which uses constants can be written without them, and will have the same behaviour (barring pathological cases where a program intentionally tries to assign to a constant and then catch the exception at runtime). Like access modifiers and type annotations, they can be used to check the correctness of a program but they don't change the program's behaviour, so many dynamic languages including Python and its contemporaries either don't have them, or added them significantly later (like `const` was eventually added to Javascript).

This is similar to the principle that "we're all consenting adults", which is a part of Python's design philosophy: the language itself doesn't need to stop you from doing things that you don't want to do. This principle can certainly be criticised, and many other languages don't adopt it, but for better or worse it is something that Python's design is based on.
---
### Answer by username_3 on 2023-05-18. Score: 11
## Monkey Patching

A core feature of Python is that absolutely everything is accessible and modifiable. Even things like closure bound variables, which are still truly private even in JavaScript, are accessible and mutable in python.

A major benefit of this system is you can modify the behavior of any code, even code inside third party modules or constants there in. This functionally is used extensively for things like unit testing when you may want to isolate some functionality but control the rest. Everything being mutable allows you to do that

If Python added constant variables, it would mean some things would not be patch-able like is conventional in Python, removing a major benefit from Python.

Similar to how private variables are marked with `_` in Python, constants are marked with ALL\_CAPS. It's up to the programmer to choose to actually not modify them or not.

Also Python doesn't have variable declarations at all so it would be a much bigger step to add them, compared to JavaScript which already had `var` when `const` was introduced.
---
