有人知道Python如何在內部管理int和long類型嗎?它會動態選擇合適的類型嗎?一個整數的限制是多少?我正在使用Python 2.6,與以前的版本有所不同嗎?我應該如何理解以下代碼?>>> print type(65535)<type 'int'>>>> print type(65536*65536)<type 'long'>更新:>>> print type(0x7fffffff)<type 'int'>>>> print type(0x80000000)<type 'long'>
3 回答

幕布斯6054654
TA貢獻1876條經驗 獲得超7個贊
在我的機器上:
>>> print type(1<<30)
<type 'int'>
>>> print type(1<<31)
<type 'long'>
>>> print type(0x7FFFFFFF)
<type 'int'>
>>> print type(0x7FFFFFFF+1)
<type 'long'>
Python使用整數(32位有符號整數,我不知道它們是否是C整數)適合適合32位的值,但是對于任何東西,它都會自動切換為長整數(任意大的位數,即bignums)更大。我猜想這將加快速度以實現較小的值,同時通過無縫過渡到bignum避免任何溢出。
添加回答
舉報
0/150
提交
取消