我使用cherrypy
和mako
與Python創建網站。減少Lambda錯誤:'Undefined'對象不可調用
在我的HTML文件中的一個,我有以下lambda函數:
${reduce(lambda list, cur : '<li>' + someFunction(cur) + '</li>' +
list, object.listing, '')}
哪裏object.listing
是字符串列表。
在提到的錯誤中執行結果:'未定義'對象不可調用。如果我刪除someFunction()
,錯誤不會發生。所以我認爲它與cur
是None
或Undefined
有關,但我不知道如何防止這種情況。
我甚至不太確定,reduce函數是如何工作的。
整個堆棧跟蹤,如要求:
[26/Dec/2012:13:15:22] HTTP
Request Headers:
REFERER: http://localhost:8080/list
HOST: localhost:8080
CONNECTION: keep-alive
Remote-Addr: 127.0.0.1
ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
USER-AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0
COOKIE: session_id=960e569568ded77610f11c48bec0a806f5a4501b
ACCEPT-LANGUAGE: en-US,en;q=0.5
DNT: 1
ACCEPT-ENCODING: gzip, deflate
[26/Dec/2012:13:15:22] HTTP Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "/Volumes/Ritalin/Projects/splitpot/controller/Splitpot.py", line 54, in event
return tmpl.render(object=doSomething())
File "/Library/Python/2.7/site-packages/mako/template.py", line 412, in render
return runtime._render(self, self.callable_, args, data)
File "/Library/Python/2.7/site-packages/mako/runtime.py", line 766, in _render
**_kwargs_for_callable(callable_, data))
File "/Library/Python/2.7/site-packages/mako/runtime.py", line 798, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/Library/Python/2.7/site-packages/mako/runtime.py", line 824, in _exec_template
callable_(context, *args, **kwargs)
File "base_html", line 31, in render_body
File "someFile_html", line 58, in render_body
File "someFile_html", line 58, in <lambda>
TypeError: 'Undefined' object is not callable
您應該使用stacktrace顯示整個錯誤,以便我們更容易診斷。 –
向我們展示你的'someFunction'。你的例子中有什麼「cur」? – alexvassel