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__函数

相关文章:

  • 2021-09-22
  • 2021-09-01
  • 2021-11-09
  • 2021-09-09
  • 2021-10-19
  • 2022-01-13
  • 2021-07-27
猜你喜欢
  • 2021-10-02
  • 2023-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
相关资源
相似解决方案