以下示例来自Python 2.7上的REST数据库驱动程序。 在下面的__setattr__方法,如果我使用注释getattr()线,它降低了从600个RPS的对象实例化性能230 为什么getattr()比在这种情况下self.__dict__.get()这么慢得多? class Element(object):
def __init__(self, client):
s
我试图用getattr功能在我的代码使用发电机 li=[]
m=[method for method in dir(li) if callable(getattr(li,method))]
print getattr(li,(str(i) for i in m))
错误: TypeError: getattr(): attribute name must be string
如果我使用
我使用GETATTR动态访问像这样一个模型的属性(假设学生模型有一个属性名为名): students = Student.objects.all()
property = 'name'
for student in students:
print getattr(student, property)
这工作得很好,但我不知道是否有可能访问以同样的方式相关的记录的特性,例如(假
在过去的一个小时里,我一直在试图弄清楚我正在开发的支持IRC的程序中的一个错误,经过一些调试后,我发现由于某种原因,getattr工作不正常。我有以下测试代码: def privmsg(self, user, channel, msg):
#Callback for when the user receives a PRVMSG.
prvmsgText = textFormat