As described in the docs:
with respect to examples you give us:
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__
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs