D22_2.1_module

##### Magic attributes associated with the class #####
# __doc__ internal document usage object to obtain an object or class / class doc__ .__
# __name__ get the class name of the function name
# __class__ obtain the current class object belongs
# __bases__ get a All classes directly inherits the parent class, returns a tuple

##### ###### reflection class
# hasattr () detects an object / class if there is a specified member hasattr (obj, "eat") notation (has attribute)
# getattr () Gets an object / class members of the value of the acquired property will return a value, acquisition method returns a function that can be called directly
"" "res = getattr (obj ," eye ") print (res) via the object the property returns out." ""
"" "func = getattr (obj," eat ") calls func (); func = getattr ( Children," eat ") call FUNC (. 1)" ""
# reflected by the object to the method, binding method is a method of binding # call without mass participation by the class the method reflected, is a common method # ordinary method requires mass participation
# res = getattr (Children, " abc", " Sorry, not the members") # You can set default values for getattr, if looking for less than the member, on the third parameter set corresponding to the return value
# setattr () value of the object / class member
# object member disposed properties setattr (obj, "hair", " blue sky"), set the class the member method setattr (Children, "age", func) which define its own func
# 4.delattr () to delete the value of the object / class member
##### (2) ##### reflection module
"" "SYS Import "" "
# Returns the sys.modules a system dictionary whose keys are loaded to all modules print (sys.modules [" __ main__ " ]) find this file modules (classes, methods and properties)

 

SYS Import
# Returns the sys.modules a system dictionary whose keys are loaded to all modules
print (sys.modules)

mymodule = sys.modules["__main__"]
print(mymodule)

def func1():
print(1)

def func2():
print(2)

def func3():
print(3)

def func4():
print(4)

# String manipulation module by members of
the while True:
FUNC = the INPUT ( "Please enter the user function you want to operate")
IF hasattr (mymodule, FUNC):
# reflected real function
Fuc = getattr (mymodule, FUNC)
Fuc ( )
the else:
Print ( "this function does not exist")

Guess you like

Origin www.cnblogs.com/banbosuiyue/p/11832480.html