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里将list中元素依次向前移动一位
Sep 12 Python
Python的Flask开发框架简单上手笔记
Nov 16 Python
python实现简易云音乐播放器
Jan 04 Python
python 字符串和整数的转换方法
Jun 25 Python
Python退火算法在高次方程的应用
Jul 26 Python
django连接mysql配置方法总结(推荐)
Aug 18 Python
python pandas获取csv指定行 列的操作方法
Jul 12 Python
django的分页器Paginator 从django中导入类
Jul 25 Python
通过 Django Pagination 实现简单分页功能
Nov 11 Python
Python基于当前时间批量创建文件
May 07 Python
Python使用socketServer包搭建简易服务器过程详解
Jun 12 Python
django和flask哪个值得研究学习
Jul 31 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
简单介绍下 PHP5 中引入的 MYSQLI的用途
2007/03/19 PHP
thinkphp常见路径用法分析
2014/12/02 PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
2016/10/10 PHP
详解PHP编码转换函数应用技巧
2016/10/22 PHP
判断多个input type=file是否有已经选择好文件的代码
2012/05/23 Javascript
JS+CSS设置img在DIV中只显示Img垂直居中的部分
2013/10/24 Javascript
JS关闭窗口与JS关闭页面的几种方法小结
2013/12/17 Javascript
Javascript中String的常用方法实例分析
2015/06/13 Javascript
JavaScript实现弹出广告功能
2017/03/30 Javascript
深入理解 JavaScript 中的 JSON
2017/04/06 Javascript
angular实现IM聊天图片发送实例
2017/05/08 Javascript
js学习总结之DOM2兼容处理重复问题的解决方法
2017/07/27 Javascript
AngularJS ionic手势事件的使用总结
2017/08/09 Javascript
利用node实现一个批量重命名文件的函数
2017/12/21 Javascript
webpack中使用iconfont字体图标的方法
2018/02/22 Javascript
JavaScript实现点击图片换背景
2020/11/20 Javascript
python通过pil将图片转换成黑白效果的方法
2015/03/16 Python
python使用cStringIO实现临时内存文件访问的方法
2015/03/26 Python
Tensorflow实现卷积神经网络的详细代码
2018/05/24 Python
用python简单实现mysql数据同步到ElasticSearch的教程
2018/05/30 Python
python去掉 unicode 字符串前面的u方法
2018/10/21 Python
python 在屏幕上逐字显示一行字的实例
2018/12/24 Python
Pytorch 搭建分类回归神经网络并用GPU进行加速的例子
2020/01/09 Python
python实现连连看游戏
2020/02/14 Python
python实现快速文件格式批量转换的方法
2020/10/16 Python
python解包用法详解
2021/02/17 Python
自定义html标记替换html5新增元素
2008/10/17 HTML / CSS
香港礼品网站:GiftU eshop
2017/09/01 全球购物
泰国Robinson百货官网:购买知名品牌的商品
2020/02/08 全球购物
印刷工程专业应届生求职信
2013/09/29 职场文书
便利店的创业计划书
2014/01/15 职场文书
群众路线教育党课主持词
2014/04/01 职场文书
高校自主招生教师推荐信
2015/03/23 职场文书
新员工入职感言范文!
2019/07/04 职场文书
担保书范文
2019/07/09 职场文书
Python开发之QT解决无边框界面拖动卡屏问题(附带源码)
2021/05/27 Python