How does Python's built-in function getattr () work?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.