326
submitted 2 months ago* (last edited 1 month ago) by raldone01@lemmy.world to c/programmerhumor@lemmy.ml

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
[-] bdonvr@thelemmy.club 195 points 2 months ago

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

[-] jedibob5@lemmy.world 87 points 2 months ago

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

[-] perviouslyiner@lemmy.world 61 points 2 months ago* (last edited 2 months ago)

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?"

[-] bleistift2@sopuli.xyz 18 points 2 months ago

Why would python even expose the current line number? What’s it useful for?

[-] raldone01@lemmy.world 38 points 2 months ago* (last edited 1 month ago)

On a serious note:

This feature is actually very useful. Libraries can use it create neat error messages. It is also needed when logging information to a file.

You should however never ever parse the source code and react to it differently.

[-] Dicska@lemmy.world 24 points 2 months ago

You underestimate the power of us, print debuggers.

[-] hackerwacker@lemmy.ml 17 points 2 months ago

Why wouldn't it? Lots of languages do. In C++ you have __LINE__.

[-] bjorney@lemmy.ca 16 points 2 months ago

The add function in the example above probably traverses the call stack to see what line of the script is currently being executed by the interpreter, then reads in that line in the original script, parses the comment, and subs in the values in the function call.

This functionality exists so when you get a traceback you can see what line of code triggered it in the error message

[-] jaxxed@lemmy.world 8 points 1 month ago

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 10 points 1 month 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.

load more comments (1 replies)
load more comments (15 replies)
[-] MrPoopyButthole@lemmy.world 92 points 2 months ago

That's disgusting

[-] scrubbles@poptalk.scrubbles.tech 85 points 2 months ago

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

Yeah that checks out

[-] raldone01@lemmy.world 27 points 2 months ago* (last edited 1 month ago)

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

[-] scrubbles@poptalk.scrubbles.tech 17 points 2 months ago

Yup, just one of those posts that could of course work in either

[-] arisunz 80 points 2 months ago

I fucking hate this, thanks OP

[-] xavier666@lemm.ee 7 points 2 months ago

How do I delete this part of the python documentation?

[-] RichardoC@lemmy.world 69 points 2 months ago

Thank you, I hate it

[-] would_be_appreciated@lemmy.ml 67 points 2 months ago

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 2 months ago

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 11 points 2 months ago

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

load more comments (4 replies)
[-] Ephera@lemmy.ml 47 points 2 months ago
[-] OsrsNeedsF2P@lemmy.ml 30 points 2 months ago

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

Disgusting.

load more comments (1 replies)
[-] shotgun_crab@lemmy.world 45 points 2 months ago

This is some javascript level shit

[-] ByteOnBikes@slrpnk.net 8 points 1 month 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"

[-] FourPacketsOfPeanuts@lemmy.world 45 points 2 months ago

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

[-] doeknius_gloek@discuss.tchncs.de 44 points 2 months ago

This does not actually work, right? Right?

[-] justcallmelarry@lemmy.dbzer0.com 50 points 2 months ago* (last edited 2 months ago)

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

[-] FourPacketsOfPeanuts@lemmy.world 33 points 2 months ago* (last edited 2 months ago)
[-] tetris11@lemmy.ml 11 points 2 months ago
load more comments (2 replies)
[-] embed_me@programming.dev 41 points 2 months ago

As if I needed more reasons to start away from python

[-] Chais@sh.itjust.works 21 points 2 months ago

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

[-] bjorney@lemmy.ca 15 points 2 months ago
[-] verdigris@lemmy.ml 8 points 2 months ago* (last edited 2 months ago)

Being able to get the line number is very different from comments being parsed.

Edit: didn't realize this was custom code built to be cursed.

[-] bjorney@lemmy.ca 17 points 2 months ago* (last edited 2 months ago)

You should look at how OPs example works first maybe

The python interpreter isn't parsing comments, the add() function is just getting the current line number from the call stack context, and using a regex to spit out the numbers to the right of the "#" on the current executing line of the source code.

load more comments (1 replies)
[-] NigelFrobisher@aussie.zone 40 points 2 months ago

They chose violence.

[-] HStone32@lemmy.world 39 points 2 months ago

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

[-] daniskarma@lemmy.dbzer0.com 35 points 2 months ago

This is heresy.

[-] LolaCat@lemmy.ca 28 points 2 months ago

I feel sick

[-] flashgnash@lemm.ee 21 points 1 month ago
load more comments (3 replies)
[-] Rozauhtuno 20 points 1 month ago

Thanks, I hate it.

[-] yogthos@lemmy.ml 18 points 2 months ago

we need a programming horror community for stuff like this

[-] drathvedro@lemm.ee 13 points 1 month ago

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.

load more comments (1 replies)
[-] davel@lemmy.ml 12 points 2 months ago

What? There is no lib module.

$ python3.13 -c 'import lib'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import lib
ModuleNotFoundError: No module named 'lib'
$
[-] b34k@lemmy.ml 16 points 2 months ago

OP wrote this add() function and has provided their own lib module in the source code.

[-] davel@lemmy.ml 23 points 2 months ago

Oh, so it’s not Python that’s cursed.

One of Python’s design philosophies is—or at least was—“we are all consenting adults here.” If you really want to turn Python into Brainfuck, the interpreter isn’t going to stop you.

load more comments (1 replies)
[-] WILSOOON@programming.dev 10 points 2 months ago
load more comments (2 replies)
[-] lowleveldata@programming.dev 8 points 2 months ago

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

[-] aliser@lemmy.world 8 points 1 month ago

bro what we are devolving

[-] mindbleach@sh.itjust.works 5 points 1 month ago

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

load more comments
view more: next ›
this post was submitted on 21 Nov 2024
326 points (100.0% liked)

Programmer Humor

32931 readers
301 users here now

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

Rules:

founded 5 years ago
MODERATORS