# -*- coding: utf-8 -*-
# @File : python反射.py
# @Author:
# @Date : 2019/12/23
# @Desc :
# Python的四个重要内置函数:getattr()、hasattr()、delattr()和setattr()较为全面的实现了基于字符串的反射机制。
# 它们都是对内存中的模块进行操作,并不会对源文件进行修改。
# 假定你应经学会了几个python框架
# 以web框架举例说明
# 第一个python脚本 commons.py
def login():
print("This is login page")
def logout():
print("This is logout page")
def index():
print("This is index page")
# 第二个用户访问界面 user.py
import commons
def run():
inp = input("Input you will visit url").strip()
modules, func = inp.split('/')
obj = __import__(modules)
if hasattr(commons, inp):
func = getattr(commons, inp)
func()
else:
print('404')
if __name__ == '__main__':
run()