如何使自定义Number类可转换为int&float?

Eri*_*rik 3 python numbers python-3.x

我在python 3中实现了一个数字类.我需要做什么

float(mynumber)
Run Code Online (Sandbox Code Playgroud)

工作正常吗?

Ned*_*der 7

从文档中,模拟数字类型:

object.__complex__(self) 
object.__int__(self) 
object.__long__(self) 
object.__float__(self) 

Called to implement the built-in functions complex(), int(), long(), and float(). 
Should return a value of the appropriate type.
Run Code Online (Sandbox Code Playgroud)