[Python-ideas] Default return values to int and float

Steven D'Aprano steve at pearwood.info
Wed Oct 5 01:13:40 CEST 2011


Carl Matthew Johnson wrote:

> This reminds me of the string.index vs. string.find discussion we had
> a while back. In basically any situation where an exception can be
> raised, it's sometimes nice to return a None-like value and sometimes
> nice to have an out-of-band exception. I have a certain amount of
> admiration for the pattern in Go of returning (value, error) from
> most functions that might have an error, but for Python as it is
> today, there's no One Obvious Way to Do It yet, and there's probably
> none forthcoming. 

I beg to differ. Raising an exception *is* the One Obvious Way in 
Python. But OOW does not mean "Only One Way", and the existence of raise 
doesn't mean that there can't be a Second Not-So-Obvious Way, such as 
returning a "not found" value.

However, returning None as re.match does is better than returning -1 as 
str.find does, as -1 can be mistaken for a valid result but None can't be.



-- 
Steven



More information about the Python-ideas mailing list