Python - 面向对象编程 - 反射 hasattr、getattr、setattr、delattr (下)

简介: Python - 面向对象编程 - 反射 hasattr、getattr、setattr、delattr (下)

反射其他模块的成员


image.png


输出结果

True

反射22222

小菠萝

fanshe 是另一个模块

 

反射的应用一


需求

  • 打开浏览器,访问一个网站
  • 单击登录就跳转到登录界面
  • 单击注册就跳转到注册界面
  • 单击的其实是一个个的链接,每一个链接都会有一个函数或者方法来处理

 

未使用反射前

class Web:
    def login(self):
        print('欢迎来到登录页面')
    def register(self):
        print('欢迎来到注册页面')
    def save(self):
        print('欢迎来到存储页面')
while True:
    obj = Web()
    choose = input(">>>").strip()
    if choose == 'login':
        obj.login()
    elif choose == 'register':
        obj.register()
    elif choose == 'save':
        obj.save()


使用反射后

class Web:
    def login(self):
        print('欢迎来到登录页面')
    def register(self):
        print('欢迎来到注册页面')
    def save(self):
        print('欢迎来到存储页面')
while True:
    obj = Web()
    choose = input(">>>").strip()
    # 判断对象是否有对应的方法
    if hasattr(obj, choose):
        # 获取对应的方法
        f = getattr(obj, choose)
        # 执行方法
        f()


反射的应用二


在做接口自动化测试的时候,我们一般都会封装 BaseRequest 类来进行复用,类里面会封装不同请求方法

 

未使用反射前

class BaseRequest:
    req = requests.Session()
    def get(self, url):
        resp = self.req.get(url)
        print("==get==")
        return resp
    def post(self, url):
        resp = self.req.post(url)
        print("==post==")
        return resp
    def put(self, url):
        resp = self.req.put(url)
        print("==put==")
        return resp
    # 不使用反射的方法
    def main(self, method, url):
        if method == "get":
            self.get(url)
        elif method == "post":
            self.post(url)
        elif method == "put":
            self.put(url)


使用反射后

    # 使用反射的方法
    def main_attr(self, method, url):
        if hasattr(self, method):
            func = getattr(self, method)
            func(url)


执行代码

request = BaseRequest()
# 不使用反射
request.main("get", "http://www.baidu.com")
request.main("post", "http://www.baidu.com")
request.main("put", "http://www.baidu.com")
# 使用反射
request.main_attr("get", "http://www.baidu.com")
request.main_attr("post", "http://www.baidu.com")
request.main_attr("put", "http://www.baidu.com")
# 输出结果
==get==
==post==
==put==
==get==
==post==
==put==


总结


当封装了多个方法,然后需要根据不同条件去调用不同方法的时候,就可以考虑使用反射了,代码量少不是丁点半点

相关文章
|
4天前
|
Python
python基础篇:面向对象编程
python基础篇:面向对象编程
21 0
|
4天前
|
Python
【Python进阶(三)】——面向对象编程
【Python进阶(三)】——面向对象编程
|
4天前
|
Python
python反射
python反射
|
4天前
|
Python
Python中的面向对象
【5月更文挑战第4天】面向对象编程(OOP)是Python的核心,涉及类与对象、封装、继承和多态。类是对象的模板,对象则是类的实例。例如,`Person`类有`__init__`构造方法和`greet`方法。
15 3
|
4天前
|
算法 Java 程序员
[重学Python] Day6 面向对象编程 基础
面向对象编程基础讲解,包括类与对象的概念,类是对象的模板,对象是类的实例。Python中使用`class`定义类,通过`__init__`初始化对象。创建对象并调用方法如`drive`和`target_client`。访问权限在Python中相对宽松,使用单下划线表示受保护的属性。面向对象的三大支柱是封装、继承和多态,封装是隐藏实现细节,仅暴露简单接口。提供了数字时钟和平面上的点的类定义作为练习示例。
14 0
|
4天前
|
算法 Python
Python面向对象oop编程(二)
Python面向对象oop编程(二)
|
4天前
|
运维 算法 Shell
第六章 Python类(面向对象编程)
第六章 Python类(面向对象编程)
|
4天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
4天前
|
设计模式 算法 程序员
Python从入门到精通:2.1.3深入学习面向对象编程——设计模式的学习与实践
Python从入门到精通:2.1.3深入学习面向对象编程——设计模式的学习与实践
|
4天前
|
数据安全/隐私保护 Python
Python从入门到精通——2.2.1深入学习面向对象编程:类和对象的定义
Python从入门到精通——2.2.1深入学习面向对象编程:类和对象的定义