0

I have the following code:

#!/usr/bin/python3

import configparser

config = configparser.ConfigParser()

config.read('bindslash.ini')

print([name for name in config.sections() if name is not 'bindslash'])

bindslash.ini looks like this:

[bindslash]

[somethingelse]

Running this program produces ['bindslash', 'somethingelse']. However, if I change the is not in the list comprehension to be != instead, the program correctly produces ['somethingelse']. As far as I understand it, these should be the exact same thing. Why isn't is not working?

I'm on Python 3.5.0, installed from Homebrew on OS X.

strugee
  • 2,752
  • 4
  • 19
  • 30
  • 2
    Try `print([name for name in config.sections() if name != 'bindslash'])`. `is not` is not `!=`. – Remi Guan Nov 28 '15 at 05:20
  • 1
    `is` checks for object identity, not object equality. Two strings that are equal may or may not be the same object. – Peter DeGlopper Nov 28 '15 at 05:30
  • 1
    @KevinGuan yeah, I tried your version, and it worked. I couldn't figure out why. that's what the question was about :) – strugee Nov 28 '15 at 06:30

0 Answers0