Home > other >  Python beginner, ask bosses to create a class in the __str__ () here is what must use STR () functio
Python beginner, ask bosses to create a class in the __str__ () here is what must use STR () functio

Time:01-30

Currently in learning class, see the __str__ is useful in online, then print after the time of this class will automatically call the __str__ below the return, but I don't quite understand why must retrun STR (self) value)? Why can't use Int or float?

The class student:
Def __init__ (self, value, next=0) :
self.value=https://bbs.csdn.net/topics/value
The self. The next=next

Def the __str__ (self) :
Value) return int (self.

CodePudding user response:

Doesn't have to STR, see what you find out, but a broader, STR you generated is not a number, for example, int or float will go wrong

CodePudding user response:

One thousand some of you cannot convert

CodePudding user response:

Python is based on the agreement. Both __init__ and the __str__ or calculation length __len__ is a protocol, a kind of informal interface, need to abide by. If you use the int or float, users in the use of your class, there will be a puzzling error.
  • Related