Compare source code

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Nov 5 23:09:03 EDT 2010


On Fri, 05 Nov 2010 08:17:02 +0530, Rustom Mody wrote:

> However the original question -- mixing tabs and spaces is bad -- has
> got lost in the flames.  Do the most die-hard python fanboys deny this? 
> And if not is it asking too much (say in python3) that mixing tabs and
> spaces be flagged as an error or at least warning?

Mixing spaces and tabs for indentation can be ambiguous. Python 2.x will 
attempt to ignore the ambiguity, and therefore run the code if it can, 
but Python 3.x treats mixed indentation in a single block as an error.

[steve at sylar ~]$ cat ambiguous.py
def f():
        print("indented with 8 spaces")
	print("indented with tab")

f()

[steve at sylar ~]$ cat unambiguous.py
def f():
        print("indented with 8 spaces")

def g():
	print("indented with tab")

f()
g()


Python 2.x will execute both files. Python 3.x will happily execute 
unambiguous.py, but raises an error on the other:

[steve at sylar ~]$ python3 ambiguous.py
  File "ambiguous.py", line 3
    print("indented with tab")
                             ^
TabError: inconsistent use of tabs and spaces in indentation



-- 
Steven



More information about the Python-list mailing list