So I'm starting to get my head around instance_/cast_ ...  Basically, 
sometimes Java sees an object as a less-specific type than it actually is, 
and being a static language, the attr/method names available are those of the 
less-specific type.  

For example:
In [3]: q=QueryParser('pants', StandardAnalyzer()).parse('shirts AND -shoes')
In [4]: q
Out[4]: <Query: +pants:shirts -pants:shoes>
In [5]: BooleanQuery.instance_(q)
Out[5]: True
In [6]: q.getClauses()
<type 'exceptions.AttributeError'>: 'Query' object has no 
attribute 'getClauses'
In [7]: bq=BooleanQuery.cast_(q)
In [8]: bq.getClauses()
Out[8]: [<BooleanClause: +pants:shirts>, <BooleanClause: -pants:shoes>]

While this is probably perfectly sensible to a Java/C++ programmer, as a 
Python coder this behavior is somewhat foreign.  Hopefully the above example 
will help make it clearer - the one in the README wasn't quite clear to me.

My question is: when should we use isinstance(obj, Klass) vs. 
Klass.instance_(obj)?  From below, it looks like python's isinstance can look 
up the Java class hierarchy but not down.  As a general rule, should we 
always be using Klass.instance_ for such tests?  Will this work correctly 
with pure-python types / Python subclasses of Java classes?

In [12]: isinstance(q, BooleanQuery)
Out[12]: False
In [23]: BooleanQuery.instance_(q)
Out[23]: True
In [13]: isinstance(bq, BooleanQuery)
Out[13]: True
In [14]: isinstance(bq, Query)
Out[14]: True

-- 
Peter Fein   ||   773-575-0694   ||   [EMAIL PROTECTED]
http://www.pobox.com/~pfein/   ||   PGP: 0xCCF6AE6B
irc: [EMAIL PROTECTED]   ||   jabber: [EMAIL PROTECTED]
_______________________________________________
pylucene-dev mailing list
[email protected]
http://lists.osafoundation.org/mailman/listinfo/pylucene-dev

Reply via email to