Reputation: 6034
Maybe I'm just too tired to see it, but why does this
cmds = '''
AA ''' + ''' BB
'''.splitlines()
result in
Traceback (most recent call last):
File "<pyshell#15>", line 3, in <module>
'''.splitlines()
TypeError: Can't convert 'list' object to str implicitly
and this works just fine:
cmds = '''
AA ''' + ''' BB
'''
print(cmds.splitlines())
?
Upvotes: 1
Views: 899
Reputation: 7377
You first example is splitting the second string before trying to join it to the first string. The second example is joining the two strings first, then splitting it.
Upvotes: 0
Reputation: 2559
It is the order in which operations are done. In
cmds = '''
AA ''' + ''' BB
'''.splitlines()
the splitlines()
successfully splits the second string
''' BB
'''
but then it tries to concatenate the result (a list) to the first string
'''
AA '''
which doesn't make sense. In the second snippet, the concatenations happens first, and then splitlines
works just fine.
Upvotes: 0
Reputation: 59158
The splitlines
method is getting called before the addition, so this:
cmds = '''
AA ''' + ''' BB
'''.splitlines()
... is equivalent to this:
cmds = ('''
AA ''') + (''' BB
'''.splitlines())
... which means you're trying to add a list to a string.
However, in the second case:
cmds = '''
AA ''' + ''' BB
'''
print(cmds.splitlines())
... you're doing the addition first, and calling splitlines
on the result.
Upvotes: 3