idiomatic way to collect and report multiple exceptions?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri May 7 21:11:59 EDT 2010


On Fri, 07 May 2010 14:28:05 -0700, Aahz wrote:

> In article <mailman.2715.1273204222.23598.python-list at python.org>, Ben
> Cohen  <ncohen at ucsd.edu> wrote:
>>
>>eg -- I'd like to do something like this:
>>
>>errors = []
>>for item in data:
>>    try:
>>        process(item)
>>    except ValidationError as e:
>>        errors.append(e)
>>raise MultipleValidationErrors(*errors)
> 
> First of all, please DO NOT post code with TABs in it.  In fact, you
> shouldn't even be writing code with TABs -- TAB characters are obsolete
> for new Python code.

I don't believe this is correct. Do you have a source for this?

Four spaces are recommended by PEP 8, and are compulsory for patches to 
the standard library, but Python continues to accept either multi-space 
indents or tabs, and in your own personal code you can use whichever you 
like.

The documentation for Python 3 continues to state that either spaces or 
tabs will be accepted (and warns against mixing them):

http://docs.python.org/py3k/reference/lexical_analysis.html#indentation



According to my tests, Python 3.1 continues to accept both spaces and 
tabs. Tabs on their own are accepted without warning:


[steve at sylar ~]$ cat tabs.py
def f(x):
	# This function uses tabs for indents.
	return x


print(f("tabs"))

[steve at sylar ~]$ python3.1 tabs.py
tabs


Even mixing tabs and spaces in the one indent is allowed:

[steve at sylar ~]$ cat mixed.py
def f(x):
    	# This uses mixed tabs and spaces.
    	return x


print(f("mixed tabs and spaces"))

[steve at sylar ~]$ python3.1 mixed.py
mixed tabs and spaces


But using both tab-based indents and space-based indents is not:

[steve at sylar ~]$ cat both.py
def f(x):
        # This uses both tabs and spaces.
        pass  # space-indent
	return x  # tab-indent


print(f("both tabs and spaces"))

[steve at sylar ~]$ python3.1 both.py
  File "both.py", line 4
    return x  # tab-indent
                         ^
TabError: inconsistent use of tabs and spaces in indentation




-- 
Steven



More information about the Python-list mailing list