Python Forum

Full Version: optimizing calls to isinstance()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
does CPython optimize calls to isinstance() when argument 2 is a well know class (e.g. a type like int) "literal" or a list or tuple of them?

i am wondering because i am using some small functions that i code in place of isinstance() to make code appear cleaner that just does the call to isinstance. so, instead of:
if isinstance(foobar,float):
    ...
i would code like:
if is_float(foobar):
    ...
these functions would be defined like:
from decimal import Decimal
from io      import IOBase
from numbers import Number

#-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------
def  is_bb        (x):return     isinstance(x, (bytes,bytearray) )
def  is_bi        (x):return     isinstance(x, (bool,int) )
def  is_bif       (x):return     isinstance(x, (bool,int,float) )
def  is_bifc      (x):return     isinstance(x, (bool,int,float,complex) )
def  is_bifcd     (x):return     isinstance(x, (bool,int,float,complex,Decimal) )
def  is_bool      (x):return     isinstance(x, bool )
def  is_bytearray (x):return     isinstance(x, bytearray )
def  is_bytes     (x):return     isinstance(x, bytes )
def  is_complex   (x):return     isinstance(x, complex)
def  is_decimal   (x):return     isinstance(x, Decimal )
def  is_dict      (x):return     isinstance(x, dict )
def  is_ds        (x):return     isinstance(x, (dict,set) )
def  is_fc        (x):return     isinstance(x, (float,complex) )
def  is_fcd       (x):return     isinstance(x, (float,complex,Decimal) )
def  is_fd        (x):return     isinstance(x, (float,Decimal) )
def  is_float     (x):return     isinstance(x, float )
def  is_frozenset (x):return     isinstance(x, frozenset )
def  is_if        (x):return     isinstance(x, (int,float) )
def  is_ifc       (x):return     isinstance(x, (int,float,complex) )
def  is_ifcd      (x):return     isinstance(x, (int,float,complex,Decimal) )
def  is_ifd       (x):return     isinstance(x, (int,float,Decimal) )
def  is_iio       (x):return     isinstance(x, (int,IOBase) )
def  is_int       (x):return     isinstance(x, int )
def  is_iobase    (x):return     isinstance(x, IOBase )
def  is_lio       (x):return     isinstance(x, (list,IOBase) )
def  is_list      (x):return     isinstance(x, list )
def  is_ls        (x):return     isinstance(x, (list,set) )
def  is_lt        (x):return     isinstance(x, (list,tuple) )
def  is_ltsf      (x):return     isinstance(x, (list,tuple,set,frozenset) )
def  is_ltsfd     (x):return     isinstance(x, (list,tuple,set,frozenset,dict) )
def  is_number    (x):return     isinstance(x, Number )
def  is_sbb       (x):return     isinstance(x, (str,bytes,bytearray) )
def  is_sbbi      (x):return     isinstance(x, (str,bytes,bytearray,int) )
def  is_set       (x):return     isinstance(x, set )
def  is_sf        (x):return     isinstance(x, (set,frozenset) )
def  is_si        (x):return     isinstance(x, (str,int) )
def  is_str       (x):return     isinstance(x, str )
def  is_tf        (x):return     isinstance(x, (tuple,frozenset) )
def  is_tuple     (x):return     isinstance(x, tuple )
#-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------
def not_bb        (x):return not isinstance(x, (bytes,bytearray) )
def not_bi        (x):return not isinstance(x, (bool,int) )
def not_bif       (x):return not isinstance(x, (bool,int,float) )
def not_bifc      (x):return not isinstance(x, (bool,int,float,complex) )
def not_bifcd     (x):return not isinstance(x, (bool,int,float,complex,Decimal) )
def not_bool      (x):return not isinstance(x, bool )
def not_bytearray (x):return not isinstance(x, bytearray )
def not_bytes     (x):return not isinstance(x, bytes )
def not_complex   (x):return not isinstance(x, complex)
def not_decimal   (x):return not isinstance(x, Decimal )
def not_dict      (x):return not isinstance(x, dict )
def not_ds        (x):return not isinstance(x, (dict,set) )
def not_fc        (x):return not isinstance(x, (float,complex) )
def not_fcd       (x):return not isinstance(x, (float,complex,Decimal) )
def not_fd        (x):return not isinstance(x, (float,Decimal) )
def not_float     (x):return not isinstance(x, float )
def not_frozenset (x):return not isinstance(x, frozenset )
def not_if        (x):return not isinstance(x, (int,float) )
def not_ifc       (x):return not isinstance(x, (int,float,complex) )
def not_ifcd      (x):return not isinstance(x, (int,float,complex,Decimal) )
def not_ifd       (x):return not isinstance(x, (int,float,Decimal) )
def not_iio       (x):return not isinstance(x, (int,IOBase) )
def not_int       (x):return not isinstance(x, int )
def not_iobase    (x):return not isinstance(x, IOBase )
def not_lio       (x):return not isinstance(x, (list,IOBase) )
def not_list      (x):return not isinstance(x, list )
def not_ls        (x):return not isinstance(x, (list,set) )
def not_lt        (x):return not isinstance(x, (list,tuple) )
def not_ltsf      (x):return not isinstance(x, (list,tuple,set,frozenset) )
def not_ltsfd     (x):return not isinstance(x, (list,tuple,set,frozenset,dict) )
def not_number    (x):return not isinstance(x, Number )
def not_sbb       (x):return not isinstance(x, (str,bytes,bytearray) )
def not_sbbi      (x):return not isinstance(x, (str,bytes,bytearray,int) )
def not_set       (x):return not isinstance(x, set )
def not_sf        (x):return not isinstance(x, (set,frozenset) )
def not_si        (x):return not isinstance(x, (str,int) )
def not_str       (x):return not isinstance(x, str )
def not_tf        (x):return not isinstance(x, (tuple,frozenset) )
def not_tuple     (x):return not isinstance(x, tuple )
#-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------.-------