When number slider has its minimum set to 0.0 and its value to 0.0 it is considered as None(NULL) inside GhPython instead of 0.0
NumberSlider_issue.gh (8.7 KB)
When number slider has its minimum set to 0.0 and its value to 0.0 it is considered as None(NULL) inside GhPython instead of 0.0
NumberSlider_issue.gh (8.7 KB)
That is how python works. It is not considered None
, that you do yourself on line 14 and 17.
In python 0
and 0.0
evaluate to False
in a comparison or boolean check. So if not 0:
will be True
and go into the statement block. Hence your x
gets set to None
, and then your a
gets set to x
.
Many languages have 0
evaluate to False
. You’ll find for instance C and C++ code littered with checks for values that are not 0
, nullptr etc.
ok, how can I check if variable has a value?
Explicitly check against None
:
if a==None:
x=None
From py2.7 interpreter:
>>> if 0 == None: print("hi")
...
>>>
I always wondered how different is not
and !=
are
Nitpick, but you didn’t use is not
, but not
.
You could do:
if a is None:
x = None
or the invers
if a is not None:
x = 42
else:
x = None
if not x:
# is different from
if x is not None:
# this is even more confusing
Riddle me this then.
If I use if not x:
and the minimum of the slider is not 0, then at value 0 x is not considered as None.
My bad, it is considered as None.
You must understand that None
is a type. 0
is a value of type integer (0.0
a float (or double).
Using is
you compare types, so if something is put into your a
it will have a type other than None
.
Using just the if not a:
you get a boolean check where a
can be of many types, but still evaluate to True
or False
.
To False
evaluate at least the values 0
, 0.0
, ""
, []
, {}
- you may see a pattern here. Typically a zero value of a type will evaluate to False
, any non-zero value evaluates to True
.
Perhaps now you can see why your initial code failed for the 0.0
case by giving unexpected execution.
That’s a very good explanation.
Thanks Nathan.
By the way, this explanation fixed a lot of scripts I could not figure out why they are not working properly.