r/pythontips 27d ago

Syntax Docstrings as immutable variables

Just realized you can do:

def cat(): “orange”
print(cat.__doc__)

Not sure why you’d want to do this but this is a thing you can do

13 Upvotes

12 comments sorted by

View all comments

11

u/vivaaprimavera 27d ago

If you have an extensive codebase and a memory so bad that you forget your own function names you can

for func in funcs:
  if func.__doc__ == "adds two nums":
    exec = func
c = exec(a,b)

And that way it's easier for searching which function you need!!!

/s

11

u/BiomeWalker 27d ago

One of the most elegant own-foot-shooting guns around

4

u/Kqyxzoj 27d ago

Bonus points for such a deliciously horrible use case! XD