this post was submitted on 21 Nov 2024
277 points (90.6% liked)

Programmer Humor

32561 readers
352 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
[–] [email protected] 18 points 2 days ago

Why is this a thing

[–] [email protected] 15 points 2 days ago

Thanks, I hate it.

[–] [email protected] 8 points 2 days ago

bro what we are devolving

[–] [email protected] 13 points 2 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.

[–] [email protected] 3 points 1 day 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.

[–] [email protected] 5 points 1 day ago

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

[–] [email protected] 33 points 2 days ago

This is heresy.

[–] [email protected] 37 points 2 days ago

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

[–] [email protected] 4 points 1 day ago

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

[–] [email protected] 196 points 3 days ago (17 children)

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

[–] [email protected] 7 points 2 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.

[–] [email protected] 8 points 1 day 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.

[–] [email protected] 1 points 1 hour ago

Conveniently Python keeps the comments around. 😄

[–] [email protected] 86 points 3 days ago

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

[–] [email protected] 55 points 3 days ago* (last edited 3 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)
[–] [email protected] 4 points 2 days 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__.

[–] [email protected] 1 points 2 days ago* (last edited 2 days ago) (1 children)

Is __doc__ storing a comment or just a text string?

[–] [email protected] 2 points 2 days 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.

load more comments (13 replies)
[–] [email protected] 17 points 2 days ago

we need a programming horror community for stuff like this

[–] [email protected] 65 points 3 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.

[–] [email protected] 24 points 2 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

[–] [email protected] 11 points 2 days ago (1 children)

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

[–] [email protected] 4 points 1 day 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.

[–] [email protected] 3 points 1 day ago* (last edited 1 day 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.

[–] [email protected] 1 points 1 hour ago* (last edited 1 hour ago)

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.

[–] [email protected] 39 points 3 days ago

They chose violence.

[–] [email protected] 76 points 3 days ago (1 children)

I fucking hate this, thanks OP

load more comments (1 replies)
[–] [email protected] 89 points 3 days ago

That's disgusting

[–] [email protected] 84 points 4 days ago (2 children)

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

Yeah that checks out

load more comments (2 replies)
[–] [email protected] 66 points 3 days ago

Thank you, I hate it

[–] [email protected] 41 points 3 days ago (1 children)

This is some javascript level shit

[–] [email protected] 7 points 2 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"

[–] [email protected] 49 points 3 days ago (2 children)
[–] [email protected] 1 points 57 minutes ago* (last edited 43 minutes ago)
[–] [email protected] 27 points 3 days ago

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

Disgusting.

[–] [email protected] 45 points 3 days ago (4 children)

This does not actually work, right? Right?

[–] [email protected] 52 points 3 days ago* (last edited 3 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)
[–] [email protected] 1 points 1 day 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

[–] [email protected] 31 points 3 days ago* (last edited 3 days ago) (1 children)
load more comments (1 replies)
load more comments (1 replies)
[–] [email protected] 45 points 4 days ago

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

[–] [email protected] 33 points 3 days ago (5 children)

As if I needed more reasons to start away from python

load more comments (5 replies)
[–] [email protected] 29 points 3 days ago

I feel sick

load more comments
view more: next ›