#!/usr/bin/env python#-*-coding:utf-8-*-getattr (), hasattr (), delattr (), SetAttr () #反射: A string-based way to manipulate its members in an object (module) import COMMONSINP = input ("Please enter a page method to enter") if Hasattr (COMMONS,INP): #检查commons中是否有对应名称的操作成员 func=getattr (COMMONS,INP) # Reflection finds the action Member func () #执行找到的成员else: print ("No input module exists") #动态导入模块和寻找模块中的方法ips =input ("GetAttr") in Commons Please enter the module and page method to enter ") #以 commons/login Form Input module and method name M,f=ips.split ('/') obj= __import__ (m) #动态导入模块if hasattr (obj,f): # Verify that the method name is specified in the current import module func1=getattr (obj,f) func1 () Else: print ("No input page or module exists") #动态导入模块和寻找模块中的方法 Import module in folder condition Ips=input ("Please enter module and page method required") #以 commons/account Form Input module and method name M,f=ips.split ('/') obj= __import__ ("lib.") +m,fromlist=true) #动态导入模块if hasattr (obj,f): #验证当前导入模块中是否有指定方法名的方法 func1=getattr (obj,f) func1 () Else: Print ("No input page or module exists")
commons.py
#!/usr/bin/env python#-*-coding:utf-8-*-def Login (): print ("Enter Login Interface") def Home (): print ("Go to Main interface") def LogOut () : print ("Log Out Program")
Lib folder under account.py
#!/usr/bin/env python#-*-coding:utf-8-*-def Test (): print ("test method")
Python Basic Learning 1-reflection