前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python反射

Python反射

作者头像
py3study
发布2020-01-08 18:37:41
4010
发布2020-01-08 18:37:41
举报
文章被收录于专栏:python3python3

反射的定义

根据字符串的形式去某个对象中操作成员

  1. 根据字符串的形式去一个对象中寻找成员
  2. 根据字符串的形式去一个对象中设置成员
  3. 根据字符串的形式去一个对象中删除成员
  4. 根据字符串的形式去一个对象中判断成员是否存在

初始反射

通过字符串的形式,导入模块

根据用户输入的模块名称,导入对应的模块并执行模块中的方法

  1. # Python使用的是3.5.1
  2. [root@root ~]# python -V
  3. Python 3.5.1
  4. # commons.py为模块文件
  5. [root@root ~]# ls
  6. commons.py  reflection.py
  7. # commons.py文件内容
  8. [root@root ~]# cat commons.py
  9. #!/usr/bin/env python
  10. # 定义了连个函数,f1和f2
  11. def f1():
  12. return "F1"
  13. def f2():
  14. return "F2"
  15. [root@root ~]# cat reflection.py
  16. #!/usr/bin/env python
  17. # _*_ coding:utf-8 _*_
  18. # 输入模块的名称
  19. mod_name = input("请输入模块名称>>> ")
  20. # 查看输入的内容及数据类型
  21. print(mod_name, type(mod_name))
  22. # 通过__import__的方式导入模块,并赋值给dd
  23. dd = __import__(mod_name)
  24. # 执行f1()函数
  25. ret = dd.f1()
  26. # 输出函数的返回值
  27. print(ret)
  28. # 执行reflection.py
  29. [root@ansheng ~]# python reflection.py
  30. # 输入模块名称
  31. 请输入模块名称>>> commons
  32. # 返回输入的内容及数据类型
  33. commons <class 'str'>
  34. # 执行F1函数
  35. F1

通过字符串的形式,去模块中寻找指定函数,并执行

用户输入模块名称和函数名称,执行指定模块内的函数or方法

  1. [root@root ~]# cat commons.py
  2. #!/usr/bin/env python
  3. def f1():
  4. return "F1"
  5. def f2():
  6. return "F2"
  7. [root@root ~]# cat reflection.py
  8. #!/usr/bin/env python
  9. # _*_ coding:utf-8 _*_
  10. # 输入模块的名称
  11. mod_name = input("请输入模块名称>>>")
  12. # 输入函数or方法的名称
  13. func_name = input("请输入函数名称>>>")
  14. # 导入模块
  15. dd = __import__(mod_name)
  16. # 导入模块中的方法
  17. target_func = getattr(dd, func_name)
  18. # 查看target_func和dd.f1的内存地址
  19. print(id(target_func), id(dd.f1))
  20. # 执行target_func()函数
  21. result = target_func()
  22. # 输出结果
  23. print(result)
  24. [root@root ~]# python reflection.py
  25. # 输入模块名称commons
  26. 请输入模块名称>>>commons
  27. # 输入函数名称f1
  28. 请输入函数名称>>>f1
  29. # 返回内存地址
  30. 139844714989224 139844714989224
  31. # 执行的函数返回结果
  32. F1

反射相关的函数

getattr(object, name[, default])

根据字符串的形式去一个对象中寻找成员

  1. # 自定义模块的内容
  2. [root@root ~]# cat commons.py
  3. #!/usr/bin/env python
  4. Blog_Url = "https://yw666.blog.51cto.com"
  5. def f1():
  6. return "F1"
  7. def f2():
  8. return "F2"
  9. >>> import commons
  10. >>> getattr(commons, "f1")
  11. <function f1 at 0x7fbce5774598>
  12. >>> getattr(commons, "f1f1f1")
  13. Traceback (most recent call last):
  14. File "<stdin>", line 1, in <module>
  15. AttributeError: module 'commons' has no attribute 'f1f1f1'

执行获取到的函数

  1. >>> target_func = getattr(commons, "f1")
  2. >>> target_func
  3. <function f1 at 0x7fbce5774598>
  4. >>> target_func()
  5. 'F1'

