1

We are looking for this type of statement, but when trying it, it doesn't work:

if type(item) == "<type 'oracle.sql.BLOB'>"

Background: We are receiving BLOB type objects back from a JDBC connection in Jython. We are trying to check whether the type of a column returned is of type so we know to decode the binary object.

What else we have tried: Other answers to questions show examples where they use the class name (i.e. type(i) is int) as a test - but in this case, if we use the supposed class name like this:

if type(item) == oracle.sql.BLOB:

We get this error:

NameError: name 'oracle' is not defined

Multiple other answers to questions of this type mention using isinstance() as a preferable method for checking types - but all the answers we saw showed coders using existing objects of that type to test against.

Yet, in this instance, we don't have an object of that oracle blob type.

How do we test for a object type? Or, how do we create an object of that type so we can use isinstance()? Or is there another approach?

1 Answer 1

2

You can get the runtime class of an object using -

.getClass().getName()

You would need to do something as follows:

if item.getClass().getName() == 'oracle.sql.BLOB':
    print "This is a BLOB."

Sample code:

import java.util.ArrayList as ArrayList

arr = ArrayList()
arr.add(10)
arr.add(20)
print arr
print "Name:", arr.getClass().getName()
print "Simple Name:", arr.getClass().getSimpleName()

if arr.getClass().getName() == 'java.util.ArrayList':
    print "This is an ArrayList."

Output:

> jython check_type.py
[10, 20]
Name: java.util.ArrayList
Simple Name: ArrayList
This is an ArrayList.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.