Python核心編程——第四章Python對象

來源:互聯網
上載者:User

三種方法查看數字資料的類型:

[1] isinstance 可讀性更好


#! /usr/bin/env python<br />'''typechk.py'''<br />def displayNumType(num):<br /> print num, 'is',<br /> if isinstance(num, (int, long, float, complex)):<br /> print 'a number of type:', type(num).__name__<br /> else:<br /> print 'not a number at all!'<br />displayNumType(-69)<br />displayNumType(99999999999999999999L)<br />displayNumType(99999999999999999999)<br />displayNumType(98.6)<br />displayNumType(-5.2+1.9j)<br />displayNumType('aaa')<br />#######<br /># output<br />#######<br />-69 is a number of type: int<br />99999999999999999999 is a number of type: long<br />99999999999999999999 is a number of type: long<br />98.6 is a number of type: float<br />(-5.2+1.9j) is a number of type: complex<br />aaa is not a number at all!<br />

[2] 調用兩次type

#! /usr/bin/env<br />python<br />'''typechk.py'''<br />def displayNumType(num):<br /> print num, 'is',<br /> if type(num)==type(0):<br /> print 'an integer'<br /> elif type(num)==type(0L):<br /> print 'a long'<br /> elif type(num)==type(0.0):<br /> print 'a float'<br /> elif type(num)==type(0+0j):<br /> print 'a complex number'<br /> else:<br /> print 'not a number at all!'<br />displayNumType(-69)<br />displayNumType(99999999999999999999L)<br />displayNumType(99999999999999999999)<br />displayNumType(98.6)<br />displayNumType(-5.2+1.9j)<br />displayNumType('aaa')<br />#######<br /># output<br />#######<br />-69 is an integer<br />99999999999999999999 is a long<br />99999999999999999999 is a long<br />98.6 is a float<br />(-5.2+1.9j) is a complex number<br />aaa is not a number at all!<br />

[3] import types

#! /usr/bin/env<br />python<br />import types<br />'''typechk.py'''<br />def displayNumType(num):<br /> print num, 'is',<br /> if type(num)==types.IntType:<br /> print 'an integer'<br /> elif type(num)==types.LongType:<br /> print 'a long'<br /> elif type(num)==types.FloatType:<br /> print 'a float'<br /> elif type(num)==types.ComplexType:<br /> print 'a complex number'<br /> else:<br /> print 'not a number at all!',</p><p> print 'but, it is',<br /> if type(num)==types.StringType:<br /> print 'a string'</p><p>displayNumType(-69)<br />displayNumType(99999999999999999999L)<br />displayNumType(99999999999999999999)<br />displayNumType(98.6)<br />displayNumType(-5.2+1.9j)<br />displayNumType('aaa')<br />#######<br /># output<br />#######<br />-69 is an integer<br />99999999999999999999 is a long<br />99999999999999999999 is a long<br />98.6 is a float<br />(-5.2+1.9j) is a complex number<br />aaa is not a number at all! but, it is a string<br />


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.