通过设置默认值可以避免获取不到方法时报错

  1. # 设置一个默认值为None
  2. >>> target_func = getattr(commons, "f1f1f1", None)
  3. >>> target_func
  4. >>>

通过getattr获取模块中的全局变量

  1. >>> import commons
  2. >>> getattr(commons, "Blog_Url", None)
  3. 'https://yw666.blog.51cto.com'
  • setattr(object, name, value)

根据字符串的形式去一个对象中设置成员

设置全局变量

  1. # 获取commons内的Name变量
  2. >>> getattr(commons, "Name", None)
  3. # 在commons模块中设置一个全局变量Name,值为Ansheng
  4. >>> setattr(commons, "Name", "YangWen")
  5. # 获取commons内的Name变量
  6. >>> getattr(commons, "Name", None)
  7. 'YangWen'

getattr结合lambda表达式设置一个函数

  1. >>> setattr(commons, "as", lambda : print("as"))
  2. >>> getattr(commons, "as")
  3. <function <lambda> at 0x000001FD3E51FD90>
  4. >>> aa = getattr(commons, "as")
  5. >>> aa()
  6. as
  • delattr(object, name)

根据字符串的形式去一个对象中删除成员

  1. >>> getattr(commons, "Name")
  2. 'Ansheng'
  3. >>> delattr(commons, "Name")
  4. # 获取不到就报错
  5. >>> getattr(commons, "Name")
  6. Traceback (most recent call last):
  7. File "<stdin>", line 1, in <module>
  8. AttributeError: module 'commons' has no attribute 'Name'

hasattr(object, name)

根据字符串的形式去一个对象中判断成员是否存在

  1. # 如果不存在就返回False
  2. >>> hasattr(commons, "Name")
  3. False
  4. >>> setattr(commons, "Name", "YangWen")
  5. # 如果存在就返回True
  6. >>> hasattr(commons, "Name")
  7. True

(双下划线)import(双下划线)方式导入多层模块

  1. >>> m = __import__("lib.commons")
  2. >>> m
  3. # 返回的路径是`lib`
  4. <module 'lib' (namespace)>
  5. >>> m = __import__("lib.commons", fromlist=True)
  6. >>> m
  7. # 返回的路径是`lib.commons`
  8. <module 'lib.commons' from '/root/lib/commons.py'>

基于反射模拟Web框架路由系统

find_index.py文件内容

  1. #!/usr/bin/env python
  2. # _*_ coding:utf-8 _*_
  3. url = input("请输入url: ")
  4. target_module, target_func = url.split('/')
  5. m = __import__("lib." + target_module, fromlist=True)
  6. if hasattr(m, target_func):
  7.    target_func = getattr(m, target_func)
  8.    r = target_func()
  9. print(r)
  10. else:
  11. print("404")

目录结构及文件内容

  1. [root@ansheng ~]# tree ./
  2. ./
  3. ├── find_index.py
  4. └── lib
  5. ├── account.py
  6. └── commons.py
  7. 1 directory, 3 files
  8. [root@root ~]# cat lib/commons.py
  9. #!/usr/bin/env python
  10. Blog_Url = "yw666.blog.51cto.com"
  11. def f1():
  12. return "F1"
  13. def f2():
  14. return "F2"
  15. [root@root ~]# cat lib/account.py
  16. #!/usr/bin/env python
  17. # _*_ coding:utf-8 _*_
  18. def login():
  19. return "login"
  20. def logout():
  21. return "logout"

执行

  1. [root@root ~]# python find_index.py
  2. 请输入url: account/login
  3. login
  4. [root@root ~]# python find_index.py
  5. 请输入url: account/logout
  6. logout
  7. [root@root ~]# python find_index.py
  8. 请输入url: commons/f1
  9. F1
  10. [root@root ~]# python find_index.py
  11. 请输入url: commons/f2
  12. F2
  13. [root@root ~]# python find_index.py
  14. 请输入url: commons/asdasd
  15. 404
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 反射的定义
  • 初始反射
  • 反射相关的函数
  • hasattr(object, name)
  • (双下划线)import(双下划线)方式导入多层模块
  • 基于反射模拟Web框架路由系统
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档