GetAttr function hasattr function in Python

Source: Internet
Author: User

Hasattr (object, name)
Action: Determines whether the object contains an attribute named name (Hasattr is implemented by calling GetAttr (Ojbect, name) to throw an exception.
Example:

' Append '  'add') False

GetAttr (Object,name,default):

Function: Returns the property value of the property whose name is named name, returns its property value directly if the property name exists, or fires the Attribeterror exception if the property name does not exist, or returns the default value when the optional parameter is the default definition.

The main function of this method is to implement the reflection mechanism. This means that the method instance can be obtained through a string. In this way, you can put a method that a class might want to invoke in the configuration file and load it dynamically when needed.

Let's use a small example to illustrate their usage:

ImportFunc_file#Customizing the Python moduleCs=input ('Please enter the URL you want to access:')ifcs=='Loggin': Func_file.loggin ()ifCS = ='Home': Func_file.home ()ifCS = ="': Pass#the following ellipsis

When I define a custom module, to invoke the method, when using if to judge, if the module in many ways, will greatly affect the efficiency of development, code redundancy is poor, obviously this is undesirable. Below we use the hasattr () function to achieve our requirements:

Examples are as follows:

ImportFunc_file#custom Python modules need to exist beforehanddefrun (): whileTrue:cs=input ('Please enter the URL you want to access:')    #hasattr to manipulate (find) members in objects (modules) using the form of a string    ifHasattr (FUNC_FILE,CS):#determine if the URL entered by the user is in the Func_file moduleFunc=getattr (FUNC_FILE,CS)#The CS function under the Func_file module is assigned a valueFunc ()#equivalent to executing the CS function under the Func_file module    Else:        Print('404')#Define error pageRun ()

After we import a custom module, GATATTR can dynamically load according to the input content and use the hasattr () function to determine whether the user input is present or not, and the custom method is called if it does not exist.

is not the feeling and we open URL URL very similar Ah!

The previous example has a problem, in fact, our function function may be stored in many modules, each need to be imported separately, then we can use the GetAttr () function to load the module dynamically? Of course you can.

Take a look at the example:

defrun (): whileTrue:cs=input ('Please enter:') V,k=cs.split ('/')#methods of obtaining input modules and modulesobj=__import__('lib.'+v,fromlist=true)#call the module fromlist=true in the Lib directory to import by path connection    ifhasattr (obj,k): F=GetAttr (Obj,k) f ()Else:        Print(' 404')if __name__=='__main__': Run ()

is not feel getattr very strong ah. In fact, GetAttr () is a block that realizes Python's reflection, and in combination with other methods such as SetAttr (), dir (), we can also make a lot of interesting things.

                                                                  

GetAttr function hasattr function in Python

Related Article

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.