语法怪癖或为什么那个有效的python [英] Syntax quirks or why is that valid python

查看:27
本文介绍了语法怪癖或为什么那个有效的python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 2.6 中,为什么以下行有效?

In python 2.6, why is the following line valid?

my_line = 'foo' 'bar'

如果这是有效的,为什么不是以下内容:

and if that is valid, why isn't the following:

my_list = 1 2 

第一个例子是字符串连接,但是,以下也无效(感谢上帝):

The first example is string concatenation, however, the following isn't valid either (thanks god):

foo = 'foo'
bar = 'bar'
foo_bar = foo bar

推荐答案

这是在做 字符串文字连接.如文档中所述,优点包括:

This is doing string literal concatenation. As noted in the documentation, advantages include the following:

此功能可用于减少拆分所需的反斜杠数量长字符串方便地穿过长行,甚至添加注释部分字符串...

This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings...

继续注意,这种连接是在编译时而不是运行时完成的.

It goes on to note that this concatenation is done at compilation time rather than run time.

这背后的历史和理由,以及被拒绝的删除该功能的建议,在PEP 3126.

The history and rationale behind this, and a rejected suggestion to remove the feature, is described in PEP 3126.

这篇关于语法怪癖或为什么那个有效的python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