this post was submitted on 21 Nov 2024
295 points (90.9% liked)

Programmer Humor

32562 readers
304 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/v2.0.0/lesson_0_comments.ipynb

Image transcription:

# First we have to import comment_arguments from arglib
# Sadly arglib is not yet a standard library.
from arglib import comment_arguments


def add(*args, **kwargs):
    c_args, c_kwargs = comment_arguments()
    return sum([int(i) for i in args + c_args])


# Go ahead and change the comments.
# See how they are used as arguments.

result = add()  # 1, 2
print(result)
# comment arguments can be combined with normal function arguments
result = add(1, 2)  # 3, 4
print(result)

Output:

3
10

This is version v2.0.0 of the post: https://github.com/raldone01/python_lessons_py/tree/v2.0.0

Note:

v1.0.0 of the post can be found here: https://github.com/raldone01/python_lessons_py/tree/v1.0.0

Choosing lib as the name for my module was a bit devious. I did it because I thought if I am creating something cursed why not go all the way?

Regarding misinformation:

I thought simply posting this in programmer humor was enough. Anyways, the techniques shown here are not yet regarded best practice. Decide carefully if you want to apply the shown concepts in your own code bases.

top 50 comments
sorted by: hot top controversial new old
[–] Rozauhtuno@lemmy.blahaj.zone 17 points 3 days ago

Thanks, I hate it.

[–] flashgnash@lemm.ee 18 points 3 days ago (1 children)
[–] Doxin@pawb.social 2 points 17 hours ago (1 children)

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.

[–] HStone32@lemmy.world 39 points 4 days ago

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

[–] daniskarma@lemmy.dbzer0.com 34 points 4 days ago

This is heresy.

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

[–] bdonvr@thelemmy.club 197 points 5 days ago (19 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 8 points 3 days 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 7 points 3 days ago (1 children)

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.

[–] raldone01@lemmy.world 1 points 1 day ago* (last edited 15 hours ago)

Conveniently Python keeps the comments around. 😄

[–] jedibob5@lemmy.world 87 points 5 days ago

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

[–] perviouslyiner@lemmy.world 58 points 5 days ago* (last edited 5 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)
load more comments (16 replies)
[–] aliser@lemmy.world 8 points 3 days ago

bro what we are devolving

[–] would_be_appreciated@lemmy.ml 68 points 5 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 25 points 4 days 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 12 points 4 days ago (1 children)

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

[–] bastion@feddit.nl 4 points 3 days 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 4 points 3 days ago* (last edited 3 days ago) (1 children)

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.

[–] raldone01@lemmy.world 2 points 1 day ago* (last edited 15 hours ago) (1 children)

I updated the source after this post was made. The image transcription still holds. I did not update the image and the post text.

You can view the git history. I will tag the specific commit at the time of the post later and update it accordingly.

[–] CanadaPlus@lemmy.sdf.org 2 points 1 day ago

Ah, and you're the author. That kind of changes the whole context here.

It doesn't mean much, but have your upvote back.

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

I fucking hate this, thanks OP

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

That's disgusting

[–] mindbleach@sh.itjust.works 5 points 3 days ago

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

[–] NigelFrobisher@aussie.zone 40 points 4 days ago

They chose violence.

[–] scrubbles@poptalk.scrubbles.tech 84 points 5 days ago (1 children)

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

Yeah that checks out

[–] raldone01@lemmy.world 23 points 5 days ago* (last edited 15 hours ago) (1 children)

You know that this is acutally working right??? 😊

load more comments (1 replies)
[–] RichardoC@lemmy.world 68 points 5 days ago

Thank you, I hate it

[–] yogthos@lemmy.ml 16 points 4 days ago

we need a programming horror community for stuff like this

[–] infinite_ass@leminal.space 4 points 3 days ago

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

[–] shotgun_crab@lemmy.world 43 points 5 days ago (1 children)

This is some javascript level shit

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

[–] Ephera@lemmy.ml 50 points 5 days ago (2 children)
[–] raldone01@lemmy.world 2 points 1 day ago* (last edited 15 hours ago)
[–] OsrsNeedsF2P@lemmy.ml 30 points 5 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 5 days ago (4 children)

This does not actually work, right? Right?

[–] justcallmelarry@lemmy.dbzer0.com 52 points 5 days ago* (last edited 5 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 33 points 5 days ago* (last edited 5 days ago) (1 children)
load more comments (1 replies)
load more comments (2 replies)

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

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

As if I needed more reasons to start away from python

[–] Chais@sh.itjust.works 22 points 5 days ago

You can so stupid shit in any language. I admit Python doesn't exactly make it difficult. A bit like JS, but different.

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

I feel sick

load more comments
view more: next ›