Cheetah
Cheetah

Reputation: 14379

Python type equality

I am reading from an Oracle database and would like to check if the field is column is of type cx_Oracle.DATETIME.

I have tried the following:

if columnTypes[columnIndex] == cx_Oracle.DATETIME:

and

if columnTypes[columnIndex] is cx_Oracle.DATETIME:

Neither work.

If I do:

print columnTypes[columnIndex]

it returns:

<type 'cx_Oracle.DATETIME'>

EDIT:

It worked by storing the type in a variable:

dbDateType = cx_Oracle.DATETIME

and

if columnTypes[columnIndex] == dbDateType

Upvotes: 2

Views: 1127

Answers (2)

KendallV
KendallV

Reputation: 396

Does the column columnTypes store the types themselves or representations of the types as for instance a string or type instance?

Try getting the output of both columnTypes[columnIndex] and type(columnTypes[columnIndex]). Hopefully one will give you <type 'cx_Oracle.DATETIME'>, which you can then compare to type(cx_Oracle.DATETIME).

If you get a super/subclass you will have to use isinstance in a similar fashion - this question might be of interest.

Upvotes: 2

Vatine
Vatine

Reputation: 21238

The best way is probably to do:

isinstance(columnTypes[columnIndex], cx_Oracle.DATETIME)

If you want to check for exact type, type(obj) == myType woudl work, but using isinstance allows for sub-typing.

Upvotes: 2

Related Questions