Multiple adjacent string literals (delimited by whitespace), possibly
using different quoting conventions, are allowed, and their meaning is
the same as their concatenation. Thus, "hello" 'world' is equivalent
to "helloworld".
import tokenize
import token
import io
import collections
class Token(collections.namedtuple('Token', 'num val start end line')):
@property
def name(self):
return token.tok_name[self.num]
def check(codestr):
lastname = None
for tok in tokenize.generate_tokens(io.BytesIO(codestr).readline):
tok = Token(*tok)
if lastname == 'STRING' and lastname == tok.name:
print('ADJACENT STRINGS: {}'.format(tok.line.rstrip()))
else:
lastname = tok.name
codestr = '''
'hello'\
'world'
for z in ('foo' 'bar', 'baz'):
x = ["abc" "def"]
y = [1, 2, 3]
'''
check(codestr)
收益率
ADJACENT STRINGS: 'hello''world'
ADJACENT STRINGS: for z in ('foo' 'bar', 'baz'):
ADJACENT STRINGS: x = ["abc" "def"]
是的,这是core part of python:
我不认为有一种方法可以禁用它,除了破解Python本身。你知道吗
但是,您可以使用下面的脚本tokenize您的代码,并在发现多个相邻字符串时发出警告:
收益率
相关问题 更多 >
编程相关推荐