Hwy doesn't len(None) return zero ?
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 ) : So I wonder why len is not allowed on None and if there are objections to extend the len function . thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
On Thu, Jul 1, 2010 at 12:09 AM, Stef Mientki stef.mien...@gmail.comwrote: 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 ) : use if Result: do something Checking the length would be a bad idea. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
On 06/30/2010 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 ) : So I wonder why len is not allowed on None and if there are objections to extend the len function . thanks, Stef Mientki Because the natural interpretation of len only makes sense for concepts such as a container or collection. The value None is no such thing. Assigning a meaning to len(None) begs the question of meanings for len(True), len(False), len(3.14), len(sys), ... This is a slippery slope, best avoided. But there are solutions: 1. Have your functions return [] or () or whatever.If they are to return a list, and the list may be empty, [] is correct. 2. If you insist on a function returning a list sometimes and other values at other times (such as None), then be prepared to write your code which uses the result with test to determine which type was returned. Fortunately that's not hard, as your one example shows. 3. Create a test function Empty(Result) which does what you want returning a boolean and write your tests as: if Empty(Result): ... Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
On Wed, Jun 30, 2010 at 12:39 PM, Stef Mientki stef.mien...@gmail.com wrote: So I wonder why len is not allowed on None and if there are objections to extend the len function . For the same reason that (None + 42) doesn't return 42, and that (None.upper()) doesn't return NONE. -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
On 6/30/2010 11:39 AM Stef Mientki said... 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: which works fine if beforehand you do Result = presumedFuncCall() or [] particularly if you want to test len subsequently. Emile 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 . thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
Please pardon me for breaking threading, but Stef's original post has not come through to me. 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. Well there's your problem right there. If you have a function that returns a list of X, and there are no X to return, you should return an empty list, not None. filter(lambda n: n%2 == 0, [1, 3, 5, 7]) # return even numbers [] There are good use-cases for functions that sometimes return X and sometimes return None, but they're rare. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Hwy doesn't len(None) return zero ?
On 30-06-2010 20:56, Gary Herron wrote: On 06/30/2010 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 ) : So I wonder why len is not allowed on None and if there are objections to extend the len function . thanks, Stef Mientki Because the natural interpretation of len only makes sense for concepts such as a container or collection. The value None is no such thing. Assigning a meaning to len(None) begs the question of meanings for len(True), len(False), len(3.14), len(sys), ... This is a slippery slope, best avoided. But there are solutions: 1. Have your functions return [] or () or whatever.If they are to return a list, and the list may be empty, [] is correct. thanks guys, I think that will be the best idea. cheers, Stef 2. If you insist on a function returning a list sometimes and other values at other times (such as None), then be prepared to write your code which uses the result with test to determine which type was returned. Fortunately that's not hard, as your one example shows. 3. Create a test function Empty(Result) which does what you want returning a boolean and write your tests as: if Empty(Result): ... Gary Herron -- http://mail.python.org/mailman/listinfo/python-list