"is not" vs. != in python

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

"is not" vs. != in python

Post by ArgentPyro on Sun Mar 08, 2009 1:15 pm
([msg=19564]see "is not" vs. != in python[/msg])

I'm currently working on optimizing some python code, and was wondering which runs faster, "is not" or != or does it not matter:

Code: Select all
n = 10

if n is not 5:
print "Done"

if n != 5:
print "Done"


Thanks.
Image
ArgentPyro
New User
New User
 
Posts: 10
Joined: Mon Feb 09, 2009 5:28 pm
Blog: View Blog (0)


Re: "is not" vs. != in python

Post by tgoe on Mon Mar 09, 2009 12:12 pm
([msg=19651]see Re: "is not" vs. != in python[/msg])

I think the 'is' operator is probably faster as all it does is check whether the objects compared have the same address in memory. It doesn't look at values at all so you have to be careful:
Code: Select all
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a is b
False
>>> a == b
True
User avatar
tgoe
Contributor
Contributor
 
Posts: 668
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: "is not" vs. != in python

Post by ArgentPyro on Mon Mar 09, 2009 1:51 pm
([msg=19660]see Re: "is not" vs. != in python[/msg])

thanks
Image
ArgentPyro
New User
New User
 
Posts: 10
Joined: Mon Feb 09, 2009 5:28 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests