[Python-ideas] why not try without except?

spir denis.spir at free.fr
Mon Apr 27 11:51:47 CEST 2009


[off list]

Le Sun, 26 Apr 2009 22:36:57 +0200,
Georg Brandl <g.brandl at gmx.net> s'exprima ainsi:

> > This is another story, indeed, but I see it 
> > closely related at the meaning level.
> >     searched = seq.find(....)	# may not return anything, not even
> > None  
> 
> What is "searched" supposed to be, then?  "Not anything" is not an option.

Raise an exception, obviously (like I showed before). 

Either None [or any or word you like (nil?)] is, say "non-assignable" or rather non-usable in any kind of expression and get NoneError because you tried to use it.
Or the func does not return anything, literally (like a pascal procedure), and you get NoReturnValueError for trying to use it.

The only safe alternative is to have 2 kinds of funcs: action-funcs and production-funcs. My workarouns is to use always verbs for actions and nouns (naming the result) for productions. This issue is that in english the same word is often both a noun and a verb ;-) (moral: flexibility is not only an advantage)

There is side-topic you evoke in your reply:
<< If you don't like the ambiguity between "procedures" returning None and "functions" returning None, define your own "not-valid" value and return it from functions. >>

Actually, this was not my point as you can see above, still this issue also exists. But we have the proper solution for it. A production-func is intended to normally return a result; if it cannot do it, we face an "exceptional case" so just throw an exception to handle it.
This is indeed very different of the issue caused by the fact that we have no mean to distinguish action-funcs. Code using or assigning the None result will run free, often fail somewhere else, thus causing usually hard-to-find bugs. You know it, for sure.
The reason is that None is assignable. Consequentely, a func should not return it if not explicitely told to.

Denis
------
la vita e estrany



More information about the Python-ideas mailing list