class F:
def __str__(self):
return 'hello china'
def __int__(self):
return 123
res = F()
print(res) #结果是hello china
print(int(res)) #结果是123
创建的对象时,会自动去寻找类中的__str__函数,
当我们用int转换类创建的对象时,会自动去寻找类中的__int__函数
相关文章:
- python中__str__和__repr__的区别 2023-11-08
- Python之__str__类的特殊方法 2023-11-08
- Python中__repr__和__str__区别 2023-11-08
- Python:__str__() 和 __repr() 2023-11-08
- 浅谈python中__str__和__repr__的区别 2023-11-08
- python中__str__()函数的用法 2023-11-08