As described in
the docs:
Quote:A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.
with respect to examples you give us:
class Spam:
pass
class Eggs:
def __str__(self):
return 'instance of my class Eggs'
spam = Spam() # instance of class Spam
eggs = Eggs() # instance of class Eggs
print(spam)
print(eggs)
Output:
<__main__.Spam object at 0x7fd8d323c978>
instance of my class Eggs
Do you see the difference? Note that with respect to printing, there is also related
__repr__()
methood. You can check
what is the difference between __str__ and __repr__