None in string = TypeError?

2014-06-09 Thread Roy Smith
We noticed recently that: None in 'foo' raises (at least in Python 2.7) TypeError: 'in string' requires string as left operand, not NoneType This is surprising. The description of the 'in' operatator is, 'True if an item of s is equal to x, else False '. From that, I would assume it

Re: None in string = TypeError?

2014-06-09 Thread Ryan Hiebert
On Mon, Jun 9, 2014 at 10:34 AM, Roy Smith r...@panix.com wrote: We noticed recently that: None in 'foo' raises (at least in Python 2.7) TypeError: 'in string' requires string as left operand, not NoneType This is surprising. It's the same in 3.4, and I agree that it's surprising, at

Re: None in string = TypeError?

2014-06-09 Thread Ian Kelly
On Mon, Jun 9, 2014 at 9:34 AM, Roy Smith r...@panix.com wrote: We noticed recently that: None in 'foo' raises (at least in Python 2.7) TypeError: 'in string' requires string as left operand, not NoneType This is surprising. The description of the 'in' operatator is, 'True if an item

Re: None in string = TypeError?

2014-06-09 Thread Paul Sokolovsky
Hello, On Mon, 9 Jun 2014 08:34:42 -0700 (PDT) Roy Smith r...@panix.com wrote: We noticed recently that: None in 'foo' raises (at least in Python 2.7) TypeError: 'in string' requires string as left operand, not NoneType This is surprising. The description of the 'in' operatator

Re: None in string = TypeError?

2014-06-09 Thread MRAB
On 2014-06-09 16:34, Roy Smith wrote: We noticed recently that: None in 'foo' raises (at least in Python 2.7) TypeError: 'in string' requires string as left operand, not NoneType This is surprising. The description of the 'in' operatator is, 'True if an item of s is equal to x, else

Re: None in string = TypeError?

2014-06-09 Thread Steven D'Aprano
On Mon, 09 Jun 2014 08:34:42 -0700, Roy Smith wrote: We noticed recently that: None in 'foo' raises (at least in Python 2.7) That goes back to at least Python 1.5, when member tests only accepted a single character, not a substring: None in abc Traceback (innermost last): File

Re: None in string = TypeError?

2014-06-09 Thread Shiyao Ma
2014-06-09 23:34 GMT+08:00 Roy Smith r...@panix.com: We noticed recently that: None in 'foo' raises (at least in Python 2.7) TypeError: 'in string' requires string as left operand, not NoneType This is surprising. The description of the 'in' operatator is, 'True if an item of s is

Re: None in string = TypeError?

2014-06-09 Thread Steven D'Aprano
On Mon, 09 Jun 2014 18:57:28 +0300, Paul Sokolovsky wrote: Hello, On Mon, 9 Jun 2014 08:34:42 -0700 (PDT) Roy Smith r...@panix.com wrote: We noticed recently that: None in 'foo' raises (at least in Python 2.7) TypeError: 'in string' requires string as left operand, not NoneType

Re: None in string = TypeError?

2014-06-09 Thread Chris Angelico
On Tue, Jun 10, 2014 at 2:14 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: This is very Pythonic, Python is strictly typed language. There's no way None could possibly be inside a string, Then `None in some_string` could immediately return False, instead of raising an

Re: None in string = TypeError?

2014-06-09 Thread Roy Smith
On Jun 9, 2014, at 11:57 AM, Paul Sokolovsky wrote: This is very Pythonic, Python is strictly typed language. There's no way None could possibly be inside a string, so if you're trying to look for it there, you're doing something wrong, and told so. Well, the code we've got is:

Re: None in string = TypeError?

2014-06-09 Thread Chris Angelico
On Tue, Jun 10, 2014 at 2:53 AM, Roy Smith r...@panix.com wrote: In retrospect, I suspect: hourly_data = [(t if status in set('CSRP') else None) for (t, status) in hours] is a little cleaner. I'd go with this. It's clearer that a status of 'SR' should result in False, not True.

Re: None in string = TypeError?

2014-06-09 Thread Ian Kelly
On Mon, Jun 9, 2014 at 10:59 AM, Chris Angelico ros...@gmail.com wrote: On Tue, Jun 10, 2014 at 2:53 AM, Roy Smith r...@panix.com wrote: In retrospect, I suspect: hourly_data = [(t if status in set('CSRP') else None) for (t, status) in hours] is a little cleaner. I'd go with

Re: None in string = TypeError?

2014-06-09 Thread Chris Angelico
On Tue, Jun 10, 2014 at 3:22 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Jun 9, 2014 at 10:59 AM, Chris Angelico ros...@gmail.com wrote: On Tue, Jun 10, 2014 at 2:53 AM, Roy Smith r...@panix.com wrote: In retrospect, I suspect: hourly_data = [(t if status in set('CSRP') else

Re: None in string = TypeError?

2014-06-09 Thread Ian Kelly
On Mon, Jun 9, 2014 at 11:40 AM, Chris Angelico ros...@gmail.com wrote: Also, this is the first time I've seen None as a constant other than the first. Usually co_consts[0] is None, but this time co_consts[4] is None. Functions always seem to have None as the first constant, but modules and

Re: None in string = TypeError?

2014-06-09 Thread Chris Angelico
On Tue, Jun 10, 2014 at 3:58 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Jun 9, 2014 at 11:40 AM, Chris Angelico ros...@gmail.com wrote: Also, this is the first time I've seen None as a constant other than the first. Usually co_consts[0] is None, but this time co_consts[4] is None.