Chinaunix

标题: Python反射中怎么做匹配呢? [打印本页]

作者: z0415010    时间: 2016-03-04 10:49
标题: Python反射中怎么做匹配呢?
小弟初学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, 才可以执行User_Login()方法。如果用户输入account/login或者account/Login, 就会提示错误AttributeError: 'module' object has no attribute 'login'。

有什么办法可以进行匹配而同样执行User_Login()方法呢?

谢谢!

作者: bskay    时间: 2016-03-11 11:04
金斧子砍柴吗?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2