获取对象信息

    #当我们调用方法时可能需要传递一个参数,这个参数类型我们知道,但是对于接收参数的方法,就不一定知道是什么参数类型了。我们该如何得知参数类型呢?
        #Python为我们提供了以下3种获取对象类型的方法。
1、使用type()函数
        #前面已经学过type()函数的使用,基本类型都可以用type()判断,例如:
1 >>> type(123)
2 <class 'int'>
3 >>> type('abc')
4 <class 'str'>
5 >>> type(None)
6 <class 'NoneType'>
   #如果一个变量指向函数或类,用type()函数返回的是什么类型,例如:
 1 #!/usr/bin/python
 2 #-*-coding:UTF-8-*-
 3 #获取对象信息
 4 
 5 class Animal(object):
 6     def run(self):
 7         print('Animal is running.')
 8 
 9 class Dog(Animal):
10     def run(slef):
11         print('Dog is running.')
12         
13 dog=Dog
14 print(type(abs))
15 print(type(dog()))
  #执行结果如下:
1 D:Pythonworkspacedatatime20171211>python 获取对象信息.py
2 <class 'builtin_function_or_method'>
3 <class '__main__.Dog'>
   #由输出结果看到,返回的是对应的Class的类型。
   #如果我们要在if语句中判断并比较两个变量的type类型是否相同,应如下操作:
 1 >>> type(123)==type(456)
 2 True
 3 >>> type(123)==int
 4 True
 5 >>> type('abc')==type('123')
 6 True
 7 >>> type('abc')==str
 8 True
 9 >>> type('abc')==type(123)
10 False
   #通过操作我们看到,判断基本数据类型可以直接写int、str等。怎么判断一个对象是否是函数呢?
        #可以使用types模块中定义的常量,例如:
 1 #!/usr/bin/python
 2 #-*-coding:UTF-8-*-
 3 #获取对象信息
 4 
 5 import types 
 6 def func():
 7     pass
 8 fn=func
 9 print(type(fn)==types.FunctionType)
10 print(type(abs)==types.BuiltinFunctionType)
11 print(type(lambda x:x)==types.LambdaType)
12 print(type(x for x in range(10))==types.GeneratorType)
  #执行结果如下:
1 D:Pythonworkspacedatatime20171211>python 获取对象信息_1.py
2 True
3 True
4 True
5 True
  #由执行结果看到,函数的判断方式需要借助types模块的帮助。
2、使用isinstance()函数
        #要明确class的继承关系,使用type()很不方便,通过判断class的数据类型确定class的继承关系要方便得多,这个时候可以使用isinstance()函数。
        #例如,继承关系是如下形式:
1 object->Animal->Dog
  #即Animal继承object、Dog继承Animal。使用isinstance()可以告诉我们一个对象是否是某种类型。
        #例如,创建如下两种类型的对象:

 1 #!/usr/bin/python3
 2 #-*-coding:UTF-8-*-
 3 #isinstance()
 4 
 5 class Animal(object):
 6     def run(self):
 7         print('Animal is running.')
 8 
 9 class Dog(Animal):
10     def run(self):
11         print('Dog is running.')
12 
13 animal=Animal()
14 dog=Dog()
#对上面两种类型的对象,使用isinstance进行判断:
1 print(isinstance(dog,Dog))
      #执行结果如下:
1 D:Pythonworkspacedatatime20171212>python isinstance().py
2 True
  #根据输出结果看到,dog是Dog类型,这个没有任何疑问,因为dog变量指向的就是Dog对象。接下来判断Animal类型,使用isinstance判断如下:
1 print(isinstance(dog,Animal))
  #执行结果如下:
1 D:Pythonworkspacedatatime20171212>python isinstance().py
2 True
  #根据输出结果看到,dog也是Animal类型。由此我们得知:尽管dog是Dog类型,不过由于Dog是从Animal继承下来的,因为Animal类型。可以这么说,isinstance()判断的是一个对象是否为该类型本身,或者是否为该类型继承类的类型。
        #我们可以确信,dog还是object类型:
1 print(isinstance(dog,object))
   #执行结果如下:
1 D:Pythonworkspacedatatime20171212>python isinstance().py
2 True
#同时确信,实际类型是Dog类型的dog,同时也是Animal类型:
1 print(isinstance(dog,Dog) and isinstance(dog,Animal))
   #执行结果如下:
1 D:Pythonworkspacedatatime20171212>python isinstance().py
2 True
#不过Animal不是Dog类型,这个我们前面已经了解了:
1 print(isinstance(Dog,Animal))
#执行结果如下:
1 D:Pythonworkspacedatatime20171212>python isinstance().py
2 False
   #能用type()判断的基本类型也可以用isinstance()判断。这个可以自己进行验证。
  #isinstance()可以判断一个变量是否为某些类型中的一种,判断变量是否为list或tuple的方法如下:
1 print(isinstance([1,2,3],(list,tuple)))
2 print(isinstance((1,2,3),(list,tuple)))
#执行结果如下:
1 D:Pythonworkspacedatatime20171212>python isinstance().py
2 True
3 True
3、使用dir()
        #如果要获取一个对象的所有属性和方法,就可以使用die()函数。dir()函数返回一个字符串的list。例如,获取一个对象的所有属性和方法的方式如下:
1 print(dir(dog))
#执行结果如下:
1 ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'run']
        #由输出结果看到,str对象包含许多属性和方法。

原文地址:https://www.cnblogs.com/DLHe/p/8035244.html