day28 反射、内置方法和元类(了解)

1. 反射

1.1 什么是反射

指的是在程序运行过程中可以"动态(不见棺材不掉泪)"获取对象的信息

  • 静态:在定义阶段就确定类型
  • 动态:在调用阶段才去确定类型

1.2 为何要用反射?

python是动态语言,而反射(reflection)机制被视为动态语言的关键。

#python是动态语言,只到使用值的时候才能发现值的类型
def func(obj):
    #判断obj下有没有x属性
    if 'x' not in obj.__dict__:
        return
    obj.x

func(10)       
# AttributeError: 'int' object has no attribute '__dict__'

1.3 实现反射机制的步骤

class People:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def say(self):
        print('<%s:%s>' %(self.name,self.age))

obj=People('辣白菜同学',18)

1、先通过多dir:查看出某一个对象下可以.出哪些属性来

print(dir(obj))
#['__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__', 'age', 'name', 'say']

2、可以通过字符串反射到真正的属性上,得到属性值

print(obj.__dict__[dir(obj)[-2]])#'name'
#辣白菜同学

1.4 四个内置函数:通过字符串来操作属性值

1、hasattr() --》判断是否存在相应的属性值

print(hasattr(obj,'name'))
print(hasattr(obj,'x'))

2、getattr() --》获取相应的属性值,拿不到则报错,可以有默认值,

print(getattr(obj,'name'))

3、setattr() --》将相应的属性的值修改为新值(新增)

setattr(obj,'vcvb','EGON') # obj.name='EGON'
print(obj.vcvb)

4、delattr() --》删除相应的属性值

delattr(obj,'name') # del obj.name
print(obj.__dict__)
#直接获取对象的属性值,Python中一切皆对象
res1=getattr(obj,'say') # obj.say
res2=getattr(People,'say') # People.say
print(res1)#绑定方法
print(res2)#函数
#<bound method People.say of <__main__.People object at 0x000001C74459B910>>
#<function People.say at 0x000001C74461A4C0>

1.5 应用案例

#1.

obj=10
if hasattr(obj,'x'):
    print(getattr(10,'x'))
else:
    pass

print(getattr(obj,'x',None))

#2.
class Ftp:
    def put(self):
        print('正在执行上传功能')

    def get(self):
        print('正在执行下载功能')

    def interactive(self):
        method=input(">>>: ").strip() # method='put'

        if hasattr(self,method):
            getattr(self,method)()
        else:
            print('输入的指令不存在')

obj=Ftp()
obj.interactive()

2. 内置方法

2.1 什么是内置方法

定义在类内部,以__开头并以__结果的方法

特点:会在某种情况下自动触发执行

2.2 为何要用内置方法

用内置方法可以实现定制我们想要的类或者是对象

2.3 如何用内置方法

2.3.1 __str__()

在打印对象时会自动触发,然后将返回值(必须是字符串类型)当做本次打印的结果输出

class People:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        # print('运行了...')
        return "<%s:%s>" %(self.name,self.age)


obj = People('辣白菜同学', 18)

# print(obj.__str__())
print(obj)  # <'辣白菜同学':18>

# obj1=int(10)
# print(obj1)

2.3.2 __del__()

在清理对象时触发,会先执行该方法

class People:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.x = open('a.txt',mode='w')
        # self.x = 占据的是操作系统资源

    def __del__(self):
        # print('run...')
        # 发起系统调用,告诉操作系统回收相关的系统资源
        self.x.close()

obj = People('辣白菜同学', 18)
# del obj # obj.__del__()
print('============>')

3. 元类(了解)

3.1 引入

一切都源自于一句话:一切皆为对象

3.2 什么是元类

元类就是用来实例化产生类的类

关系:元类---实例化---->类(People)---实例化---->对象(obj)

class People:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def say(self):
        print('%s:%s' %(self.name,self.name))


print(People.__dict__)

# 如何得到对象
# obj=调用类()
obj=People('egon',18)
print(type(obj))

# 如果说类也是对象
# People=调用类(。。。)

# 查看内置的元类:
# 1、type是内置的元类
# 2、我们用class关键字定义的所有的类以及内置的类都是由元类type实例化产生
print(type(People))
print(type(int))

3.3 class关键字创造类People的步骤

类有三大特征:

1、类名

 class_name="People"

2、类的基类

 class_bases=(object,)

3、执行类体代码拿到类的名称空间

# class_dic={}
# class_body="""
# def __init__(self,name,age):
# self.name=name
# self.age=age
# def say(self):
# print('%s:%s' %(self.name,self.name))
# """
# exec(class_body,{},class_dic)
# # print(class_dic)

