Search code examples
pythonisinstance

Python: checking type of variables


I have a question about python.

I have variables a, b, c and d.

And I have the following line:

if    not isinstance(a, int)   or not isinstance(b, int)  \
   or not isinstance(c, int)   or not isinstance(d, int)  \
   or not isinstance(a, float) or not isinstance(b, float)\
   or not isinstance(c, float) or not isinstance(d, float):
    do something

Is it possible to make this code shorter?

Thanks!


Solution

  • U should use all:

    if not all(isinstance(var, (int, float)) for var in [a, b, c, d]):
        # do stuff
    

    Note, that you can supply both int and 'float' to the isinstance call.