13.15. OOP Stringify About

  • str()

  • repr()

13.15.1. SetUp

>>> import datetime
>>>
>>> date = datetime.date(1969, 7, 21)

13.15.2. Str

>>> str(date)
'1969-07-21'
>>> print(date)
1969-07-21

13.15.3. Repr

>>> repr(date)
'datetime.date(1969, 7, 21)'
>>> date
datetime.date(1969, 7, 21)

13.15.4. Memory Address

>>> class User:
...     pass
...
>>> mark = User()

Printing user will display memory address of an object:

>>> print(mark)  
<__main__.User object at 0x1064f0f90>

CPython implementation of id() builtin function will return the same memory address of an object, but in decimal form. You can convert this to the hexadecimal form using hex() function.

>>> id(mark)  
4400811920
>>>
>>> hex(id(mark))  
'0x1064f0f90'