Python lookup function

醉酒当歌 提交于 2020-01-07 03:41:07

问题


I want to set up a lookup function with mako. on top of the template, i have

<%!
    lookup = { 'key': function }
%>

<%def name="function()">
    Output
</%def>

so i can use it later

<%def name="body()">
    ${lookup['key']()}
</%def>

this gives me a function is not a defined error. can i get around this?

i know why it doesn't work, as it runs first, before the method gets loaded, but how else would i set this up?


回答1:


I can tell you why it isn't working, but I do not have a clean solution at this point. Your template as given compiles into this Python code:

# -*- encoding:utf-8 -*-
from mako import runtime, filters, cache
UNDEFINED = runtime.UNDEFINED
__M_dict_builtin = dict
__M_locals_builtin = locals
_magic_number = 5
_modified_time = 1285968547.0498569
_template_filename='<snip>'
_template_uri='<snip>'
_template_cache=cache.Cache(__name__, _modified_time)
_source_encoding='utf-8'
_exports = ['function']


# SOURCE LINE 1

lookup = { 'key': function }


def render_body(context,**pageargs):
    context.caller_stack._push_frame()
    try:
        __M_locals = __M_dict_builtin(pageargs=pageargs)
        __M_writer = context.writer()
        # SOURCE LINE 3
        __M_writer(u'\n\n')
        # SOURCE LINE 7
        __M_writer(u'\n')
        return ''
    finally:
        context.caller_stack._pop_frame()


def render_function(context):
    context.caller_stack._push_frame()
    try:
        __M_writer = context.writer()
        # SOURCE LINE 5
        __M_writer(u'\n    Output\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

As you can see your function has actually been defined as render_function. The Mako docs specify how to call defs from outside a template, but they do not indicate how to do this properly at runtime. The code that I have linked too simply does a lookup for "render_%s" % name (see mako.template, line 217), so you might consider just doing that.




回答2:


Maybe you could delay the lookup of function from dict-creation time to invocation time?

<%!
    lookup = { 'key': lambda: function() }
%>

I haven't used Mako, but it works in the Python shell:

>>> x = lambda: foo()
>>> x
<function <lambda> at 0x10047e050>
>>> x()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
NameError: global name 'foo' is not defined
>>> def foo():
...    print "Test"
... 
>>> x()
Test


来源:https://stackoverflow.com/questions/3842458/python-lookup-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!