First, reflection
definition: use a string to manipulate (Find/check/delete/set) members in objects (modules)
#getattr: Get Properties in module
#hasattr: Checks if a member (function) exists in the module
#delattr: Remove members from a module
#setattr: Set a block in the
Common Reflection modes:
Import fanshe_commons #导入访问函数的模块 "fanshe_commons module with functions such as Login,logout,home" Def Run (): INP = input ("Please enter access URL:") if INP = = ' Login ': #fanshe_commons. Login () #调用fanshe_commons模块中login函数, when the input input URL is login, execute the login function, At this time than forget to execute function to take login () elif INP = = ' Logout ': fanshe_commons.logout () elif InP = = ' Home ': Fanshe_commons.home () Else:print (" 404 ") if __name__ = = ' __main__ ': #if __name__ = = ' __main__ ' indicates that the main function is executed only in the current module run ()
Reflective class 02:
Hasattr: Checks if a member (function) exists in the module
#getattr: Get Properties in module
Import fanshe_commons # #导入访问函数的模块 fanshe_commons a block containing the Login,logout,home function Def run (): InP =input (" Please enter the URL to access: ") #inp属于一个字符串, then FANSHE_COMMONS.INP ()! = Fanshe_commons.login () if Hasattr (FANSHE_COMMONS,INP): # #hasattr : Check if a member (function) func = GetAttr (FANSHE_COMMONS,INP) is present in the module #getattr: Gets the property in the module func () else:print ("404") if __name__ = = ' __main__ ': Run ()
Reflective Class 03:
Apply the "_import_" method: You can call the methods in multiple modules, without having to import a block
Format: #模块/method
Import fanshe_commons # #导入访问函数的模块 fanshe_commons a block containing login,logout,home function def run (): #commons/login Note: In the output mode also need to follow "a block/method" of InP =input ("Please enter the URL to access:") #inp属于一个字符串, then FANSHE_COMMONS.INP ()! = Fanshe_commons.login () m , F = inp.split ('/') #将mn比作为一个模块, F is a function or member of this module, obj = __import__ (m) #将模赋值给一个值if hasattr (obj,f): # #hasattr: Check if a member (function) func = GetAttr (obj,f) is present in the module #getattr: Gets the property in the module func () else:print ("404") if __name__ = = ' __main__ ': Run ()
class 04: Import functions in different folders
# obj = __import__ ("lib." +m,fromlist=true) #导入两个不同的文件夹中的函数
Import fanshe_commons # #导入访问函数的模块 fanshe_commons a block containing login,logout,home function def run (): #commons/login Note: In the output mode also need to follow "a block/method" of InP =input ("Please enter the URL to access:") #inp属于一个字符串, then FANSHE_COMMONS.INP ()! = Fanshe_commons.login () m , F = inp.split ('/') #将mn比作为一个模块, F is the function or member of this module # obj = __import__ (m) #将模赋值给一个值obj = __import__ ("lib." +m,fromlist=true) #导入两个不同的文件夹中的函数if hasattr (obj,f): # #hasattr: Checks for a member (function) in the module func = GetAttr (obj,f) # GetAttr: Get module in attribute func () else:print ("404") if __name__ = = ' __main__ ': Run ()
Extended
Several common ways to import modules are:
A:import XXX Module
B:from xxx Folder Import + method
C:obj = _import_ ("module")
D:obj = _import_ ("folder. oo.xx", fromlist=true)
Second, the regular expression
Three
Python_day6_ Reflection _ Regular expression of the article