this post was submitted on 21 Nov 2024
282 points (90.8% liked)

Programmer Humor

32559 readers
628 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/main/lesson_0_comments.ipynb

Image transcription:

from lib import add

# Go ahead and change the comments.
# See how python uses them as arguments.

result = add()  # 1 2
print(result)
result = add()  # 3 4
print(result)
result = add()  # 3 4 5 20
print(result)

Output:

3
7
32
(page 2) 32 comments
sorted by: hot top controversial new old
[–] [email protected] 11 points 2 days ago (1 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 2 days ago (1 children)

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

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

Thank you, I hate it

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

That's disgusting

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

As if I needed more reasons to start away from python

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

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

load more comments (4 replies)
[–] [email protected] 10 points 2 days ago (1 children)
load more comments (1 replies)
[–] [email protected] 48 points 2 days ago (1 children)
[–] [email protected] 25 points 2 days ago

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

Disgusting.

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

I feel sick

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

This does not actually work, right? Right?

[–] [email protected] 32 points 2 days ago* (last edited 2 days ago) (1 children)
[–] [email protected] 11 points 2 days ago
[–] [email protected] 49 points 2 days ago* (last edited 2 days 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] 11 points 2 days ago* (last edited 2 days ago) (4 children)

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

[–] [email protected] 11 points 2 days 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] 7 points 2 days ago* (last edited 2 days 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

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

Thank you ! 😄

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

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

load more comments (1 replies)
load more comments (1 replies)
[–] [email protected] 194 points 2 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] 15 points 2 days 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] 2 points 2 days ago (3 children)

It's quite useful to parse comments and generate documentation from them, either as plain old hypertext or in your editor with LSP.

load more comments (3 replies)
[–] [email protected] 55 points 2 days ago* (last edited 2 days 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] 15 points 2 days ago (3 children)

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

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

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

load more comments (1 replies)
load more comments (2 replies)
[–] [email protected] 85 points 2 days ago

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

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

Makes sence if u think about it. We use comments as docstrings that the interpreter has an understanding of. Python lets u fuck with its internals (at least in an immutable manner) so why not fuck with comments.

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

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

Yeah that checks out

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

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

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

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

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

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

load more comments
view more: ‹ prev next ›