资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

面向对象-反射-其他内置方法-创新互联

1.反射

1.1反射含义

通过字符串的形式操作对象的相关属性。
方法有hasattr,getattr,delattr

10年积累的网站设计制作、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站策划后付款的网站建设流程,更有高明免费网站建设让你可以放心的选择与我们合作。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class People:
    country='China'

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

    def talk(self):
        print('%s is talking' %self.name)

obj=People('egon',18)

print(obj.name) #obj.__dict__['name']
print(obj.talk)
choice='name'
# 这样直接使用字符串是不可以的,报错信息 AttributeError: 'People' object has no attribute 'choice'
# print(obj.choice) #print(obj.'name')

#但是反射,可以通过字符串操作对象中的属性
print("hasattr(obj,'name')===",hasattr(obj,'name')) #等同obj.name #obj.__dict__['name']
print("hasattr(obj,'talk')===",hasattr(obj,'talk')) #等同#obj.talk
print("hasattr(obj,'talk12')===",hasattr(obj,'talk12'))

print("getattr(obj,'namexxx',None)===",getattr(obj,'namexxx',None))
print("getattr(obj,'talk',None)===",getattr(obj,'talk',None))

setattr(obj,'sex','male') #obj.sex='male'
print("setattr(obj,'sex','male')===",obj.sex)

delattr(obj,'age') #del obj.age
print("delattr(obj,'age')===",obj.__dict__)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
egon
>
hasattr(obj,'name')=== True
hasattr(obj,'talk')=== True
hasattr(obj,'talk12')=== False
getattr(obj,'namexxx',None)=== None
getattr(obj,'talk',None)=== >
setattr(obj,'sex','male')=== male
delattr(obj,'age')=== {'name': 'egon', 'sex': 'male'}

Process finished with exit code 0

1.2反射的应用

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
#反射的应用:

class Service:
    def run(self):
        while True:
            inp=input('>>: ').strip() #cmd='get a.txt'
            cmds=inp.split() #cmds=['get','a.txt']

            # print(cmds)
            if hasattr(self,cmds[0]):
                func=getattr(self,cmds[0])
                func(cmds)

    def get(self,cmds):
        print('get.......',cmds)
    def put(self,cmds):
        print('put.......',cmds)

obj=Service()
obj.run()

print("getattr(obj,'namexxx',None)===",getattr(obj,'namexxx',None))
print("getattr(obj,'talk',None)===",getattr(obj,'talk',None))

setattr(obj,'sex','male') #obj.sex='male'
print("setattr(obj,'sex','male')===",obj.sex)

delattr(obj,'age') #del obj.age
print("delattr(obj,'age')===",obj.__dict__)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
>>: get a.txt
get....... ['get', 'a.txt']

2.其他内置方法

2.1item系列

__getitem__   obj['name']像字典一样调用属性的时候会触发__getitem__执行
__setitem__   obj['age']=22像字典一样设置属性的时候会触发__setitem__执行
__delitem__   del obj['age']像字典一样删除属性的时候会触发__delitem__执行
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Foo: #Dict
    def __init__(self,name):
        self.name=name

    def __getitem__(self, item):
        print('getitem...self.__dict__.get(%s)'%item)
        # 这里不返回,obj['name']是不会获取到值的,因为这样操作就是执行__getitem__方法
        return self.__dict__.get(item)

    def __setitem__(self, key, value):
        print("setitem...self.__dict__[%s]=%s"%(key,value))
        self.__dict__[key]=value

    def __delitem__(self, key):
        print("delitem...del self.__dict__[%s]"%key)
        del self.__dict__[key]

obj=Foo('egon')
# 查看属性
print(obj.__dict__)
# getitem触发时机:obj['name']像字典一样调用属性的时候,会触发
print("obj['name']",obj['name']) #结果egon
print("obj['namexx']",obj['namexx']) # 没有,返回None
print("obj.name",obj.name) # 这样不会触发__getitem__,必须让字典一样操作才会触发

