Skip to main content
13 events
when toggle format what by license comment
Oct 20, 2023 at 16:07 comment added Barmar And only a string literal at the beginning of a definition is preserved as a docstring; any others will be ignored.
Oct 20, 2023 at 16:06 comment added Barmar @JörgWMittag They serve a similar purpose as comments, since they explain/describe the code in some way. The fact that they're preserved at runtime doesn't negate that. You can also use string literals outside definitions, and then they really are ignored like comments. Since Python doesn't have multi-line comments like /* ... */, triple-quoted strings are often used as a replacement.
Oct 20, 2023 at 16:03 comment added Barmar I think Python adopted this from Common Lisp.
Oct 15, 2023 at 11:17 comment added feldentm Python is a language that is commonly used in ways that execute each line of code only once. In such use cases, it does degrade performance as compilation is a major part in these cases.
Oct 14, 2023 at 16:18 comment added kaya3 This doesn't degrade performance at all, at least beyond the cost of parsing the string literal (which is one-off and negligible, possibly not even more expensive than parsing a comment beginning with #). CPython absolutely recognises a string literal on its own as a noop and doesn't emit any bytecode instructions for it.
Jul 5, 2023 at 11:16 comment added Jörg W Mittag Note that docstrings are not comments. They are syntactic sugar for assigning to an attribute named __doc__. The whole point of docstrings is that, unlike comments, they are not ignored but kept around at runtime so you can query them at runtime.
May 20, 2023 at 20:23 comment added LHP A string that is not assigned to anything - or any constant expression in "void context" for that matter - would be trivial to compile into nothing at all. No performance hit whatsoever. One (minor) disadvantage would be that such a comment can't easily be placed inside an expression. Also, in a language with a statement terminator or separator (typically semicolon), it might look a little awkward.
May 19, 2023 at 20:05 comment added Ginger @user16217248 Is the performance loss actually measurable, though? Worrying about that feels like worrying that one day a tree will grow where I'm standing.
May 19, 2023 at 19:50 comment added CPlus @Ginger Initialized is still overhead. Whereas comments are ignored entirely.
May 19, 2023 at 19:39 comment added Ginger @user16217248 How would this degrade performance? The strings would just be initialized and then left untouched for the rest of execution, right?
May 19, 2023 at 17:46 comment added CPlus Personally I dislike this, because it degrades performance.
S May 19, 2023 at 16:57 review First answers
May 19, 2023 at 17:05
S May 19, 2023 at 16:57 history answered cocomac CC BY-SA 4.0