Python 内置函数之反射操作

import

  • 动态导入模块
    1
    2
    index = __import__('index')
    index.sayHello()

isinstance

  • 判断对象是否是类或者类型元组中任意类元素的实例
    1
    2
    3
    4
    5
    6
    7
    8
    isinstance(1,int)
    True

    isinstance(1,str)
    False

    isinstance(1,(int,str))
    True

issubclass

  • 判断类是否是另外一个类或者类型元组中任意类元素的子类
    1
    2
    3
    4
    5
    6
    7
    8
    issubclass(bool,int)
    True

    issubclass(bool,str)
    False

    issubclass(bool,(str,int))
    True

hasattr

  • 检查对象是否含有属性
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class Student:

    def __init__(self,name):
    self.name = name

    s = Student('Aim')

    hasattr(s,'name') #a含有name属性
    True

    hasattr(s,'age') #a不含有age属性
    False

getattr

  • 获取对象的属性值
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class Student:
    def __init__(self,name):
    self.name = name

    getattr(s,'name') #存在属性name
    'Aim'
    getattr(s,'age',6) #不存在属性age,但提供了默认值,返回默认值
    getattr(s,'age') #不存在属性age,未提供默认值,调用报错
    Traceback (most recent call last):
    File "", line 1, in
    getattr(s,'age')
    AttributeError: 'Stduent' object has no attribute 'age'

setattr

  • 设置对象的属性值
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class Student:
    def __init__(self,name):
    self.name = name

    a = Student('Kim')
    a.name
    'Kim'

    setattr(a,'name','Bob')
    a.name
    'Bob'

delattr

  • 删除对象的属性
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    class A:
    def __init__(self,name):
    self.name = name
    def sayHello(self):
    print('hello',self.name)

    1.测试属性和方法a.name
    '小麦'
    a.sayHello()
    hello 小麦

    2.删除属性delattr(a,'name')
    a.name

    Traceback (most recent call last):
    File "", line 1, in
    a.name
    AttributeError: 'A' object has no attribute 'name'

callable

  • 检测对象是否可被调用
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class B: #定义类B
    def __call__(self):
    print('instances are callable now.')

    callable(B) #类B是可调用对象
    True

    b = B() #调用类B

    callable(b) #实例b是可调用对象
    True

    b() #调用实例b成功
    instances are callable now.