我最近读了关于getattr()函数的文章。问题是我仍然不能理解它的用法。我对getattr()的唯一理解是,getattr(li,“pop”)与调用li.pop相同。
我不理解书中提到如何使用它来获取函数的引用,而直到运行时才知道它的名称。也许这就是我在编程方面的新手。谁能解释一下这个问题?我何时以及如何使用它?
我最近读了关于getattr()函数的文章。问题是我仍然不能理解它的用法。我对getattr()的唯一理解是,getattr(li,“pop”)与调用li.pop相同。
我不理解书中提到如何使用它来获取函数的引用,而直到运行时才知道它的名称。也许这就是我在编程方面的新手。谁能解释一下这个问题?我何时以及如何使用它?
当前回答
# getattr
class hithere():
def french(self):
print 'bonjour'
def english(self):
print 'hello'
def german(self):
print 'hallo'
def czech(self):
print 'ahoj'
def noidea(self):
print 'unknown language'
def dispatch(language):
try:
getattr(hithere(),language)()
except:
getattr(hithere(),'noidea')()
# note, do better error handling than this
dispatch('french')
dispatch('english')
dispatch('german')
dispatch('czech')
dispatch('spanish')
其他回答
setattr ()
我们使用setattr向类实例添加一个属性。我们传递类实例、属性名和值。
getattr ()
使用getattr可以检索这些值
例如
Employee = type("Employee", (object,), dict())
employee = Employee()
# Set salary to 1000
setattr(employee,"salary", 1000 )
# Get the Salary
value = getattr(employee, "salary")
print(value)
Getattr (object, 'x')完全等价于object.x。
getattr只在两种情况下有用。
你不能写object。X,因为您事先不知道您想要哪个属性(它来自字符串)。对于元编程非常有用。 您需要提供一个默认值。对象。如果没有y, y将引发AttributeError。但是getattr(object, 'y', 5)将返回5。
对我来说,getattr是最简单的解释方式:
它允许您根据字符串的内容调用方法,而不是键入方法名称。
例如,你不能这样做:
obj = MyObject()
for x in ['foo', 'bar']:
obj.x()
因为x不是内置类型,而是str。然而,你可以这样做:
obj = MyObject()
for x in ['foo', 'bar']:
getattr(obj, x)()
它允许您根据输入动态地连接对象。我发现它在处理自定义对象和模块时很有用。
它也从https://www.programiz.com/python-programming/methods/built-in/getattr澄清
class Person:
age = 23
name = "Adam"
person = Person()
print('The age is:', getattr(person, "age"))
print('The age is:', person.age)
年龄:23岁
年龄:23岁
class Person:
age = 23
name = "Adam"
person = Person()
# when default value is provided
print('The sex is:', getattr(person, 'sex', 'Male'))
# when no default value is provided
print('The sex is:', getattr(person, 'sex'))
性别:男性
AttributeError: 'Person'对象没有属性'sex'
Python中的对象可以具有属性——数据属性和使用这些属性的函数(方法)。实际上,每个对象都有内置属性(在Python控制台中尝试dir(None), dir(True), dir(…),dir(dir))。
例如,你有一个对象人,它有几个属性:名字,性别等。
访问这些属性(无论是方法还是数据对象)通常需要写:person.name, person。性别、person.the_method()等。
但是,如果在编写程序时不知道属性的名称怎么办?例如,您将属性名存储在名为attr_name的变量中。
if
attr_name = 'gender'
然后,不写了
gender = person.gender
你可以写
gender = getattr(person, attr_name)
一些做法:
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
>>> class Person():
... name = 'Victor'
... def say(self, what):
... print(self.name, what)
...
>>> getattr(Person, 'name')
'Victor'
>>> attr_name = 'name'
>>> person = Person()
>>> getattr(person, attr_name)
'Victor'
>>> getattr(person, 'say')('Hello')
Victor Hello
如果对象中不存在具有给定名称的属性,getattr将引发AttributeError:
>>> getattr(person, 'age')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute 'age'
但是你可以传递一个默认值作为第三个参数,如果这个属性不存在,它将被返回:
>>> getattr(person, 'age', 0)
0
你可以使用getattr和dir来遍历所有属性名并获得它们的值:
>>> dir(1000)
['__abs__', '__add__', ..., '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
>>> obj = 1000
>>> for attr_name in dir(obj):
... attr_value = getattr(obj, attr_name)
... print(attr_name, attr_value, callable(attr_value))
...
__abs__ <method-wrapper '__abs__' of int object at 0x7f4e927c2f90> True
...
bit_length <built-in method bit_length of int object at 0x7f4e927c2f90> True
...
>>> getattr(1000, 'bit_length')()
10
这样做的一个实际用途是找到所有名称以test开头的方法并调用它们。
与getattr类似的还有setattr,它允许你设置一个对象的属性。
>>> setattr(person, 'name', 'Andrew')
>>> person.name # accessing instance attribute
'Andrew'
>>> Person.name # accessing class attribute
'Victor'
>>>