Python iterates over and gets the object properties--dir (), __dict__,getattr,setattr

Source: Internet
Author: User

First, traverse the properties of the object:

1.dir (obj): Returns the list of property name strings for the object, including properties and methods.

For attr in Dir (obj):

Print (attr)

2.OBJ.__DICT__: Returns the object's property name and value key-value pair or the method name and method object (which can be called) key-value pairs.

The Dir function and the __dict__ property are basically all objects (modules, classes) that can be used, and Dir () returns the parent collection returned by __dict__.

Second, get or set properties

GetAttr (obj,name)//Returns a property value or function object, and if name is the function name, it can be called by parentheses, that is, GetAttr (Obj,name) () called the function name.

SetAttr (Obj,name,value)//equivalent to Obj.name=value assignment statement

Third, iterate over the object properties and then call the function or set the value

Will

Python iterates over and gets the object properties--dir (), __dict__,getattr,setattr

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.