this post was submitted on 21 Nov 2024
302 points (91.0% liked)

Programmer Humor

32591 readers
934 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 20 points 6 days ago

Thanks, I hate it.

[–] flashgnash@lemm.ee 19 points 6 days ago (1 children)
[–] Doxin@pawb.social 5 points 4 days 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.

[–] aliser@lemmy.world 8 points 6 days ago

bro what we are devolving

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

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

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

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

[–] bdonvr@thelemmy.club 199 points 1 week 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 6 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 9 points 6 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 4 days ago* (last edited 4 days ago)

Conveniently Python keeps the comments around. 😄

[–] jedibob5@lemmy.world 87 points 1 week ago

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

[–] perviouslyiner@lemmy.world 58 points 1 week ago* (last edited 1 week 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)
[–] MrPoopyButthole@lemmy.world 90 points 1 week ago

That's disgusting

[–] scrubbles@poptalk.scrubbles.tech 86 points 1 week ago (2 children)

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

Yeah that checks out

load more comments (2 replies)
[–] arisunz@lemmy.blahaj.zone 78 points 1 week ago (1 children)

I fucking hate this, thanks OP

load more comments (1 replies)
[–] RichardoC@lemmy.world 69 points 1 week ago

Thank you, I hate it

[–] would_be_appreciated@lemmy.ml 69 points 1 week 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 1 week ago (5 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

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

This does not actually work, right? Right?

[–] Swedneck@discuss.tchncs.de 1 points 6 days 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 52 points 1 week ago* (last edited 1 week 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 1 week ago* (last edited 1 week ago) (1 children)
load more comments (1 replies)
load more comments (1 replies)

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

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

This is some javascript level shit

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

[–] NigelFrobisher@aussie.zone 40 points 1 week ago

They chose violence.

[–] HStone32@lemmy.world 39 points 1 week ago

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

[–] daniskarma@lemmy.dbzer0.com 34 points 1 week ago

This is heresy.

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

As if I needed more reasons to start away from python

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

I feel sick

load more comments
view more: next ›