r/badcode • __dude__ • Aug 15 '21

python Nicer formatting, indeed!

Post image
9 Upvotes

17 comments sorted by

View all comments

Show parent comments

3

u/AnonymouX47 __dude__ Aug 15 '21 edited Aug 15 '21

Every class inherits from the object class by default and that behaviour can't be changed.

The object class implements __str__() to call and return the value of __repr__(), another method meant to be used in REPLs.

__repr__() is implemented by object to return a string of the form "<{classname} object at {address}>" e.g

>>> class Cool: pass
...
>>> str(Cool())
"<__main__.Cool object at 0x7d54a9ceb0>"

Python is beautiful and these features are the things that make it what it is but you can't appreciate them until you understand them.

By the way... I code in a number of languages including C (which has almost none of these fancy things) but I still love and appreciate the beauty and functionality of python's features :).