Python getattr()函数使用方法代码实例


Posted in Python onAugust 10, 2020

getatter()通过方法名字符串调用方法,这个方法最主要的作用就是实现反射机制,也就是说可以通过字符串获取方法实例,这样就可以把一个类可能要调用的方法放到配置文件里,需要的时候进行动态加载。

1: 可以从类中获取属性和函数

新建test.py文件,代码如下:

# encoding:utf-8
import sys
 
class GetText():
  def __init__(self):
    pass
 
  @staticmethod
  def A():
    print("this is a staticmethod function")
 
  def B(self):
    print("this is a func")
  c = "cc desc"
 
if __name__ == '__main__':
  print(sys.modules[__name__]) # <module '__main__' from 'D:/脚本项目/lianxi/clazz/test.py'>
  print(GetText)  # <class '__main__.GetText'>
  # 获取函数
  print(getattr(GetText, "A"))  # <function GetText.A at 0x00000283C2B75798>
  # 获取函数返回值
  getattr(GetText, "A")()  # this is a staticmethod function
  getattr(GetText(), "A")()  # this is a staticmethod function
 
  print(getattr(GetText, "B"))  # <function GetText.B at 0x000001371BF55798>
  # 非静态方法不可用
  # getattr(GetText, "B")()
  getattr(GetText(), "B")()   # this is a func
  print(getattr(GetText, "c")) # cc desc
  print(getattr(GetText(), "c"))  # cc desc

2:从模块中获取类(通过类名字符串得到类对象)

新建test1.py,代码如下:

#encoding:utf-8
import sys
import test
print(sys.modules[__name__])
 
# 从模块中获取类对象
class_name = getattr(test, "GetText")
print(class_name)  # <class 'test.GetText'>
 
# 调用类的属性和函数
print(getattr(class_name, "A"))  # <function GetText.A at 0x000001D637365678>
# 获取函数返回值
getattr(class_name, "A")()  # this is a staticmethod function
getattr(class_name(), "A")()  # this is a staticmethod function
 
print(getattr(class_name(), "B"))  # <bound method GetText.B of <test.GetText object at 0x0000022D3B9EE348>>
# getattr(class_name, "B")()  非静态方法不可用
getattr(class_name(), "B")()  # this is a func
 
# 获取属性值
print(getattr(class_name, "c"))  # cc desc
print(getattr(class_name(), "c"))  # cc desc

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python计算书页码的统计数字问题实例
Sep 26 Python
Python的Django框架中模板碎片缓存简介
Jul 24 Python
Python黑魔法Descriptor描述符的实例解析
Jun 02 Python
教你用python3根据关键词爬取百度百科的内容
Aug 18 Python
Python简单遍历字典及删除元素的方法
Sep 18 Python
python3.4下django集成使用xadmin后台的方法
Aug 15 Python
PyQt5实现无边框窗口的标题拖动和窗口缩放
Apr 19 Python
python 实现A*算法的示例代码
Aug 13 Python
Python3实现的简单三级菜单功能示例
Mar 12 Python
解决Keras 与 Tensorflow 版本之间的兼容性问题
Feb 07 Python
详解pandas中iloc, loc和ix的区别和联系
Mar 09 Python
python 使用tkinter+you-get实现视频下载器
Nov 17 Python
Python matplotlib模块及柱状图用法解析
Aug 10 #Python
Python如何操作docker redis过程解析
Aug 10 #Python
基于Python实现下载网易音乐代码实例
Aug 10 #Python
Python grequests模块使用场景及代码实例
Aug 10 #Python
基于Python pyecharts实现多种图例代码解析
Aug 10 #Python
Python Celery异步任务队列使用方法解析
Aug 10 #Python
使用Python将语音转换为文本的方法
Aug 10 #Python
You might like
PHP高级对象构建 多个构造函数的使用
2012/02/05 PHP
php页面防重复提交方法总结
2013/11/25 PHP
php mail to 配置详解
2014/01/16 PHP
php使用curl通过代理获取数据的实现方法
2016/05/16 PHP
ajax+php实现无刷新验证手机号的实例
2017/12/22 PHP
JavaScript OOP面向对象介绍
2010/12/02 Javascript
javascript SpiderMonkey中的函数序列化如何进行
2012/12/05 Javascript
JavaScript弹出窗口方法汇总
2014/08/12 Javascript
Jquery对象和Dom对象的区别分析
2014/11/20 Javascript
浅谈jQuery中对象遍历.eq().first().last().slice()方法
2014/11/26 Javascript
JS+CSS实现仿msn风格选项卡效果代码
2015/10/22 Javascript
JavaScript结合Bootstrap仿微信后台多图文界面管理
2016/07/22 Javascript
jQuery网页定位导航特效实现方法
2016/12/19 Javascript
canvas实现十二星座星空图
2017/02/14 Javascript
Iview Table组件中各种组件扩展的使用
2018/10/20 Javascript
vue使用axios上传文件(FormData)的方法
2019/04/14 Javascript
浅析vue插槽和作用域插槽的理解
2019/04/22 Javascript
nodejs对项目下所有空文件夹创建gitkeep的方法
2019/08/02 NodeJs
vue Treeselect下拉树只能选择第N级元素实现代码
2020/08/31 Javascript
Python标准库06之子进程 (subprocess包) 详解
2016/12/07 Python
Python基于百度AI的文字识别的示例
2018/04/21 Python
使用pandas实现csv/excel sheet互相转换的方法
2018/12/10 Python
Django Rest framework频率原理与限制
2019/07/26 Python
win10系统Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安装教程
2019/12/03 Python
python 多进程和协程配合使用写入数据
2020/10/30 Python
斯洛伐克家具和时尚装饰品购物网站:Butlers.sk
2019/09/08 全球购物
size?法国官网:英国伦敦的球鞋精品店
2020/03/15 全球购物
质检的岗位职责
2013/11/17 职场文书
对孩子的寄语
2014/04/09 职场文书
优秀安全员事迹材料
2014/05/11 职场文书
计划生育证明格式范本
2014/09/12 职场文书
2014年平安夜寄语
2014/12/08 职场文书
2016公务员年度考核评语
2015/12/01 职场文书
小学语文课《掌声》教学反思
2016/03/03 职场文书
分享几个JavaScript运算符的使用技巧
2021/04/24 Javascript
python调试工具Birdseye的使用教程
2021/05/25 Python