Python: Obtenir le type d’un objet

Author:

classe
{filelink=16878}

import types
 
def dump(object):
    print object,"=>",
 
    if type(object) is types.IntType:
        print "INTEGER",
    if type(object) is types.FloatType:
        print "FLOAT",
    if type(object) is types.StringType:
        print "STRING",
    if type(object) is types.ClassType:
        print "CLASSE",
    if type(object) is types.InstanceType:
        print "INSTANCE",
    print
 
dump(0)
dump(0.0)
dump("0")
 
class A:
    pass
 
class B:
    pass
 
dump(A)
dump(B)
 
a = A()
b = B()
 
dump(a)
dump(b)
 
"""
0 => INTEGER
0.0 => FLOAT
0 => STRING
__main__.A => CLASSE
__main__.B => CLASSE
<__main__.A instance at 1> => INSTANCE
<__main__.B instance at 2> => INSTANCE
"""

Livres Sur ce Sujet

Apprendre à programmer avec Python 3 Apprenez à programmer en Python Python : Petit guide à l’usage du développeur agile Apprendre à programmer avec Python 3 : Avec plus de 50 pages de corigés d’exercices !

Leave a Reply

Your email address will not be published. Required fields are marked *


six × 6 =