when to use 'is not' and !=

42 views
Skip to first unread message

likage

unread,
Jul 25, 2016, 12:45:38 PM7/25/16
to Python Programming for Autodesk Maya
I am wondering if anyone can tell me when it is best to use is or is not over == or !=

I have this line of code:
if (rx != None) and (ry != None) and (rz != None):

and I am told that it will be better to write it this way instead:
if (rx is not None) and (ry is not None) and (rz is not None):

Both method will gives the same result not?
But why is/is not preferred over !=/==?


Andres Weber

unread,
Jul 25, 2016, 3:51:33 PM7/25/16
to Python Programming for Autodesk Maya
Pretty succinct explanation over here which covers it nicely.  Mainly one is just actual equality versus class/instance identity.  Try not to use is not for things where you're trying to compare direct values.
http://stackoverflow.com/questions/2209755/python-operation-vs-is-not

fruity

unread,
Jul 25, 2016, 5:06:11 PM7/25/16
to Python Programming for Autodesk Maya
My answer is maybe out of topic, but you could also use if or if not, which makes things easier to read, i think (although it's probably a personal point of view). I don't know if there is a big difference in python between != and is not, i guess they probably call a very similar code under the hood, but i do know that it makes a huge difference to have a code easy to read and to maintain ! So maybe you should just go for the easier to maintain instead of going for the faster. If you want to do faster code, maybe python is not the best choice.
I find 
if rx and ry and rz:
easier to read than 
if rx is not None and ry is not None and rz is not None
in your example, you could even do :
if all(x for x in (rx, ry, rz)):
    print 'do things'
that you can extrapolate to :
if all((rx, ry, rz)):
    print 'do things'
(i didn't test it, so i may have done some errors, but you get the idea ;-) 

What is great with python is that if you think something would be easier to read with the syntax you have in mind, this syntax probably works !
And anyway, i am not a developer, so maybe what i say is complete bullshit, so is anyone has a different point of view, feel free to answer ^_^

Justin Israel

unread,
Jul 25, 2016, 6:36:56 PM7/25/16
to python_in...@googlegroups.com
On Tue, Jul 26, 2016 at 9:06 AM fruity <fruit...@gmail.com> wrote:
My answer is maybe out of topic, but you could also use if or if not, which makes things easier to read, i think (although it's probably a personal point of view). I don't know if there is a big difference in python between != and is not, i guess they probably call a very similar code under the hood,

Andres answer actually addresses the fact that they are different and don't call the same code. There is a distinction between the two syntaxes.
 
but i do know that it makes a huge difference to have a code easy to read and to maintain ! So maybe you should just go for the easier to maintain instead of going for the faster. If you want to do faster code, maybe python is not the best choice.
I find 
if rx and ry and rz:
easier to read than 
if rx is not None and ry is not None and rz is not None
in your example, you could even do :
if all(x for x in (rx, ry, rz)):
    print 'do things'
that you can extrapolate to :
if all((rx, ry, rz)):
    print 'do things'
(i didn't test it, so i may have done some errors, but you get the idea ;-) 

There are cases where it might be necessary to explicitely say "is None", and this could actually be one of those cases. Consider that rx, ry, and rz can have a valid value of 0. Using the "if rx" syntax would do a bool test against the value. If 0 is valid, then this would evaluate to False. But you could have initialized those values to None to determine if a user had ever actually set them in the first place.

def evaluate(rx=None):
    if rx is None:
        return someDefaultOperation()
    someSpecificOperation(rx)

Same goes for passing the values into all(). This will bool test the value of each element. Your all() approach might have to be modified to something like this, if None is distinct from 0:

if all(v is not None for v in [rx, ry, rz]):
    ...

This may or may not be more readable depending on how long the list if elements may be. 

 

What is great with python is that if you think something would be easier to read with the syntax you have in mind, this syntax probably works !
And anyway, i am not a developer, so maybe what i say is complete bullshit, so is anyone has a different point of view, feel free to answer ^_^

Le lundi 25 juillet 2016 20:51:33 UTC+1, Andres Weber a écrit :
Pretty succinct explanation over here which covers it nicely.  Mainly one is just actual equality versus class/instance identity.  Try not to use is not for things where you're trying to compare direct values.
http://stackoverflow.com/questions/2209755/python-operation-vs-is-not



On Monday, July 25, 2016 at 12:45:38 PM UTC-4, likage wrote:
I am wondering if anyone can tell me when it is best to use is or is not over == or !=

I have this line of code:
if (rx != None) and (ry != None) and (rz != None):

and I am told that it will be better to write it this way instead:
if (rx is not None) and (ry is not None) and (rz is not None):

Both method will gives the same result not?
But why is/is not preferred over !=/==?


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/c49f3041-9f11-47e1-995c-e6eef0515d5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cesar Saez

unread,
Jul 26, 2016, 8:56:20 AM7/26/16
to python_in...@googlegroups.com
Also empty containers bool test to false, so if you ever implement  __iter__ and __len__ in one of your base classes the bool test will probably give you unexpected results.

tl;dr:
- is / is not test identity
- == != > < test value

Cheers!

Reply all
Reply to author
Forward
0 new messages