On Tue, Jul 26, 2016 at 9:06 AM fruity <[email protected]> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/c49f3041-9f11-47e1-995c-e6eef0515d5b%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/c49f3041-9f11-47e1-995c-e6eef0515d5b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1waMSW1K%2BN%2BYJnOZTvTan8wdd--Z9yUjbykjwgX4-Esg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to