How would I use a negative form of Python's isinstance()?
Normally negation would work something like
x != 1 if x not in y if not a
I just haven't seen an example with isinstance(), so I'd like to know if there's a correct way to used negation with isinstance().
You are simply negating the "truth value" (ie Boolean) that isinstance is returning.
Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.
type() returns the type of the object you put in as an argument, and is usually not useful unless compared with a real type (such as type(9) == int ). isinstance() returns a boolean - true or false - based on whether the object is of given type.
Negation: The not operator in Python can be used only in the unary form, which means negation, returning the a result that is the opposite of its operand. Its boolean prototype is not (bool) -> bool.
Just use not
. isinstance
just returns a bool
, which you can not
like any other.
That would seem strange, but:
if not isinstance(...): ...
The isinstance
function returns a boolean value. That means that you can negate it (or make any other logical operations like or
or and
).
Example:
>>> a="str" >>> isinstance(a, str) True >>> not isinstance(a, str) False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With