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

Programmer Humor

32733 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/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 204 points 1 month 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

[–] jedibob5@lemmy.world 91 points 1 month ago

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

[–] perviouslyiner@lemmy.world 62 points 1 month ago* (last edited 1 month ago) (1 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?"

[–] bleistift2@sopuli.xyz 17 points 1 month ago (3 children)

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

[–] raldone01@lemmy.world 38 points 1 month 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 1 month ago

You underestimate the power of us, print debuggers.

[–] hackerwacker@lemmy.ml 17 points 1 month ago (1 children)

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

load more comments (1 replies)
[–] bjorney@lemmy.ca 17 points 1 month 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 (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 1 month 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.

load more comments (1 replies)
load more comments (15 replies)
[–] MrPoopyButthole@lemmy.world 91 points 1 month ago

That's disgusting

[–] scrubbles@poptalk.scrubbles.tech 88 points 1 month ago (1 children)

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

Yeah that checks out

[–] raldone01@lemmy.world 24 points 1 month ago* (last edited 1 month ago) (1 children)

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

[–] scrubbles@poptalk.scrubbles.tech 17 points 1 month ago

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

[–] arisunz@lemmy.blahaj.zone 79 points 1 month ago (1 children)

I fucking hate this, thanks OP

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

Thank you, I hate it

[–] would_be_appreciated@lemmy.ml 68 points 1 month 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 24 points 1 month 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 11 points 1 month ago (4 children)

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

load more comments (4 replies)
[–] Ephera@lemmy.ml 50 points 1 month ago (2 children)
[–] OsrsNeedsF2P@lemmy.ml 31 points 1 month ago

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

Disgusting.

load more comments (1 replies)
[–] FourPacketsOfPeanuts@lemmy.world 46 points 1 month ago

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

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

This does not actually work, right? Right?

[–] justcallmelarry@lemmy.dbzer0.com 54 points 1 month ago* (last edited 1 month ago) (1 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

[–] N0x0n@lemmy.ml 11 points 1 month ago* (last edited 1 month ago) (4 children)

Thanks :) ! Could you tell me what use case/purpose such function can have from a dev perspective?

[–] bjorney@lemmy.ca 11 points 1 month ago

This stuff is normally used for creating human readable error messages. E.g. printing the line of your code that actually set off the exception

[–] vort3@lemmy.ml 8 points 1 month ago

I'd say nothing that can't be achieved by docstrings.

[–] justcallmelarry@lemmy.dbzer0.com 8 points 1 month ago* (last edited 1 month ago) (1 children)

This specific use case? To make a meme, mainly ¯\(ツ)

As for the components: Parsing comments have been used for stuff like type hints / formatting / linting, tho generally not at run time (afaik).

The tooling for finding out where something is called from can be used to give a better understanding of where things go wrong when an exception happens or similar, to add to logs.

I would say that in general you don’t need either functionality except for certain edge-usecases

load more comments (1 replies)
load more comments (1 replies)
[–] FourPacketsOfPeanuts@lemmy.world 34 points 1 month ago* (last edited 1 month ago) (1 children)
[–] tetris11@lemmy.ml 12 points 1 month ago
load more comments (2 replies)
[–] shotgun_crab@lemmy.world 43 points 1 month ago (1 children)

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"

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

They chose violence.

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

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

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

This is heresy.

[–] embed_me@programming.dev 33 points 1 month ago (2 children)

As if I needed more reasons to start away from python

[–] Chais@sh.itjust.works 22 points 1 month 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 14 points 1 month ago (3 children)
load more comments (3 replies)
[–] LolaCat@lemmy.ca 30 points 1 month ago

I feel sick

[–] Rozauhtuno@lemmy.blahaj.zone 20 points 1 month ago

Thanks, I hate it.

[–] flashgnash@lemm.ee 19 points 1 month ago (3 children)
load more comments (3 replies)
[–] yogthos@lemmy.ml 16 points 1 month ago

we need a programming horror community for stuff like this

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

load more comments (1 replies)
[–] davel@lemmy.ml 12 points 1 month ago (2 children)

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 1 month ago (1 children)

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

[–] davel@lemmy.ml 24 points 1 month 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 9 points 1 month ago (2 children)
load more comments (2 replies)
[–] aliser@lemmy.world 8 points 1 month ago

bro what we are devolving

[–] lowleveldata@programming.dev 8 points 1 month ago (1 children)

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

load more comments (1 replies)
load more comments
view more: next ›