From: Stephen Hansen on 30 Jun 2010 14:50 On 6/30/10 11:39 AM, Stef Mientki wrote: > hello, > > I've lot of functions that returns their result in some kind of tuple / > list / array, > and if there is no result, these functions return None. > Now I'm often what to do something if I've more than 1 element in the > result. > So I test: > > if len ( Result ) > 1 : > > But to prevent exceptions, i've to write ( I often forget) > if Result and ( len ( Result ) > 1 ) : Just do: if Result: You don't have to do a length check > 1; because if Result has a length of 0, it'll be false too. So the above check will catch both None, and empty sequences. > So I wonder why len is not allowed on None > and if there are objections to extend the len function . Len is not allowed on None, becaues None is not a sequence, and doesn't have a length. None, *very* much on purpose, is distinct and does not behave like anything else. It's the "I'm not anything" object. -- ... Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
From: Stefan Behnel on 30 Jun 2010 14:57 Stef Mientki, 30.06.2010 20:39: > I've lot of functions that returns their result in some kind of tuple / list / array, > and if there is no result, these functions return None. > Now I'm often what to do something if I've more than 1 element in the result. > So I test: > > if len ( Result )> 1 : > > But to prevent exceptions, i've to write ( I often forget) > if Result and ( len ( Result )> 1 ) : > > So I wonder why len is not allowed on None > and if there are objections to extend the len function . Because getting an exception is actually a feature. Imagine a world where None would implement all sorts of protocols, such as len, getitem, getattr, etc., and would always return something that would be useful for, well, someone, maybe even a majority of "use cases". In such a world, it would actually be very easy to write buggy code that doesn't handle None values properly, simply because it's easy for programmers to forget to do so. And this would mean that code that would best drop dead early would instead silently ignore all errors and just do, well, something, which may or may not be meaningful, correct and/or useful. That would be very hard to debug code, as it would fail in obscure places that may be completely unrelated to the original problem. The current behaviour, on the other hand, will give you an exception exactly in the place where you treat the None value in an illegal way, so it will be easy for you to see what the problem is and much easier to track it down. Stefan
From: Tim Chase on 30 Jun 2010 15:02 On 06/30/2010 01:50 PM, Stephen Hansen wrote: > On 6/30/10 11:39 AM, Stef Mientki wrote: >> if len ( Result )> 1 : >> >> But to prevent exceptions, i've to write ( I often forget) >> if Result and ( len ( Result )> 1 ) : > > Just do: > > if Result: > > You don't have to do a length check> 1; because if Result has a length > of 0, it'll be false too. So the above check will catch both None, and > empty sequences. Not to counter the rest of your comment below (which is right on), the OP asked about "> 1", not "> 0" for which "if Result" would work...one character vs. more than one character (your test would be 0 vs more-than-0) >> So I wonder why len is not allowed on None >> and if there are objections to extend the len function . > > Len is not allowed on None, becaues None is not a sequence, and doesn't > have a length. None, *very* much on purpose, is distinct and does not > behave like anything else. It's the "I'm not anything" object. -tkc
From: Stephen Hansen on 30 Jun 2010 15:08 On 6/30/10 12:02 PM, Tim Chase wrote: > On 06/30/2010 01:50 PM, Stephen Hansen wrote: >> On 6/30/10 11:39 AM, Stef Mientki wrote: >>> if len ( Result )> 1 : >>> >>> But to prevent exceptions, i've to write ( I often forget) >>> if Result and ( len ( Result )> 1 ) : >> >> Just do: >> >> if Result: >> >> You don't have to do a length check> 1; because if Result has a length >> of 0, it'll be false too. So the above check will catch both None, and >> empty sequences. > > Not to counter the rest of your comment below (which is right on), the > OP asked about "> 1", not "> 0" for which "if Result" would work...one > character vs. more than one character (your test would be 0 vs more-than-0) Gah, oops. You're right. I misread, my bad. In that case yes, he's right and needs "if Result and len(Result) > 1" -- ... Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
From: Dave Angel on 30 Jun 2010 15:18
Stephen Hansen wrote: > On 6/30/10 11:39 AM, Stef Mientki wrote: >> hello, >> >> I've lot of functions that returns their result in some kind of tuple / >> list / array, >> and if there is no result, these functions return None. >> Now I'm often what to do something if I've more than 1 element in the >> result. >> So I test: >> >> if len ( Result ) > 1 : >> >> But to prevent exceptions, i've to write ( I often forget) >> if Result and ( len ( Result ) > 1 ) : > > Just do: > > if Result: > > You don't have to do a length check > 1; because if Result has a > length of 0, it'll be false too. So the above check will catch both > None, and empty sequences. > <snip> Look closer: the OP wanted len(Result) > 1 not len(Result) > 0. For that, you need two checks, for example, as for example: if Result and (len(Result)>1): .. DaveA |