문제

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!

도움이 되었습니까?

해결책

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

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

PS I know that isinstance() is recommended.

다른 팁

This works for me:

if isinstance(cursor, cx_Oracle.Cursor):
    ...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top