- 論壇徽章:
- 0
|
小弟初學(xué)python, 麻煩各位高手看看怎么在反射中做字符串匹配?
代碼如下:
###############account.py############
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
#Login/Logout/Logoff
def User_Login():
print "Login"
def User_Logout():
print "Logout"
##############Index.py##############
# 定義User的操作 輸入方式為:XXX/XXX
#from reflect.backend.account import account
data = raw_input("Please input the address:")
addr = data.split('/')
#ImportError: No module named account
user_operation= __import__('backend.'+addr[0])
#'module' object has no attribute 'login'
model = getattr(user_operation,addr[0])
func = getattr(model,addr[1])
func()
我的問題是:用戶輸入account/User_Login, 才可以執(zhí)行User_Login()方法。如果用戶輸入account/login或者account/Login, 就會提示錯誤AttributeError: 'module' object has no attribute 'login'。
有什么辦法可以進(jìn)行匹配而同樣執(zhí)行User_Login()方法呢?
謝謝!
|
|