2012-07-12 45 views

回答

7

從文檔,Emulating Numeric Types

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. 
+1

謝謝,文檔中的哪裏找到了這個?我確實花了一些時間尋找它。 – Erik 2012-07-12 00:49:51

+0

@erik十六進制()和十進制()和斌()等...作爲鏈接我張貼在 – 2012-07-12 00:53:46

+0

我添加了答案的鏈接。 – 2012-07-12 00:54:14

2

實現__float____int__或任何你想要的......

另外值得一提的是,使用abcnumbers模塊可以爲最終用戶有用...另見:http://www.rafekettler.com/magicmethods.html「類型轉換魔術方法下「