'module_name'的含义
The meaning of the 'module_name'
点击here。
在代码的 latt 中。我无法理解'module_name'。它来自哪里? 'module_name' 是什么意思?
def add_routes(app, module_name):
n = module_name.rfind('.')
if n == (-1):
mod = __import__(module_name, globals(), locals())
else:
name = module_name[n+1:]
mod = getattr(__import__(module_name[:n], globals(), locals(), [name]), name)
for attr in dir(mod):
if attr.startswith('_'):
continue
fn = getattr(mod, attr)
if callable(fn):
method = getattr(fn, '__method__', None)
path = getattr(fn, '__route__', None)
if method and path:
add_route(app, fn)
我认为 'module_name' 与 class RequestHandler
有关。但我不知道这是什么意思。是不是让class RequestHandler
变成了一个模组?是这样的吗?
import RequestHandler
I can't understand the 'module_name'
module_name
是传递给名为 add_routes()
的函数的值的占位符
例如,
add_routes(a, b)
# New Definition
a is app
b is module_name
n = b.rfind('.')
...
Where it comes from?
在 awesome-python3-webapp 中,module_name
仅在 add_routes()
函数中使用。
I think the 'module_name' is related to the class RequestHandler. But
i dont't know what it means. Does it let class RequestHandler to
become a moudle? Something like this?
考虑:
# RequestHandler.py
def add_routes(app, module_name):
print(app)
print(module_name)
在这种情况下,您可以像您提到的那样通过 import
使用上面的代码:
import RequestHandler
a = foo
b = bar
RequestHandler.add_routes(a, b)
Result:
foo
bar
点击here。 在代码的 latt 中。我无法理解'module_name'。它来自哪里? 'module_name' 是什么意思?
def add_routes(app, module_name):
n = module_name.rfind('.')
if n == (-1):
mod = __import__(module_name, globals(), locals())
else:
name = module_name[n+1:]
mod = getattr(__import__(module_name[:n], globals(), locals(), [name]), name)
for attr in dir(mod):
if attr.startswith('_'):
continue
fn = getattr(mod, attr)
if callable(fn):
method = getattr(fn, '__method__', None)
path = getattr(fn, '__route__', None)
if method and path:
add_route(app, fn)
我认为 'module_name' 与 class RequestHandler
有关。但我不知道这是什么意思。是不是让class RequestHandler
变成了一个模组?是这样的吗?
import RequestHandler
I can't understand the 'module_name'
module_name
是传递给名为 add_routes()
例如,
add_routes(a, b)
# New Definition
a is app
b is module_name
n = b.rfind('.')
...
Where it comes from?
在 awesome-python3-webapp 中,module_name
仅在 add_routes()
函数中使用。
I think the 'module_name' is related to the class RequestHandler. But i dont't know what it means. Does it let class RequestHandler to become a moudle? Something like this?
考虑:
# RequestHandler.py
def add_routes(app, module_name):
print(app)
print(module_name)
在这种情况下,您可以像您提到的那样通过 import
使用上面的代码:
import RequestHandler
a = foo
b = bar
RequestHandler.add_routes(a, b)
Result:
foo
bar