在Python中,反射指的是通过字符串来操作对象的属性,涉及到四个内置函数的使用(Python中一切皆对象,类和对象都可以用下述四个方法)
| class Teacher: |
| def __init__(self,full_name): |
| self.full_name =full_name |
| |
| t=Teacher('Egon Lin') |
| |
| |
| hasattr(t,'full_name') |
| |
| |
| getattr(t,'full_name',None) |
| |
| |
| setattr(t,'age',18) |
| |
| |
| delattr(t,'age') |
基于反射可以十分灵活地操作对象的属性,比如将用户交互的结果反射到具体的功能执行
| >>> class FtpServer: |
| ... def serve_forever(self): |
| ... while True: |
| ... inp=input('input your cmd>>: ').strip() |
| ... cmd,file=inp.split() |
| ... if hasattr(self,cmd): |
| ... func=getattr(self,cmd) |
| ... func(file) |
| ... def get(self,file): |
| ... print('Downloading %s...' %file) |
| ... def put(self,file): |
| ... print('Uploading %s...' %file) |
| ... |
| >>> server=FtpServer() |
| >>> server.serve_forever() |
| input your cmd>>: get a.txt |
| Downloading a.txt... |
| input your cmd>>: put a.txt |
| Uploading a.txt... |
MARKDOWN_HASHd01685de4679355f62d70da65d2fa45fMARKDOWNHASH:
Python的Class机制内置了很多特殊的方法来帮助使用者高度定制自己的类,这些内置方法都是以双下划线开头和结尾的,会在满足某种条件时自动触发,我们以常用的__str__和__del\_为例来简单介绍它们的使用。
__str__方法会在对象被打印时自动触发,print功能打印的就是它的返回值,我们通常基于方法来定制对象的打印信息,该方法必须返回字符串类型
| >>> class People: |
| ... def __init__(self,name,age): |
| ... self.name=name |
| ... self.age=age |
| ... def __str__(self): |
| ... return '<Name:%s Age:%s>' %(self.name,self.age) |
| ... |
| >>> p=People('lili',18) |
| >>> print(p) |
| <Name:lili Age:18> |
__del__会在对象被删除时自动触发。由于Python自带的垃圾回收机制会自动清理Python程序的资源,所以当一个对象只占用应用程序级资源时,完全没必要为对象定制__del__方法,但在产生一个对象的同时涉及到申请系统资源(比如系统打开的文件、网络连接等)的情况下,关于系统资源的回收,Python的垃圾回收机制便派不上用场了,需要我们为对象定制该方法,用来在对象被删除时自动触发回收系统资源的操作
| class MySQL: |
| def __init__(self,ip,port): |
| self.conn=connect(ip,port) |
| def __del__(self): |
| self.conn.close() |
| |
| obj=MySQL('127.0.0.1',3306) |