this post was submitted on 21 Nov 2024
283 points (90.8% liked)

Programmer Humor

32559 readers
604 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

Python allows programmers to pass additional arguments to functions via comments. Now armed with this knowledge head out and spread it to all code bases.

Feel free to use the code I wrote in your projects.

Link to the source code: https://github.com/raldone01/python_lessons_py/blob/main/lesson_0_comments.ipynb

Image transcription:

from lib import add

# Go ahead and change the comments.
# See how python uses them as arguments.

result = add()  # 1 2
print(result)
result = add()  # 3 4
print(result)
result = add()  # 3 4 5 20
print(result)

Output:

3
7
32
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 22 hours ago

The best language is complete, succinct, orderly and clear. And never adds a single goddamn thing ever.

[–] [email protected] 4 points 22 hours ago

That is C++ levels of "why the fuck did they add that."

[–] [email protected] 7 points 23 hours ago

bro what we are devolving

[–] [email protected] 12 points 1 day ago

Thanks, I hate it.

[–] [email protected] 16 points 1 day ago

Why is this a thing

[–] [email protected] 11 points 1 day ago (1 children)

I hate this shit being routinely used in PHP. Symfony uses those functional comments for routing, essentially scanning every controller file as text on every visit, to gather the url patterns above functions. Laravel uses Reflection, which is functionally the same thing, to provide arguments to controller functions. Also, kind of related, the project I'm working now has few functions that use backtrace to return different results based on where they are called from. It is indeed very cursed and I'm ripping out any usages of them whenever I see one.

[–] [email protected] 2 points 22 hours ago

Comment Annotations were a nessecary thing as php did not support a native way to do it. However, since php 8, there is now native attributes.

[–] [email protected] 32 points 1 day ago

This is heresy.

[–] [email protected] 14 points 1 day ago

we need a programming horror community for stuff like this

[–] [email protected] 36 points 1 day ago

This is an affront to nature. Comments shouldn't even make it past the scanner.

[–] [email protected] 39 points 2 days ago

They chose violence.

[–] [email protected] 8 points 2 days ago (1 children)

That's quite cool. But I'm not sure what's the use case for it.

[–] [email protected] 1 points 1 day ago

To make bugs less traceable, I guess.

[–] [email protected] 1 points 2 days ago
[–] [email protected] 65 points 2 days ago (1 children)

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.

[–] [email protected] 23 points 1 day ago (1 children)

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

[–] [email protected] 9 points 1 day ago (1 children)

I'm going to say it just is misinformation, if that's what "lib" is here.

[–] [email protected] 3 points 18 hours ago (1 children)

Yeah. 'lib' isn't a standard Python library, it's the name of the abomination that this person created. Since python has quite a bit of useful introspection, they can do something like:

  • get the stack
  • find the exact call to abomination.add()
  • reparse the text of that line, turn the text of the comment into actual numbers, and add them

Now, I don't know if python keeps the comments around, so it may involve getting the filename and line number, reading the file, and manually extracting the comment text from that line.

[–] [email protected] 2 points 18 hours ago* (last edited 18 hours ago)

It's not even actually called lib. The line just straight up isn't in the image "transcribed", and it's from arglib import comment_arguments in the original code.

Yeah, I gave this one a downvote.

[–] [email protected] 42 points 2 days ago (1 children)

This is some javascript level shit

[–] [email protected] 7 points 1 day ago

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"

[–] [email protected] 74 points 2 days ago (1 children)

I fucking hate this, thanks OP

[–] [email protected] 7 points 2 days ago

How do I delete this part of the python documentation?

load more comments
view more: next ›