# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法getattr() #getattr() #说明: ''' getattr(...) getattr(object, name[, default]) -> value default:默认值 Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argum…
最近看Dive into python第四章自省中提到getattr()函数,作为一个内建函数平时自己没怎么用过所以也不太理解这个函数的一些用法 看了下函数本身的doc getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returne…
如果某个实例foo有多个方法, 当对foo的每一个方法我们都需要使用try ... except ...进行包装的时候,内置函数getattr()可以用来精简代码. 1. getattr()的用法 # https://docs.python.org/2/library/functions.html#getattr getattr(object, name[, default]) Return the value of the named attribute of object. name mus…