Discuss / Python / d

定义Moel的函数是def getattr(self, key):,为什么调的时候是getattr(self,k,None)呢?

查了下官方文档,大概是这个意思: getattr(object, name[, default]) Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.


  • 1

Reply