Type validation for cx_Oracle.OracleCursor
Question
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!
Solution
You can check type of variable using type(variable)
:
if str(type(cursor)) == "<type 'OracleCursor'>":
...
PS I know that isinstance()
is recommended.
OTHER TIPS
This works for me:
if isinstance(cursor, cx_Oracle.Cursor):
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow