Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"==" and "is" in Python
#1
Yesterday I was working with some code, and I used the "is" instead of the "==" as following:
Code:
import os

h = '[]>: '

while 1 > 0:
    p = raw_input(h)
    if (p is 'h?' or p is '--help'):
        h = '[h?]>: '
        print('\th?, --help\t\t\t-shows this list of commands\n\tpy?, --python\t\t\t-start interactive python in CLI\n\tpydle?, --python-idle\t\t\t')

    if (p is 'py?' or p is '--python'):
        h = '[py?]>: '
        os.system('python2.6')

    if (p is '--exit' or p is 'x?'):
        os.system('clear') and os.system('cls')
        print('Goodbye')
        break

    if (p is 'cs?' or p is '--clear'):
        h = '[cs?]>: '
        os.system('clear') and os.system('cls')
    if (p is 'lst?' or p == '--list'):
        h = '[lst?]>: '
        print(os.listdir('.'))
And it would just pass all of the if statements.... but when I replaced the "is" with the "==" it worked correctly. Any ideas? I thought they were the same operators?
Do what thou wilt shall be the whole of the Law. Love is the law, love under will.
.::The Rights of Man::.
Reply
#2
It seems to be the characters used in the commands(-- and ?). I guess "is" doesn't evaluate those. Here is an example using and interactive session with python.
Code:
>>> p = 'h'
>>> if p is 'h':
    print 'hi'

    
hi
>>> p = 'h?'
>>> if p is 'h?':
    print 'hi'

    
>>> p = '--h'
>>> if p is '--h':
    print 'hi'

    
>>> if p == '--h':
    print 'hi'

    
hi
>>>
I guess it's just safer to use "==".
[Image: izsyo6.jpg]


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 2,001 09-30-2019, 12:59 AM
Last Post: samsmith001
  Simple Python Python Compiler Canoris 21 9,169 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,650 12-11-2010, 04:13 PM
Last Post: Bursihido
  Python help Kharnage 2 886 02-12-2010, 09:07 PM
Last Post: Kharnage
  I Need Help with Python Kharnage 19 4,412 02-06-2010, 08:58 AM
Last Post: Kharnage

Forum Jump:


Users browsing this thread: 1 Guest(s)