re.search when used within an if/else fails

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Nov 28 16:39:03 EST 2012


On Wed, 28 Nov 2012 14:08:15 -0600, Evan Driscoll wrote:

> I'm only entering this thread now so I'm not really in context, but
> you're correct; in Python 3, -tt is set by default, which makes illegal
> mixing an error.
> 
> However, not all mixing is illegal: what it means by "code that
> illegally mixes tabs and spaces" is "code whose interpretation differs
> depending upon the interpretation of the tab width". While I'm not going
> to go test it, I think that if you, say, indent from the first to the
> second level with tabs (consistently), indent from the second to third
> level with spaces (consistently), and indent from the third to fourth
> level with tabs (consistently), it should not complain. 

Correct, which disappoints me. Testing with Python 3:

py> if True:
...     if True: # tab
...         pass  # tab, then four spaces
...
py>

I would prefer that the "pass" line would fail with an illegal indent, 
but it does not. But at least the following fails cleanly:


py> if True:
...     if True: # tab
...         pass  # tab, then four spaces
...         pass  # two spaces, tab, four spaces
  File "<stdin>", line 4
    pass  # two spaces, tab, four spaces
                                       ^
TabError: inconsistent use of tabs and spaces in indentation


> Or perhaps I
> should say "it should complain that you're a bad person and should feel
> bad, but it won't." :-) (In fact, you could indent one block at the
> second level with tabs and another with spaces.)

I don't mind different blocks using different indentation. You have 
always been able to do this:


py> if True:
...         pass  # eight spaces
...
py> if True:
...   pass  # two spaces
...
py>

If you don't like that, don't do it! Consistent indentation globally per 
file is a matter for coding conventions. However, I think that within a 
single block, Python should enforce "all tabs" or "all spaces".

Perhaps it would be nice if Python honoured a directive setting indent 
style to spaces or indents, as it honours source code encoding lines:

# -*- indent: <mode> -*-

Where <mode> could be one of:

space[s]	Only accept spaces in indentation
tab[s]		Only accept tabs in indentation
mixed		Accept "mixed" tabs and spaces, but only if consistent

with mixed the default for backward compatibility.



-- 
Steven



More information about the Python-list mailing list