Thanks, I hate it.
Programmer Humor
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
- Posts must be relevant to programming, programmers, or computer science.
- No NSFW content.
- Jokes must be in good taste. No hate speech, bigotry, etc.
Why is this a thing
Because whoever wrote this went to great lengths to make it work. It's by no means a feature of python. It's a feature of their code.
bro what we are devolving
That is C++ levels of "why the fuck did they add that."
The best language is complete, succinct, orderly and clear. And never adds a single goddamn thing ever.
IMO comments should never ever be parsed under any circumstances but I probably don't know enough to really speak on this
Can we just clarify that you mean that comments should never be parsed by the language engine. There are valid annotation systems, but the goal is alway to ensure that one passable can never impact the other.
Imagine if here a comment could create a syntax error! This is even worse for runtime scripting languages like python.
Sure, but let's just clarify that this is someone going out of their way to create this problem, using Python's ability to read it's own code.
Basically, you can load any text file, including a source code file, and do whatever you want with it.
So, a function can be written that finds out whatever's calling it, reads that file, parses the comments, and uses them as values. This can also be done with introspection, using the same mechanism that displays tracebacks.
Conveniently Python keeps the comments around. 😄
No, your intuition is correct, this is extremely cursed.
Seen in a code review (paraphrased):
"Why does this break when you add comments in the middle?"
That's disgusting
checks the community to make sure I'm in programmer humor
Yeah that checks out
Thank you, I hate it
I assume the people freaking out about how dumb python is didn't bother to read the code and have never coded in python in their life, because the behavior here is totally reasonable. Python doesn't parse comments normally, which is what you'd expect, but if you tell it to read the raw source code and then parse the raw source code for the comments specifically, of course it does.
You would never, ever accidentally do this.
...you'd also never, ever do it on purpose.
yeah frankly this post is borderline misinformation, they specifically import a library to read comments as arguments, it's like redefining keywords in C and complaining about C being dumb
Implementation of the add()
function is here: https://github.com/raldone01/python_lessons_py/blob/main/lib.py
It is now directly in the notebook in the latest version: https://github.com/raldone01/python_lessons_py/blob/v2.0.0/lesson_0_comments.ipynb
Yup, the function actually goes and finds the code that calls it and parses the comment.
Disgusting.
This does not actually work, right? Right?
they have to import a separate library to do this, it's not a part of standard python, and this post is basically just misinformation
The add()
function (that is available in the source code) basically uses some built in debugging tools to find out where in the code the function is called, and then parses the comment from the file and uses it for adding stuff.
I’ve never tried (becuse why would you…) but something similar can probably be built in any interpreted language
It’s not something Python does by design
Every day further from god's light etc...
This is some javascript level shit
It's actually kind of nice to see this as a JS developer.
Not like, "Oh wow this is neat!"
But like, "Finally the golden child, Python, also has some fucked up shit"
They chose violence.
This is an affront to nature. Comments shouldn't even make it past the scanner.
This is heresy.
I feel sick