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==


总结


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

相关文章
|
3月前
|
Python
python 面向对象编程(2)
python 面向对象编程(2)
|
16天前
|
Python
python反射
python反射
|
1月前
|
Python
Python中的面向对象编程与继承
本文将深入探讨Python中面向对象编程的核心概念,重点讨论继承的实现原理以及在实际开发中的应用。通过详细的示例和解释,读者将能够全面理解Python中继承的使用方式和优势,为提高代码的复用性和可维护性提供有效的技术支持。
|
2天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
19天前
|
Python
Python面向对象编程学习应用案例详解
面向对象编程在Python中通过类定义对象结构和行为。示例:1) 使用`class`关键字定义类,如`class Person`;2) `__init__`方法初始化对象属性,如`self.name`和`self.age`;3) 实例化对象,如`person1 = Person("张三", 25)`;4) 访问属性和方法,如`person1.name`;5) 定义类方法,如`def introduce(self)`;6) 调用方法,如`person1.introduce()`;7) 类继承,如`class Student(Person)`;8) 多态,通过继承重写方法实现。
9 1
|
1月前
|
Python
Python面向对象编程简介
Python面向对象编程简介
18 1
|
1月前
|
存储 机器学习/深度学习 数据安全/隐私保护
【Python 基础教程 24】全面入门Python面向对象编程:深度探索与实战教程
【Python 基础教程 24】全面入门Python面向对象编程:深度探索与实战教程
79 0
|
1月前
|
存储 Python
Python的面向对象编程(OOP)
Python的面向对象编程(OOP)
15 0
|
1月前
|
存储 Python
python面向对象编程
python面向对象编程
11 0
|
1月前
|
Python
Python中的面向对象编程:基础与实践
Python中的面向对象编程:基础与实践
11 0