views:

28

answers:

1

This could be very basic question. I'd like to validate a variable is type of cx_Oracle.OracleCursor, so I tried a couple of trials but all failed:

import cx_Oracle

def execute_sql(cursor):
    """Execute a SQL using cursor(cx_Oracle.OracleCursor)."""
    if not cursor:
        logging.error('cursor is null.')
        return None  # TODO: raise some exception.
    # elif isinstance(cursor, OracleCursor): # failed
    # elif isinstance(cursor, cx_Oracle.OracleCursor): # failed
        logging.error('cursor is not OracleCursor.')
        return None  # TODO: raise some exception.
# ...

How can I validate the type of cursor?

And the additional question is what exception/error is suitable for invalid type error?

Thanks for any help!

+1  A: 

You can check type of variable using type(variable):

if str(type(cursor)) == "<type 'OracleCursor'>":
       ...

PS I know that isinstance() is recommended.

Michał Niklas