This article shares with you the main isPythonbuilt-in functionsGetAttr () related content, take a look at it, I hope you learn Python http://www.maiziedu.com/land/python/helpful. The above code looks at the portal Record GetAttris apythona built-in function in the,First look at the definition >>> Help (GetAttr) Help onbuilt-inchfunctionGetAttrinchModule__BUILTIN__: GetAttr (...) GetAttr (object, name[,default]), value GetA named attribute froman object; GetAttr (x, ' Y ') is equivalent to x.y. whenAdefaultArgument isGiven, it isReturned whenThe attribute doesn ' t Exist Without it, an exception isRaisedinchThat Case. >>> GetAttr ()The three parameters: · Object:the object to instantiate · Name: The command action to perform · Default:(can omit parameters) when the command action to be executed does not exist, it will return the incomingdefault GetAttr ()The main function is to get the instance of the method dynamically by passing in the string. The biggest use of this function is the function decoupling, you can say some of the methods may be called, write to a configuration file, when needed to dynamically load. the explanation is abstract and simple:GetAttr (object, name)equivalent toObject.nameThe following code illustrates forI in range (T): C, *args=Map(Str,input ().Split()) GetAttr (s),C) (*(int(x) forxInchargs))This code implements the user from thestdininput commands and parameters, the command is assigned to theC, the parameter is passed to a pointer variable*args,used here*argsis to receive an indeterminate number of parameters. And then callgetattr (s,c) to performCthis directive (note:sis a string), heregetattr (s,c)it can be equivalent toS.C,in parentheses behind(* (int (x) for x in args)),here is a generator expression, which is called a parametric unpacking, similar to how you define a function with any number (positional) Parameters-*The sequence is traversed before the sequence and its members become function call parameters. As you can see, this usage is much more convenient than enumeration, and also has a higher extensibility. Extended PythonNeutralizationGetAttrThere are also relatedhasattr,SetAttr,delattr, by example. #defining a Class objectclassVimiix: def__init__(self): Self.name = ' Vimiix ' defSetName(self, name): Self.name = Name defGetName(self): returnSelf.name #instantiating an object foo = Vimiix () hasattr (object, name) determines Whether an object has a name Property >>>hasattr (foo, ' GetName ') True setattr (Object,name,default) sets a new property and assigns a value . similar foo.age = >>>setattr (foo, ' age ', ' 18 ') delattr (object, name) removes the name Property of the object >>>delattr (foo, ' age ') >>>getattr (foo, "age", ' not find ') ' Not find ' Source: Vimiix
How does Python's built-in function getattr () work?