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

Programmer Humor

32730 readers
365 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] 202 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

[–] [email protected] 90 points 1 month ago

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

[–] [email protected] 59 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?"

[–] [email protected] 17 points 1 month ago (3 children)

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

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

[–] [email protected] 24 points 1 month ago

You underestimate the power of us, print debuggers.

[–] [email protected] 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)
[–] [email protected] 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

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

[–] [email protected] 8 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)
[–] [email protected] 91 points 1 month ago

That's disgusting

[–] [email protected] 88 points 1 month ago (1 children)

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

Yeah that checks out

[–] [email protected] 24 points 1 month ago* (last edited 1 month ago) (1 children)

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

[–] [email protected] 17 points 1 month ago

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

[–] [email protected] 79 points 1 month ago (1 children)

I fucking hate this, thanks OP

load more comments (1 replies)
[–] [email protected] 68 points 1 month ago

Thank you, I hate it

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

[–] [email protected] 25 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

[–] [email protected] 12 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)
[–] [email protected] 49 points 1 month ago (2 children)
[–] [email protected] 29 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)
[–] [email protected] 46 points 1 month ago

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

[–] [email protected] 45 points 1 month ago (4 children)

This does not actually work, right? Right?

[–] [email protected] 53 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

[–] [email protected] 10 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?

[–] [email protected] 10 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

[–] [email protected] 8 points 1 month ago

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

[–] [email protected] 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)
[–] [email protected] 32 points 1 month ago* (last edited 1 month ago) (1 children)
[–] [email protected] 11 points 1 month ago
load more comments (2 replies)
[–] [email protected] 41 points 1 month ago (1 children)

This is some javascript level shit

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

[–] [email protected] 39 points 1 month ago

They chose violence.

[–] [email protected] 37 points 1 month ago

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

[–] [email protected] 34 points 1 month ago

This is heresy.

[–] [email protected] 33 points 1 month ago (2 children)

As if I needed more reasons to start away from python

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

[–] [email protected] 14 points 1 month ago (3 children)
load more comments (3 replies)
[–] [email protected] 29 points 1 month ago

I feel sick

[–] [email protected] 19 points 1 month ago (3 children)
load more comments (3 replies)
[–] [email protected] 18 points 1 month ago

Thanks, I hate it.

[–] [email protected] 17 points 1 month ago

we need a programming horror community for stuff like this

[–] [email protected] 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)
[–] [email protected] 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'
$
[–] [email protected] 16 points 1 month ago (1 children)

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

[–] [email protected] 23 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)
[–] [email protected] 9 points 1 month ago (2 children)
load more comments (2 replies)
[–] [email protected] 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)
[–] [email protected] 8 points 1 month ago

bro what we are devolving

load more comments
view more: next ›