Boolean case scenario

21 views
Skip to first unread message

yann19

unread,
Sep 23, 2016, 12:58:48 AM9/23/16
to Python Programming for Autodesk Maya
Hi all,

I would like to ask for opinion where I am using Boolean to test my 'selection' case.
In the following code, the boolean term for this scenario is called selection_case
I am sure that there is a term for such scenarios but I am not sure what it is called

In anyways, can someone kindly help take a look at my code and advise if I am on the right track? Is defining selection_case as None the right choice?

def test():
   
...
    selection_case
= None
   
# If the selection type is the apkgStack
   
if sel_custom:
        selection_case
= True
   
if sel_org:
        selection_case
= False

   
while not check:
       
if selection_case:
           
# perform this set of stuff
       
else:
           
# perform the other set of stuff


Justin Israel

unread,
Sep 23, 2016, 1:15:54 AM9/23/16
to python_in...@googlegroups.com
There can be times when you need to distinguish between a True/False setting and also whether the setting is even been set at all. That would call for having the None value. An example would be if you accepted a boolean option from a user, but you also want to know if the option has even been provided by the user at all. None could be the value when the option has not even been set.

But, in the way you are using it here, there is a problem. You are doing a "truthy" test on the value of selection_case. Both False and None evaluate to False when tested like that:

    if selection_case:
        ...
    else:
        ...

So when your selection_case is False or None, the "else" block will succeed. If you need to know the difference between False and None, then you need:

if selection_case is None:
    ...


Justin

--
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/d77c44e7-1715-4241-abd0-8b665396fb35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yann19

unread,
Sep 23, 2016, 12:34:54 PM9/23/16
to Python Programming for Autodesk Maya
Initially I had defined my selection_case to be False instead of None as I am not so sure which of the two I should put.
There has been a function before this test() code I wrote that checks for selection. If there are no selections, it will return None. If there are selections, it will then check if the selections fulfills the nodetype criteria as defined in wihin sel_custom and sel_orig.

Hence my usage of:
    if selection_case:
        ...
    else:
        ...

In cases like this, should I just put it as selection_case = False instead?

By the way, when I tried using
    if selection_case is None:
        ...

It is not working as I expected it would output the same as in the code I have written

Justin Israel

unread,
Sep 23, 2016, 3:56:11 PM9/23/16
to Python Programming for Autodesk Maya

To be honest, there isn't enough context here for me to really understand what you are trying to do. I would need to see a small real example to better understand the logic you want to achieve.
But the fact is that you can specifically check for "is None". The question is really how you want to use it to achieve some kind of goal here.


--
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.

Alok Gandhi

unread,
Sep 24, 2016, 1:32:20 AM9/24/16
to python_in...@googlegroups.com

By the way, when I tried using
    if selection_case is None:
        ...
It is not working as I expected it would output the same as in the code I have written

Not answering the original question but you can clearly distinguish between True, False and None, as Justin has rightly pointed out. 

In python None (https://docs.python.org/2.7/c-api/none.html?highlight=none%20object) is just a singleton, immutable object, meaning that there is only one instance of None during a session of python interpreter(singleton) and it cannot be changed(immutable).

So when you run python, there is always only one object None with no methods to mutate it. Btw, this is the most precise definition of immutability in python - objects that do not have any mutable methods implemented for them are immutable, nothing more, nothing less. There is not some 'magic lock' that prevent objects from changing in memory, it is just the absence of methods that can mutate it.

So None just happens to be a convinience object, that can be  used for implying the notion of an empty or none or null value.

Also note that, in python, variable names are just like stickers or labels that you put on objects in memory. With different assignments you just move around those labels.

Now let's talk about `None` and `booleans`.

The following values are considered false in python:
  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False.
All other values are considered true — so objects of many types are always true.

Let's say you create a variable and set it to None.
>>> var = None                                                      

What you have done here is just created a new reference or "label" or "sticker" on the `None` object im memory.


'None' will always resolve to false as boolean:
>>> bool(var)                                                       
False                                                               


But, we can still check if it is `None`
>>> var is None                                                     
True                                                                

Note that in the above, the is keyword just compares the memory address of objects (oject identity)
is basically answers the question "Do I have two names for the same object?"

This allows us to have three separate states of our variable either they are None, or False, or True.

While using None and booleans togetther always remember to use is None to first check if it is None or not. I would even to go out on the limb to say that whenever a variable is set to None, in most cases use if var is None instead of if not var.

Here's a more concrete example,
--------------------------------------------------------------------------------
# The goal here is to find that value and
# save it for later use.


# We are dealing with a value
# that can be either True or False.


# First, we assign `None` to our variable
value_true_or_false = None

# Next, `value_true_or_false` variable is passed to
# some function to get the value
value_true_or_false = some_func_to_get_value(value_true_or_false)



# At this point we want to save this value, but before saving it, 
# we need to check if the function has 'filled in' our value.

# BAD (WILL FAIL)
# Usually we are tempted to do this,
if value_true_or_false:  # Note: `if var` is more pythonic than `if var == True`
    save(value_true_or_false)

# The above will work if setting_true_or_false is True,
# but will NOT save the value if it is False!


# GOOD (WILL WORK)
# We want to save both the values either True or False, whatever it is.
# So the correct way to do this is:
if value_true_or_false is not None:
    save(value_true_or_false)
----------------------------------------------------------------------------------------------------------------------------
Hope that helps

- Alok


On Sat, Sep 24, 2016 at 3:55 AM, Justin Israel <justin...@gmail.com> wrote:

To be honest, there isn't enough context here for me to really understand what you are trying to do. I would need to see a small real example to better understand the logic you want to achieve.
But the fact is that you can specifically check for "is None". The question is really how you want to use it to achieve some kind of goal here.

On Sat, 24 Sep 2016, 4:34 AM yann19 <yang...@gmail.com> wrote:
Initially I had defined my selection_case to be False instead of None as I am not so sure which of the two I should put.
There has been a function before this test() code I wrote that checks for selection. If there are no selections, it will return None. If there are selections, it will then check if the selections fulfills the nodetype criteria as defined in wihin sel_custom and sel_orig.

Hence my usage of:
    if selection_case:
        ...
    else:
        ...

In cases like this, should I just put it as selection_case = False instead?

By the way, when I tried using
    if selection_case is None:
        ...

It is not working as I expected it would output the same as in the code I have written

--
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_maya+unsub...@googlegroups.com.

--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1-9oPa1%3DE-9bXOXyo%2BqnOmPp0PvV8ULnt-jnS4m7D2kQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages