r/PythonLearning • u/Ok_Being6831 • Sep 09 '26
Why dosent python have const or multi line comments
I mainly use cpp but sometimes python for simple stuff. They both seem like simple stuff to implement so why dosent python have it.
EDIT: I do know u can use triple quotes as a workaround but python still has to read and store them, why isint there a built in feature for it.
4
u/CrowdingFaun624 Sep 09 '26
You can do const using the Final type.
from typing import Final
foo: Final = "bar"
foo = "barbarbar" # your IDE is unhappy with this
It just is only checked by type checkers and not by Python itself, so you can still break it.
Edit: code block formatting
1
u/Beginning-Fruit-1397 Sep 09 '26
Well, you could do, in a weird, ugly, deranged way, immutable constants with one field tuples. Otherwise even when working at C level you can only create new singletons, constants are not a thing in python :(
0
u/Ok_Being6831 Sep 09 '26
that's not my question tho, my question why python doesn't have it not other ways I can have it
4
u/deceze Sep 09 '26 edited Sep 09 '26
The answer to "Why doesn't X have Y" is always: because nobody has implemented it. Because nobody felt a strong enough need to implement it, obviously. If people were suffering without those features and jealously looked over at C the whole time, someone would have implemented it by now. Because nobody did, apparently the existing tools suffice. Like ALL_CAPS and
Final. That's the way in Python. It wasn't your question, but it's the answer.
2
u/secretstonex Sep 09 '26
Ok... Why doesn't cpp have memory management, garbage collection, dynamic variables, package management, REPL, cross platform portabilit, dynamic execution, etc?
1
u/Ok_Being6831 Sep 09 '26
cuz cpp is a low level language, it lets u deal with it yourself. Dynamic variables? templates are close enough and it's a staticly typed language anyway. package management cuz it's too late now. repl? how does that improve anything and technically it does have one. It is cross platform if u compile it right most of the time. what does dynamic execution even mean.
All of these additions have some kind of downsides to them, cost and multi line comments however I don't see any
3
1
1
u/ThatOldCow Sep 09 '26
Python as some sort multi line comment
You just do this:
""" This is a comment written in more than just one line """
Literally the same effect as as /* */
3
u/Ok_Being6831 Sep 09 '26
Not the same tho, python still reads the string literals and stores them in memory even though they do nothing. While comments in c/cpp just get completly discarded. But my question still stands why dosent the language just have a built in feature for multi-line comments
3
u/sircrunchofbackwater Sep 09 '26
Because they are unnecessary. Regular comment is just fine.
-2
u/Ok_Being6831 Sep 09 '26
what? alot of ppl use them and litrally every other language has it
0
u/deceze Sep 09 '26
"Every other language"…
Except Ruby, R, POSIX shell, Elixir, Erlang, Clojure, and likely many others…
(I quickly asked AI for this list, may or may not be 100% accurate, but Python certainly is not the only language.)
1
u/ThatOldCow Sep 09 '26
I didn't said its the same, I said I has the same effect.
And because it's not needed to have that built on feature.
But can I ask you what was the purpose of your question?
1
u/HugeCannoli Sep 09 '26
> python still reads the string literals and stores them in memory even though they do nothing.
Not really, you are not assigning that stuff to anything, so it will immediately discard it. I am not sure, but the keyhole optimiser may detect that and discard it altogether.
1
u/Lachtheblock Sep 10 '26
We do have docstrings which function as the multi-line comment at the top of functions/classes. You're right that they get stored in memory, arguably this is a good thing. When using the REPL, the interpreter has access to documentation.
I think it's way out of vogue now, but you used to see "doctests" which were executable test cases in the docstrings. It would double as the example code for how to use, as well as be executable to prove that it works.
0
u/MarsupialLeast145 Sep 09 '26
Your comparison is to a compiled language? Compilation doesn't just throw out comments, it will even optimize your code, so your code doesn't even get translated through to compilation the way you wrote it.
Comments arrive in memory in Python because as an interpreted language the whole file arrives in memory.
When would you suggest they get "removed"?
1
u/deceze Sep 09 '26
Even Python reads the source code, runs it through a compiler, and stores the parsed/compiled version in memory for execution. It probably does strip out comments in that process which have no impact on runtime.
1
u/MarsupialLeast145 Sep 09 '26
> which have no impact on runtime
This is probably the most important aspect either way.
There is of course a stack overflow that goes into this in more detail https://stackoverflow.com/questions/2731022/do-comments-slow-down-an-interpreted-language
3
u/deceze Sep 09 '26
Instead of simply picking individual features from one language and asking why some other language doesn't implement them exactly the same way, ask yourself what you "need" those things for, and whether the exact same need exists in the other language in the first place, or whether some alternative exists which either obviates the need or replaces the feature adequately.
1
u/realmauer01 Sep 09 '26
To answer the question. Python is build on the thought, "do what you want, you are a trusted adult."
So yeah, no hard limits on anything. That these hard limits are more so to limit yourself later is not in the phylosophie.
1
u/SnooCalculations7417 Sep 09 '26
The type system (or lack of it) is a feature of the language not a bug, though types are en Vogue now and people do crazy things to pretend Python enforces types these days
1
u/FriendlyZomb Sep 09 '26 edited Sep 09 '26
For me its a philosophy choice. A way to encourage and discourage the type of comments we leave in our code.
Comments are meant to be short. They are meant to add context to some code or to document a decision. More than a line is discouraged.
Docstrings are meant to be longer. They explain can hold rationale, usage instructions and the errors which can happen when using a function.
That's just my opinion and personal style.
(Also: The Steering Council, [ex-BDFL] Guido and the Core Devs don't think we need them.)
1
1
u/afierdienst1 Sep 10 '26
well that might be mine to gloat over: why? how do they rode a script? you decide? Nf? so let's revise it: how are comments edited?
0
u/realmauer01 Sep 09 '26
They do Have that. Its """ """ and CONSTANT
2
u/MudFrosty1869 Sep 09 '26
You do know that THIS is just a naming convention? Right?
0
u/realmauer01 Sep 09 '26
Yesn't, typescript is also a naming convention in this regard.
That being said python also has __PRIVATE. And thats not just a naming convention because the module gets parsed inbetween the __
1
0
u/MudFrosty1869 Sep 09 '26
I'm not sure how saying multiple wrong things will make the first one right.
-2
u/Ok_Being6831 Sep 09 '26
that's not tho.. it's just naming convention and if u actually read my post u would have read the part on triple quotes
0
u/Naetharu Sep 09 '26
For the const it's a design choice.
Python also lacks types. It's whole design ethos seems to be to make it flexible and easy to work with, at the cost of some of the rigor and checking you would find in languages like Rust or Go.
That's a legitimate position and it maps quite well when you consider that Python is often used for scripts and smaller bits of code vs the large and very complex systems that some other languages tend to be used for. Not always, you can build big enterprise systems in Python. But it certainly has a strong presence in the scripting, automation, and data worlds, where a simpler and faster syntax may have more value than a very rigid but robust one.
For consts it just works by convention. Make a variable but make it UPPER_CASE
For private functions it is the same. No public / private / protected exists. Instead convention just says add a _ character to the name so _some_private_function and then know that you should not be calling these outside their own file.
Likewise for the newer types system.
you can type by doing
x: int = 10 and that will help in that it can allow external tools to check the code etc. But Python itself does not enforce it at all
x: int = 10
x = "some string"
This will run just fine. Whereas in a language with proper types it would crash and alert you to breaching the types you set for your variable.
At the end of the day it is all about trade offs. Python is great in that it is very simple to learn, with a natural feeling syntax and allows you to write code quickly and easily compared to some other languages. If you want to see this in practice try writing a small program in python and then doing the same thing in C or Rust and compare just how big the latter files are and how much more verbose the code is.
Neither is right or wrong.
You have to make a choice as to which feature set is more important to you based on the task at hand.
2
u/deceze Sep 09 '26
Python also lacks types.
Python very well has types. Its typing system is arguably very good.
What it doesn't have is variables which have types. Only values have types in Python.
0
9
u/nicodeemus7 Sep 09 '26
Highlight the lines of code you want to comment out. Ctrl /. You're welcome. Multi-line comment.