博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
12、反射
阅读量:4551 次
发布时间:2019-06-08

本文共 1074 字,大约阅读时间需要 3 分钟。

 

# class Foo:#     pass## class Bar(Foo):#     pass### obj=Bar()## # print(isinstance(obj,Bar))# # print(isinstance([],list))## print(issubclass(Bar,Foo))# 反射:指的是通过字符串来操作属性class Foo:    def __init__(self,name):        self.name=nameobj=Foo('egon')# hasattr()# print(hasattr(obj,'name')) #'name' in obj.__dict__# getattr()# print(getattr(obj,'name')) #obj.__dict__['name']# print(getattr(obj,'age')) #obj.__dict__['age']# print(getattr(obj,'age',None)) #obj.__dict__['age']# setattr()# setattr(obj,'age',18) #obj.age=18# setattr(obj,'name','EGON') #obj.name='EGON'# print(obj.__dict__)# delattr()# delattr(obj,'name')# del obj.name# print(obj.__dict__)class Ftp:    def get(self):        print('get')    def put(self):        print('put')    def login(self):        print('login')    def run(self):        while True:            choice=input('>>>: ').strip()            if hasattr(self,choice):                method=getattr(self,choice)                method()            else:                print('命令不存在')obj=Ftp()obj.run()

 

转载于:https://www.cnblogs.com/z-x-y/p/10047589.html

你可能感兴趣的文章
Scrapy的学习和使用
查看>>
7.内部类(一)之详解内部类
查看>>
1.messager消息提示框
查看>>
[PY]进制转换
查看>>
STL系列 list
查看>>
匿名方法和Lambda表达式
查看>>
京东的核心业务
查看>>
读书笔记(六)--成交
查看>>
Secret Number hdu 2113
查看>>
软件架构(体系结构,Architecture)和软件框架
查看>>
阶梯博弈(没怎么搞懂)
查看>>
python request post请求body中有json数组
查看>>
IDT hook KiTrap03
查看>>
字节对齐
查看>>
使用Python SocketServer快速实现多线程网络服务器
查看>>
离散数学
查看>>
外观模式理解和示例
查看>>
IDEA远程仓库版本回滚
查看>>
C++矩阵库 Eigen 简介(转载)
查看>>
sklearn的train_test_split()各函数参数含义解释(非常全)
查看>>