this post was submitted on 21 Nov 2024
281 points (90.7% liked)

Programmer Humor

32557 readers
595 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
[–] Rozauhtuno@lemmy.blahaj.zone 12 points 1 day ago

Thanks, I hate it.

[–] flashgnash@lemm.ee 16 points 1 day ago

Why is this a thing

[–] aliser@lemmy.world 8 points 1 day ago

bro what we are devolving

[–] infinite_ass@leminal.space 4 points 22 hours ago

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

[–] mindbleach@sh.itjust.works 4 points 22 hours ago

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

[–] drathvedro@lemm.ee 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.

[–] piccolo@sh.itjust.works 2 points 23 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.

[–] daniskarma@lemmy.dbzer0.com 32 points 1 day ago

This is heresy.

[–] HStone32@lemmy.world 36 points 1 day ago

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

[–] yogthos@lemmy.ml 14 points 1 day ago

we need a programming horror community for stuff like this

[–] bdonvr@thelemmy.club 195 points 2 days ago (14 children)

IMO comments should never ever be parsed under any circumstances but I probably don't know enough to really speak on this

[–] jaxxed@lemmy.world 4 points 1 day ago (1 children)

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.

[–] bastion@feddit.nl 4 points 22 hours ago

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.

[–] Artyom@lemm.ee 5 points 1 day ago (1 children)

This isn't standard python. lib is not in the standard library. Python also doesn't have any special variables where it stores comments, other than __doc__ which stores a docstring. If I had to guess, add is reading the file/REPL via __file__.

[–] driving_crooner@lemmy.eco.br 1 points 1 day ago* (last edited 1 day ago) (1 children)

Is __doc__ storing a comment or just a text string?

[–] Artyom@lemm.ee 1 points 23 hours ago

It's a string, although sometimes Python conflates the two. The recommended way to make a multi-line comment is to just make a multi-line string and just don't use it.

[–] jedibob5@lemmy.world 85 points 2 days ago

No, your intuition is correct, this is extremely cursed.

[–] perviouslyiner@lemmy.world 55 points 2 days ago* (last edited 2 days ago) (5 children)

Seen in a code review (paraphrased):

image of a program which is estimating the size of an array by counting how many lines of source code were used to construct it

"Why does this break when you add comments in the middle?"

load more comments (5 replies)
[–] Rooki@lemmy.world 3 points 1 day ago (2 children)

Comments should be removed before shipping.

[–] lud@lemm.ee 4 points 1 day ago (1 children)

Python is an interpreted language but for a compiled language absolutely (and obviously).

[–] Rooki@lemmy.world 2 points 1 day ago

I guess there could be just a script before deployment.

[–] raldone01@lemmy.world 3 points 1 day ago

Well now that causes breakage two dependencies down the line. Good luck with that. 😅

load more comments (9 replies)
[–] NigelFrobisher@aussie.zone 39 points 2 days ago

They chose violence.

[–] would_be_appreciated@lemmy.ml 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.

[–] Swedneck@discuss.tchncs.de 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

[–] CanadaPlus@lemmy.sdf.org 9 points 1 day ago (1 children)

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

[–] bastion@feddit.nl 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.

[–] CanadaPlus@lemmy.sdf.org 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.

[–] arisunz@lemmy.blahaj.zone 74 points 2 days ago (1 children)

I fucking hate this, thanks OP

load more comments (1 replies)
[–] MrPoopyButthole@lemmy.world 87 points 2 days ago

That's disgusting

[–] shotgun_crab@lemmy.world 42 points 2 days ago (1 children)

This is some javascript level shit

[–] ByteOnBikes@slrpnk.net 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"

[–] RichardoC@lemmy.world 68 points 2 days ago

Thank you, I hate it

[–] scrubbles@poptalk.scrubbles.tech 82 points 2 days ago (2 children)

checks the community to make sure I'm in programmer humor

Yeah that checks out

load more comments (2 replies)
[–] Ephera@lemmy.ml 49 points 2 days ago (1 children)
[–] OsrsNeedsF2P@lemmy.ml 27 points 2 days ago

Yup, the function actually goes and finds the code that calls it and parses the comment.

Disgusting.

[–] doeknius_gloek@discuss.tchncs.de 46 points 2 days ago (4 children)

This does not actually work, right? Right?

[–] Swedneck@discuss.tchncs.de 1 points 21 hours ago

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

[–] justcallmelarry@lemmy.dbzer0.com 50 points 2 days ago* (last edited 2 days ago) (6 children)

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

load more comments (6 replies)
[–] FourPacketsOfPeanuts@lemmy.world 32 points 2 days ago* (last edited 2 days ago) (1 children)
load more comments (1 replies)
load more comments (1 replies)

Every day further from god's light etc...

[–] embed_me@programming.dev 32 points 2 days ago (5 children)

As if I needed more reasons to start away from python

load more comments (5 replies)
[–] LolaCat@lemmy.ca 30 points 2 days ago

I feel sick

load more comments
view more: next ›