python introspection mechanism

python introspection is through a mechanism of the internal structure of the object

the Person class: 
"" "
person
" ""
name = "the User"

class Student (the Person):
DEF __init __ (Self, scool_name):
self.scool_name = scool_name

IF __name__ == "__main__":
the User = Student ( "Mu class network ")

# __dict__ through query properties
print (user .__ dict__) # here does not find the name of the property to the Person, because it belongs to the class attribute of Person, not belonging to the object attribute
user .__ dict __ [" school_addr " ] =" Beijing "
Print (user.school_addr)
Print (the Person .__ dict__)
Print (user.name)
Print (dir (the User)) # will list all the properties of objects, only the attribute name, not the property value

 

You can return an object in python __dict__ and dir () properties, except that:

  • __dict__ is a property of the object, and dir () is a built-in method;
  • __dict__ return property name and value of an object, i.e. dict type, dir () Returns a property name of the List ;
  • Each object has not __dict__, and dir () are functions for objects of various types, such as module, type, class, object;
  • _dict__ object returns only writable property (writable attributes), and dir () Returns all relevant (relavent) properties, and for different types of objects, a different effect .

Guess you like

Origin www.cnblogs.com/callyblog/p/11331638.html