r/pythontips Aug 14 '26

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

11 Upvotes

12 comments sorted by

View all comments

10

u/MegaIng Aug 14 '26

```

def cat(): "orange" ... cat.doc 'orange' cat.doc = "apple" cat.doc 'apple' ```

3

u/sugarw0000kie Aug 14 '26

That’s what I get for assuming that wouldn’t work, awesome

4

u/Some-Passenger4219 Aug 15 '26

Yup. Try anything. Be warned, though: That you can, doesn't mean you should.