python getattr函数资料翻译

该函数用于得到一个对象的属性,使用字符串而非标示符表示属性,简而言之,下面两条语句等效

value = obj.attribute
value = getattr(obj, "attribute")

如果对象存在该属性,则属性值被返回,如果不存在该属性,则返回AttributeError异常


该函数可以用于函数,模块,对象,可以用于任意支持.操作的上述类型

下面给出几个例子

path = getattr(sys, "path")
doc = getattr(len, "__doc__")


result = obj.method(args)


func = getattr(obj, "method")
result = func(args)

理论上也可以放在一行

result = getattr(obj, "method")(args)
但是这样做容易导致异常不易检查

因此建议使用荣块捕获异常

try:
    func = getattr(obj, "method")
except AttributeError:
    ... deal with missing method ...
else:
    result = func(args)

下面的2个例子则进行了函数存在检查和参数检查


func = getattr(obj, "method", None)
if func:
    func(args)


func = getattr(obj, "method", None)
if callable(func):
    func(args)

总结:个人觉得该函数对于实现工厂类有较大帮助,没有遇到相关代码之前,可能不太好理解该函数作用


下面附上原文:

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:


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)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mopria Print Service 2.3推出新的移动打印功能,支持用户使用 安卓设备的“分享”功能进行打印 拓展移动打印功能,方便用户使用其最喜爱的应用程序中的“分享”选项打印照片、文本 、网页等 加州圣拉蒙 – 2017年11月15日– 致力于推动全行业移动打印标准的全球非盈利性会员制组织 Mopria®联盟今日发布Mopria Print Service 2.3,以扩大移动打印支持,允许用户使用诸多最喜爱的 应用程序中的“分享”功能进行打印。此外,Mopria Print Service 2.3还与Android 8.0 Oreo™兼容,后 者的默认打印解决方案采用Mopria核心技术,支持通过1亿多台经Mopria认证的打印机实现轻松移 动打印。所有安卓设备可在Google Play™商店下载其最新版。 Mopria Print Service 2.3利用Facebook、Flipboard、LinkedIn、Twitter和Pinterest等诸多 备受欢迎的应用中现有的“分享”功能,使用户能够轻松打印。当使用“分享”功能时,用户 将看到其中包含Mopria Print Service选项,该选项位于电子邮件和消息选项下方。“分享” 图标位于醒目位置,用户只需选择Mopria Print Service选项、选择打印机、调整设置并 打印即可。 该新版本还支持沿多个方向打孔、折边、装订等分页装订选项,同时提供新界面,新增一 项便于用户快速查看可供使用的打印机并确定其状态的功能。该新界面提供便捷的起点, 方便用户添加或隐藏打印机、访问Mopria设置、排除打印故障以及查看经Mopria认证的 打印机列表。该新界面包含教程,已将这一用户请求最多的功能添加至该项服务中,为新 用户提供移动打印流程指导及建议。 Mopria联盟指导委员会主席Brent Richtsmeier表示:“我们将继续为Mopria Print Service 添加功能,旨在使安卓设备的移动打印体验尽可能轻松便捷。利用全球最受欢迎的应用中 的分享功能中新增的打印选项,用户将重新发现移动打印的便利。” 自发布以来,Mopria Print Service已支持通过安卓智能手机和平板电脑连接经Mopria认 证的打印机实现打印。Mopria Print Service允许用户轻松自定义彩色打印、打印份数、 双面打印、纸张尺寸、页面打印范围、介质类型和打印方向等打印设置,同时还可以定制 用户身份验证、点针式打印和会计功能等工作场所功能,而且不需要安装特定打印机品牌 的应用程序。 自创始人佳能、惠普、三星和施乐成立Mopria联盟以来的四年里,该非盈利性会员制组 织成员已增加至21个,他们代表着全球打印机行业。除创始公司之外,Mopria联盟目前 包括:Adobe、柯尼卡美能达、高通、利盟、京瓷、东芝、Brother、爱普生、富士施乐 、微软、NEC、Pantum、理光、YSoft、夏普、戴尔和致伸科技(Primax)。 Mopria技术目前设备安装数量已超过7亿台,预计不出一年便会超过10亿台。在继续努力 使移动打印变得轻松便捷的同时,Mopria联盟还将进一步专注于云打印和扫描标准化。 Mopria联盟的目标在于促使这些解决方案为消费者和企业用户变得更加方便可及、直观 简便。如需获取经Mopria认证的打印机和打印附件的最新列表,敬请访问: http://mopria.org/certified-products。 关于Mopria®联盟 Mopria®联盟是一个非盈利性会员制组织,由全球领先的科技公司组成,其成立初衷为简 化智能手机、平板电脑和其他设备的打印操作。该联盟制定并推广一系列技术标准,旨在 提供连接不同设备和移动操作系统的直观体验。这些标准的普及使用户能够与各种品牌的 打印机进行无缝交互。此外,对新移动设备或打印机而言,将无需进行应用程序的下载和 安装。利用支持Mopria的移动设备寻找经Mopria认证的打印机和附件实现轻松打印,可 在www.mopria.org了解详情或者在Google Play商店下载最新版Mopria软件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值