4、调用元类

People=type(class_name,class_bases,class_dic)

3.4 如何自定义元类来控制类的产生

class Mymeta(type): # 只有继承了type类的类才是元类
    #            空对象,"People",(),{...}
    def __init__(self,x,y,z):
        print('run22222222222....')
        print(self)
        # print(x)
        # print(y)
        # print(z)
        # print(y)
        # if not x.istitle():
        #     raise NameError('类名的首字母必须大写啊!!!')

    #          当前所在的类,调用类时所传入的参数
    def __new__(cls, *args, **kwargs):
        # 造Mymeta的对象
        print('run1111111111.....')
        # print(cls,args,kwargs)
        # return super().__new__(cls,*args, **kwargs)
        return type.__new__(cls,*args, **kwargs)
# People=Mymeta("People",(object,),{...})
# 调用Mymeta发生三件事,调用Mymeta就是type.__call__
# 1、先造一个空对象=>People,调用Mymeta类内的__new__方法
# 2、调用Mymeta这个类内的__init__方法,完成初始化对象的操作
# 3、返回初始化好的对象
class People(metaclass=Mymeta):
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def say(self):
        print('%s:%s' %(self.name,self.name))

强调:

# # 只要是调用类,那么会一次调用
# # 1、类内的__new__
# # 2、类内的__init__

3.5 __call__

class Foo:
    def __init__(self,x,y):
        self.x=x
        self.y=y

    #            obj,1,2,3,a=4,b=5,c=6
    def __call__(self,*args,**kwargs):
        print('===>',args,kwargs)
        return 123

obj=Foo(111,222)
# print(obj) # obj.__str__
res=obj(1,2,3,a=4,b=5,c=6) # res=obj.__call__()
print(res)
# 应用:如果想让一个对象可以加括号调用,需要在该对象的类中添加一个方法__call__
# 总结:
# 对象()->类内的__call__
# 类()->自定义元类内的__call__
# 自定义元类()->内置元类__call__

3.6 自定义元类控制类的调用 ==> 类的对象的产生

class Mymeta(type): # 只有继承了type类的类才是元类
    def __call__(self, *args, **kwargs):
        # 1、Mymeta.__call__函数内会先调用People内的__new__
        people_obj=self.__new__(self)
        # 2、Mymeta.__call__函数内会调用People内的__init__
        self.__init__(people_obj,*args, **kwargs)

        # print('people对象的属性:',people_obj.__dict__)
        people_obj.__dict__['xxxxx']=11111
        # 3、Mymeta.__call__函数内会返回一个初始化好的对象
        return people_obj

3.6.1 类的产生

# People=Mymeta()=》type.__call__=>干了3件事
# 1、type.__call__函数内会先调用Mymeta内的__new__
# 2、type.__call__函数内会调用Mymeta内的__init__
# 3、type.__call__函数内会返回一个初始化好的对象
class People(metaclass=Mymeta):
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def say(self):
        print('%s:%s' %(self.name,self.name))

    def __new__(cls, *args, **kwargs):
        # 产生真正的对象
        return object.__new__(cls)

3.6.2 类的调用

# obj=People('egon',18) =》Mymeta.__call__=》干了3件事
# 1、Mymeta.__call__函数内会先调用People内的__new__
# 2、Mymeta.__call__函数内会调用People内的__init__
# 3、Mymeta.__call__函数内会返回一个初始化好的对象
obj1=People('egon',18)
obj2=People('egon',18)
# print(obj)
print(obj1.__dict__)
print(obj2.__dict__)

3.7 属性查找

3.7.1 属性查找的原则:对象 ==> 类 ==> 父类

切记:父类 不是 元类

3.7.2 例子

class Mymeta(type):
    n=444

    def __call__(self, *args, **kwargs): #self=<class '__main__.StanfordTeacher'>
        obj=self.__new__(self) # StanfordTeacher.__new__
        # obj=object.__new__(self)
        print(self.__new__ is object.__new__) #True
        self.__init__(obj,*args,**kwargs)
        return obj

class Bar(object):
    # n=333

    # def __new__(cls, *args, **kwargs):
    #     print('Bar.__new__')
    pass

class Foo(Bar):
    # n=222

    # def __new__(cls, *args, **kwargs):
    #     print('Foo.__new__')
    pass

class StanfordTeacher(Foo,metaclass=Mymeta):
    # n=111

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


obj=StanfordTeacher('lili',18)
print(obj.__dict__)
# print(obj.n)
# print(StanfordTeacher.n)

猜你喜欢

转载自www.cnblogs.com/Henry121/p/12708687.html