list comprehension problem

MRAB python at mrabarnett.plus.com
Thu Oct 29 12:09:12 EDT 2009


Diez B. Roggisch wrote:
> mk wrote:
> 
>> Hello everyone,
>>
>> print hosts
>> hosts = [ s.strip() for s in hosts if s is not '' and s is not None and
>> s is not '\n' ]
>> print hosts
>>
>> ['9.156.44.227\n', '9.156.46.34 \n', '\n']
>> ['9.156.44.227', '9.156.46.34', '']
>>
>> Why does the hosts list after list comprehension still contain '' in
>> last position?
>>
>> I checked that:
>>
>> print hosts
>> hosts = [ s.strip() for s in hosts if s != '' and s != None and s != '\n'
>> ] print hosts
>>
>> ..works as expected:
>>
>> ['9.156.44.227\n', '9.156.46.34 \n', '\n']
>> ['9.156.44.227', '9.156.46.34']
>>
>>
>> Are there two '\n' strings in the interpreter's memory or smth so the
>> identity check "s is not '\n'" does not work as expected?
>>
>> This is weird. I expected that at all times there is only one '\n'
>> string in Python's cache or whatever that all labels meant by the
>> programmer as '\n' string actually point to. Is that wrong assumption?
> 
> Yes. Never use "is" unless you know 100% that you are talking about the same
> object, not just equality.
> 
Some objects are singletons, ie there's only ever one of them. The most
common singleton is None. In virtually every other case you should be
using "==" and "!=".



More information about the Python-list mailing list