#设置属性
obj.sex='male'  #这样不会触发__setitem__,但也能给对象添加数据属性
print("obj.sex='male'====",obj.__dict__)
print("obj.sex======",obj.sex)
obj["age"]=22
print("obj['age']=22======",obj.__dict__)
print("obj.age======",obj.age) #设置后,可以直接使用对象来获取数据属性

#删除属性

del obj.sex #这样不会触发__del__item
print("del obj.sex====",obj.__dict__)
del obj['age']
print("del obj['age']====",obj.__dict__)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
{'name': 'egon'}
getitem...self.__dict__.get(name)
obj['name'] egon
getitem...self.__dict__.get(namexx)
obj['namexx'] None
obj.name egon
obj.sex='male'==== {'name': 'egon', 'sex': 'male'}
obj.sex====== male
setitem...self.__dict__[age]=22
obj['age']=22====== {'name': 'egon', 'sex': 'male', 'age': 22}
obj.age====== 22
del obj.sex==== {'name': 'egon', 'age': 22}
delitem...del self.__dict__[age]
del obj['age']==== {'name': 'egon'}

Process finished with exit code 0

2.2isinstance和issubclass

"isinstance(obj,cls)检查是否obj是否是类 cls 的对象"

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Foo(object):
    pass
obj = Foo()
print(isinstance(obj, Foo))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
True

Process finished with exit code 0
"issubclass(sub, super)检查sub类是否是 super 类的派生类"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Foo(object):
    pass
class Bar(Foo):
    pass

print(issubclass(Bar, Foo))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
True

Process finished with exit code 0

2.3reprstr

"没有设置这两个方法时"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class School:
    def __init__(self,name,addr,type):
        self.name=name
        self.addr=addr
        self.type=type

s1=School('oldboy1','北京','私立')
print('from repr: ',repr(s1))
print('from str: ',str(s1))
print(s1)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
from repr:  <__main__.School object at 0x000001CC930BEAC8>
from str:  <__main__.School object at 0x000001CC930BEAC8>
<__main__.School object at 0x000001CC930BEAC8>

Process finished with exit code 0
"可以显式调用repr和str方法,
repr和str方法同时存在时,会优先调用str方法,
str方法没有时,会调用repr方法
repr和str方法都是在print(obj)时调用的,没有这两个方法,输出的是对象的内存地址,有了这两个方法,就可以自行设置有意义的输出"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class School:
    def __init__(self,name,addr,type):
        self.name=name
        self.addr=addr
        self.type=type

    def __repr__(self):
        return 'School(%s,%s)' %(self.name,self.addr)
    def __str__(self):
        return '(%s,%s)' %(self.name,self.addr)

s1=School('oldboy1','北京','私立')
# 可以显式调用repr和str方法
print('from repr: ',repr(s1))
print('from str: ',str(s1))
print('from repr: ',s1.__repr__())
print('from str: ',s1.__str__())

print(s1)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
from repr:  School(oldboy1,北京)
from str:  (oldboy1,北京)
from repr:  School(oldboy1,北京)
from str:  (oldboy1,北京)
(oldboy1,北京)

Process finished with exit code 0

2.4del

"程序正常结束后,或回收系统资源时如f.close(),会调用__del__"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Open:
    def __init__(self,filename):
        print('open file.......')
        self.filename=filename

    def __del__(self):
        print('回收操作系统资源:self.close()')
f=Open('settings.py')

print('----main------')
"手动del,会提前执行__del__"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Open:
    def __init__(self,filename):
        print('open file.......')
        self.filename=filename

    def __del__(self):
        print('回收操作系统资源:self.close()')
f=Open('settings.py')
#程序结束的时候,会自动del f 回收f,就会执行__del__
del f  # 如果没有这行代码,会先执行print(--main--),之后回收f,运行__del__,如果手动在这里del,就会先执行__del__,然后执行后面语句
print('----main------')

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
open file.......
回收操作系统资源:self.close()
----main------

Process finished with exit code 0

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:面向对象-反射-其他内置方法-创新互联
标题网址:http://cdkjz.cn/article/edgej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220