English documents:
-
-
getattr (
object,
name[,
default])
-
-
-
Return the value of the named attribute of
object.
name must is a string. The If the string is the name of the one of the object ' s attributes, and the result is the value of this attribute. For example, was
getattr(x, ‘foobar‘) equivalent to
x.foobar . If the named attribute does not exist, the
default is returned if provided, and otherwise is
AttributeError raised.
-
-
-
-
Description
-
-
-
-
-
1. The function function is to get the property named name from Object objects, equivalent to calling Object.name.
-
#Defining class Student>>>classStudent:def __init__(self,name): Self.name=name>>> s = stduent ('Aim')>>> GetAttr (s),'name')#equivalent to calling S.name'Aim'>>>S.name'Aim'
2. Function The third parameter of the default is an optional parameter, if the object in the meaning of the Name property, the value of the Name property is returned, if there is no Name property, the default value is returned, if default does not pass in the value, then an error.
#Defining class Student>>>classStudent:def __init__(self,name): Self.name=name>>> GetAttr (s),'name')#existence attribute name'Aim'>>> GetAttr (s),' Age', 6)#No attribute age exists, but default value is provided, return default value6>>> GetAttr (s),' Age')#attribute age not present, default value not provided, error calledTraceback (most recent): File"<pyshell#17>", Line 1,inch<module>GetAttr (s),' Age') Attributeerror:'stduent'object has no attribute' Age'
Python built-in function (--GETATTR)