• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python中getattr函数使用方法 getattr实现工厂模式

python中getattr函数使用方法 getattr实现工厂模式

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python getattr函数,python getattr,python中getattr,getattr函数,getattr等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

看了下函数本身的doc

Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
</div>

解释的很抽象 告诉我这个函数的作用相当于是

object.name

试了一下getattr(object,name)确实和object.name是一样的功能.只不过这里可以把name作为一个变量去处理书上的例子很好的说明了这个函数的功用,使用getattr可以轻松实现工厂模式。

例:一个模块支持html、text、xml等格式的打印,根据传入的formate参数的不同,调用不同的函数实现几种格式的输出

返回的是这个方法的对象 就可以直接使用了 如果要添加新的格式 只需要在模块中写入新的方法函数 在调用output函数时使用新的参数就可以使用不同的格式输出

确实很方便


为了加深对getattr函数的理解 转载一篇英文的说明

Python's getattr function is used to fetch an attribute from an object, using a string object instead of an identifier to identify the attribute. In other words, the following two statements are equivalent:

[code]
value = obj.attribute
value = getattr(obj, "attribute")
If the attribute exists, the corresponding value is returned. If the attribute does not exist, you get an AttributeError exception instead.

The getattr function can be used on any object that supports dotted notation (by implementing the __getattr__ method). This includes class objects, modules, and even function objects.

path = getattr(sys, "path")
doc = getattr(len, "__doc__")
The getattr function uses the same lookup rules as ordinary attribute access, and you can use it both with ordinary attributes and methods:

result = obj.method(args)

func = getattr(obj, "method")
result = func(args)
or, in one line:

result = getattr(obj, "method")(args)
Calling both getattr and the method on the same line can make it hard to handle exceptions properly. To avoid confusing AttributeError exceptions raised by getattr with similar exceptions raised inside the method, you can use the following pattern:

try:
    func = getattr(obj, "method")
except AttributeError:
    ... deal with missing method ...
else:
    result = func(args)
The function takes an optional default value, which is used if the attribute doesn't exist. The following example only calls the method if it exists:

func = getattr(obj, "method", None)
if func:
    func(args)
Here's a variation, which checks that the attribute is indeed a callable object before calling it.

func = getattr(obj, "method", None)
if callable(func):
    func(args)
</div>

</div>

您可能想查找下面的文章:

  • 浅谈python中的getattr函数 hasattr函数
  • Python中getattr函数和hasattr函数作用详解
  • 详解Python中 __get__和__getattr__和__getattribute__的区别
  • 浅谈python中的getattr函数 hasattr函数
  • Python中getattr函数和hasattr函数作用详解
  • python中的内置函数getattr()介绍及示例
  • python中getattr函数使用方法 getattr实现工厂模式
  • Python __getattr__与__setattr__使用方法

相关文章

  • 举例讲解Python中的list列表数据结构用法
  • python使用socket远程连接错误处理方法
  • 关于tf.reverse_sequence()简述
  • python简单实现刷新智联简历
  • Python3通过Luhn算法快速验证信用卡卡号的方法
  • python字符串连接方式汇总
  • Python单链表的简单实现方法
  • python清除字符串里非字母字符的方法
  • python列表操作实例
  • Python调用C语言开发的共享库方法实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • TFRecord格式存储数据与队列读取实例
    • json跨域调用python的方法详解
    • Python中文分词工具之结巴分词用法实例总结【经典案例】
    • python使用opencv读取图片的实例
    • 使用Python读写文本文件及编写简单的文本编辑器
    • Python实用日期时间处理方法汇总
    • python用来获得图片exif信息的库实例分析
    • python实现实时监控文件的方法
    • Python中实现参数类型检查的简单方法
    • Python实现简单的文件传输与MySQL备份的脚本分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有