【问题标题】:SyntaxError: unexpected character after line continuation character using .format(\*\*vars())SyntaxError:使用 .format(\*\*vars()) 的行继续字符后出现意外字符
【发布时间】:2013-05-06 17:34:02
【问题描述】:

我开始使用我的第一个cherrypy 应用程序。我正在使用文档中的示例http://docs.cherrypy.org/dev/progguide/REST.html

import cherrypy

class Resource(object):

    def __init__(self, content):
        self.content = content

    exposed = True

    def GET(self):
        return self.to_html()

    def PUT(self):
        self.content = self.from_html(cherrypy.request.body.read())

    def to_html(self):
        html_item = lambda (name,value): '<div>{name}:{value}</div>'.format(\*\*vars())
        items = map(html_item, self.content.items())
        items = ''.join(items)
        return '<html>{items}</html>'.format(**vars())

    @staticmethod
    def from_html(data):
        pattern = re.compile(r'\<div\>(?P<name>.*?)\:(?P<value>.*?)\</div\>')
        items = [match.groups() for match in pattern.finditer(data)]
        return dict(items)

class ResourceIndex(Resource):
    def to_html(self):
        html_item = lambda (name,value): '<div><a href="{value}">{name}</a></div>'.format(\*\*vars())
        items = map(html_item, self.content.items())
        items = ''.join(items)
        return '<html>{items}</html>'.format(**vars())

class Root(object):
    pass

root = Root()

root.sidewinder = Resource({'color': 'red', 'weight': 176, 'type': 'stable'})
root.teebird = Resource({'color': 'green', 'weight': 173, 'type': 'overstable'})
root.blowfly = Resource({'color': 'purple', 'weight': 169, 'type': 'putter'})
root.resource_index = ResourceIndex({'sidewinder': 'sidewinder', 'teebird': 'teebird', 'blowfly': 'blowfly'})

conf = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 8000,
    },
    '/': {
        'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    }
}

cherrypy.quickstart(root, '/', conf)

lambda 方法在以下行出现错误:

html_item = lambda (name,value): '<div>{name}:{value}</div>'.format(\*\*vars())

并在阅读Python 3.2 Lambda Syntax Error后改为如下

html_item = lambda nv: '<div>{nv[0]}:{nv[1]}</div>'.format(\*\*vars())

但现在我收到“SyntaxError: unexpected character after line continuation character”链接到上述行的末尾.format(\*\*vars())

是什么原因造成的?

我正在运行 Python 3.2 和 CherryPy 3.2.2

【问题讨论】:

    标签: python python-3.x cherrypy


    【解决方案1】:

    删除反斜杠。关键字参数扩展的正确语法是双星号:

    html_item = lambda nv: '<div>{nv[0]}:{nv[1]}</div>'.format(**vars())
    

    这看起来像是 CherryPy 文档渲染中的错误。

    \ 反斜杠(外部字符串文字)用于表示行继续,并且只允许在行的 end 处告诉 Python 忽略换行符:

    somevar = some_function_call(arg1, arg2) + \
              some_other_function_call(arg3, arg4)
    

    不建议使用该语法(您应该使用括号代替),但这是 Python 期望在此处看到的而不是星号。

    显示异常的快速演示确实是由反斜杠引起的:

    >>> test = dict(foo='bar')
    >>> '{foo}'.format(\*\*test)
      File "<stdin>", line 1
        '{foo}'.format(\*\*test)
                               ^
    SyntaxError: unexpected character after line continuation character
    >>> '{foo}'.format(**test)
    'bar'
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多