I have a question regarding if not
statement in Python 2.7
.
I have written some code and used if not
statements. In one part of the code I wrote, I refer to a function which includes an if not
statement to determine whether an optional keyword has been entered.
It works fine, except when 0.0
is the keyword's value. I understand this is because 0
is one of the things that is considered 'not'. My code is probably too long to post, but this is an analogous (albeit simplified) example:
def square(x=None):
if not x:
print "you have not entered x"
else:
y=x**2
return y
list=[1, 3, 0 ,9]
output=[]
for item in list:
y=square(item)
output.append(y)
print output
However, in this case I got left with:
you have not entered x
[1, 9, None, 81]
Where as I would like to get:
[1, 9, 0, 81]
In the above example I could use a list comprehension, but assuming I wanted to use the function and get the desired output how could I do this?
One thought I had was:
def square(x=None):
if not x and not str(x).isdigit():
print "you have not entered x"
else:
y=x**2
return y
list=[1, 3, 0 ,9]
output=[]
for item in list:
y=square(item)
output.append(y)
print output
This works, but seems like a bit of a clunky way of doing it. If anyone has another way that would be nice I would be very appreciative.
If you can identify a single type that all input must be compatible with, then you can convert it and handle the exception:
This has a few advantages.
float
. This can be any number, a string containing a number ('5.0'
), or anything else that can be converted to afloat
. (float
instances are returned unchanged.)float
. Python provides the__float__
magic method, so if your users have exotic types and need to use your function, they're not sunk.An aside (maybe):
If
None
is an invalid value, don't make it a default value. Just force the caller to provide input:U can handle Exception here. Make code like this
As far as I understand it you want to catch the cases where no input was given:
and the cases where it's not a number. With the second one you'll end up with quite some problems, because you maybe want to allow numpy-ndarrays in the future or something else, normally I would just suggest don't bother with excluding other classes but if you only want to allow
int
andfloat
then usethe
...
represent other allowed classes (maybeDecimal
orFractions
).So you could write it as:
Because
None
is not anint
nor afloat
you can also omit the first condition and write:Since you are using
None
to signal "this parameter is not set", then that is exactly what you should check for using theis
keyword:Checking for type is cumbersome and error prone since you would have to check for all possible input types, not just
int
.Problem
You understand it right.
not 0
(and alsonot 0.0
) returnsTrue
inPython
. Simple test can be done to see this:Thus, the problem is explained. This line:
Must be changed to something else.
Solutions
There are couple of ways which can be done to fix the issue. I am just going to list them from what I think is the best solution down to the last possible solutions:
To handle all possible valid cases.
Since
square
should naturally expect a number input with the exclusion of complex number and shouldreturn
an error otherwise, I think the best solution is to evaluate usingif not isinstance(x, numbers.Number) or isinstance(x, numbers.Complex):
numbers.Number is the abstract class to check if argument
x
is a number (credit to Copperfield for pointing this out).Excerpt from Python Standard Library Documentation explains just what you need - with the exception of complex number:
But, you don't want the input to be complex number. So, just omit it using
or isinstance(x, numbers.Complex)
To handle just the data types you want to handle.
If you have a list valid inpug data types you, you could also put up just those specific data types you want to handle. That is, you don't want to handle the cases for data types other than what you have specified. Examples:
(Code above is done in more Pythonical way, as suggested by cat)
Not handling impossible cases: you know what the users would not put up as input.
Think it more loosely, if you know - not the data types you want to handle like in the second solution - but the data types which the user would not put, then you can have looser condition check like this:
The only downside of this solution is that you don't handle complex type. Therefore can only be implementing by owing to the fact that the users would not have complex number as the input.
To handle input errors only for the known possible inputs which can cause the errors.
For example, if you know that x is always
int
orNone
- and thus the only possible input error isNone
- then we can simply write the logic to avoidy
being evaluated only whenx
isNone
like this:...and yet the most dangerous for being used if you do not know exactly what the users would put up for the input. Otherwise, this solution is fine and is also the simplest.
Your solution, I think more or less belongs to this category. You know what input the user will give and what the user will not. Thus, using this solution or your own solution:
Is fine, except that the example solution is simpler
Given your case, you can use any solution above to get:
(Side Note: I try to format the solutions to look like "canonical solutions" for ease of reading purpose. This way, those who have the same questions and who visit this page in the future may be able to find the solutions more comprehensive and readable)