问题
Recently we found a couple of bugs in our code based because a developer forgot to add a comma in the middle of a list of strings and python just concatenated the strings. look below:
The intended list was: ["abc", "def"]
Developer wrote: ["abc" "def"]
and we got: ["abcdef"]
now I am concerned over similar mistakes in other part of the code, is this functionality a core part of python? is it possible to disable it?
回答1:
Yes, this is a core part of python:
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"
.
I don't think there is a way to disable it, short of hacking Python itself.
However, you could use the script below to tokenize your code and warn you when it finds multiple adjacent strings:
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)
yields
ADJACENT STRINGS: 'hello''world'
ADJACENT STRINGS: for z in ('foo' 'bar', 'baz'):
ADJACENT STRINGS: x = ["abc" "def"]
来源:https://stackoverflow.com/questions/26026964/is-there-any-way-to-stop-python-to-concatanate-space-delimited-strings