获取对象信息:
当我们拿到一个对象的引用时,如何知道这个对象是什么类型,有哪些方法呢?
使用type()
首先,我们来判断对象类型,使用type()函数:
基本类型都可以用type()判断:
print type(123)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
<type 'int'>
print type('str')
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
<type 'str'>
def fun1(a):
print a
fun1('xxx')
print type(fun1)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
xxx
<type 'function'>
from mycompany.web.Tortoise import *
c=Tortoise();
print type(c)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
xxxx
<class 'mycompany.web.Tortoise.Tortoise'>
使用isinstance():
对于class的继承关系来说,type()就很不方便,我们要判断class的类型,可以使用isinstance()函数。
我们回顾上次的例子,如果继承关系是:
object -> Animal -> Dog -> Husky
那么,isinstance() 就可以告诉我们,一个对象是否是某种类型,先创建3种类型的对象:
from mycompany.web.Dog import *
a = Animal()
a.run()
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
Animal isxx running...
----------------------------------------------------------------------------
from mycompany.web.Dog import *
a = Animal()
a.run()
print isinstance(a, Animal)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
Animal isxx running...
True
from mycompany.web.Dog import *
a = Dog()
a.run()
print '11111111111111'
print isinstance(a, Dog)
print '22222222222222'
print isinstance(a, Animal)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
Dog isxx running...
11111111111111
True
22222222222222
True
from mycompany.web.Dog import *
a = Dog()
a.run()
print '11111111111111'
print isinstance(a, Dog)
print '22222222222222'
print isinstance(a, Animal)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
Dog isxx running...
11111111111111
True
22222222222222
True
-----------------------------------------------------------------------
from mycompany.web.Husky import *
a = Husky()
a.run()
print '11111111111111'
print isinstance(a, Husky)
print '22222222222222'
print isinstance(a, Animal)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
00000000
Dog isxx running...
11111111111111
True
22222222222222
True
from mycompany.web.Husky import *
h = Husky()
h.run()
print isinstance(h, Husky)
print "1111111111111111"
print isinstance(h, Dog)
print "2222222222222222"
print isinstance(h, Animal)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
00000000
Dog isxx running...
True
1111111111111111
True
2222222222222222
True
使用dir():
from mycompany.web.Husky import *
h = Husky()
h.run()
print isinstance(h, Husky)
print "1111111111111111"
print isinstance(h, Dog)
print "2222222222222222"
print isinstance(h, Animal)
print "3333333333333333"
print dir('h')
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a1.py
00000000
Dog isxx running...
True
1111111111111111
True
2222222222222222
True
3333333333333333
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Process finished with exit code 0