When I was running the following code, which has blank lines (no space) inside functions, I got different behavior from Python 3.6.5 when running the code line by line in interpreter mode and python3 split.py
:
# File split.py
def find(s, start, predictor):
for i in range(start, len(s)):
if predictor(s[i]):
return i
return -1
def find_all(s, sep=" \t\n"):
beg_of_nonsep = 0
while beg_of_nonsep < len(s):
beg_of_nonsep = find(s, beg_of_nonsep, lambda ch, sep_chs=sep: ch not in sep_chs)
if beg_of_nonsep == -1:
break
end_of_nonsep = find(s, beg_of_nonsep + 1, lambda ch, sep_chs=sep: ch in sep_chs)
if end_of_nonsep == -1:
end_of_nonsep = len(s)
yield (beg_of_nonsep, end_of_nonsep)
beg_of_nonsep = end_of_nonsep + 1
split = lambda s: [s[beg: end] for (beg, end) in find_all(s)]
print(split(""))
print(split(" \t\n"))
print(split(" \tssss\n"))
When running the code line by line in interpreter mode, python3
gave me nasty errors:
Type "help", "copyright", "credits" or "license" for more information.
>>> def find(s, start, predictor):
... for i in range(start, len(s)):
... if predictor(s[i]):
... return i
... return -1
...
>>> def find_all(s, sep=" \t\n"):
... beg_of_nonsep = 0
... while beg_of_nonsep < len(s):
... beg_of_nonsep = find(s, beg_of_nonsep, lambda ch, sep_chs=sep: ch not in sep_chs)
... if beg_of_nonsep == -1:
... break
...
>>> end_of_nonsep = find(s, beg_of_nonsep + 1, lambda ch, sep_chs=sep: ch in sep_chs)
File "<stdin>", line 1
end_of_nonsep = find(s, beg_of_nonsep + 1, lambda ch, sep_chs=sep: ch in sep_chs)
^
IndentationError: unexpected indent
>>> if end_of_nonsep == -1:
File "<stdin>", line 1
if end_of_nonsep == -1:
^
IndentationError: unexpected indent
>>> end_of_nonsep = len(s)
File "<stdin>", line 1
end_of_nonsep = len(s)
^
IndentationError: unexpected indent
>>>
>>> yield (beg_of_nonsep, end_of_nonsep)
File "<stdin>", line 1
yield (beg_of_nonsep, end_of_nonsep)
^
IndentationError: unexpected indent
>>>
>>> beg_of_nonsep = end_of_nonsep + 1
File "<stdin>", line 1
beg_of_nonsep = end_of_nonsep + 1
^
IndentationError: unexpected indent
>>>
>>> split = lambda s: [s[beg: end] for (beg, end) in find_all(s)]
>>>
>>> print(split(""))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: 'NoneType' object is not iterable
>>> print(split(" \t\n"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: 'NoneType' object is not iterable
>>> print(split(" \tssss\n"))
And the last print
here never quit until I used ctrlc to stop it.
Thus, I thought there were many bugs with my code.
However, when I ran the code with python3 split.py
, none of this happened:
[]
[]
['ssss']
This is rather confusing to me.
To be clear, I was actually using vimcmdline on Debian 9.8 with vim 8.1.
I am also using pylint
through pymode
, which split any trailing space, of which it considered superfluous.
This problem happened when I tried to use its builtin keybinding to send split.py
to the python3
in the tmux
split it opened.
I have already filled an issue, but I can't help wonder why python3
behaves like this.