isinstance(Region) vs. isinstance(Region.nearby) not giving the same result

Asked by Romuald C.

Hello,

Tried to search for an answer but did not find anything satisfactory but I'm kinda bad at Internet search (so sorry if an answer already exists)

Here is my problem:
I'm trying to write some function that performs specific processing based on parameter type (mainly Match or Region).
No problem with Match, but Region seems a bit more tricky so I dug a bit more and found out that there seems to have several types of Region, depending where it comes from.

This code (with Sikuli 1.0.1):
  from sikuli import *

  reg = Region(1,1,100,100)

  print "Is reg a Region?"
  if isinstance(reg, Region):print "yes, a Region"
  else:print "nope, not a Region"

  print "Is reg.nearby(5) a Region?"
  if isinstance(reg.nearby(5), Region):print "yes, a Region"
  else:print "nope, not a Region"

gives

  Is reg a Region?
  yes, a Region
  Is reg.nearby(5) a Region?
  nope, not a Region

Seeing this result (which seems strange as documentation says that nearby() "Returns a new Region"), I tried this
  print "reg class=%s"%reg.__class__
  print "reg.nearby(5) class=%s"%reg.nearby(5).__class__
and got
  reg class=<class 'sikuli.Region.Region'>
  reg.nearby(5) class=<type 'org.sikuli.script.Region'>

So, here comes the question: what am I doing wrong to get reg.nearby(5) not being a Region?

Thanks in advance
Romuald

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

this is a historical oddity, that we have to live with, because I have to stay with this to be backwards compatible.

On the Python API level, there is a Python class Region, that is a superclass of the Java API class with the same name.
So depending on how the Region object tested came into life, it is either an instance of the Python class Region or of the Java class Region (org.sikuli.script.Region).

The complete (99 %) API of "class Region" is implemented at the Java level. So if a Region.method() returns a new Region object, this is always a org.sikuli.script.Region object (as reg.nearby(5) in your case).

You can cast every Region object to be Python Region using
reg = Region(reg)

Your check case could easily be solved this way:

if isinstance(reg, Region) or isinstance(reg, JRegion): print "is a region"

JRegion is a name that is created in the global namespace at SikuliX startup using

import org.sikuli.script.Region as JRegion

to be able to later during init say

class Region(JRegion): # define the Python class Region as superclass of org.sikuli.script.Reagion
...

Revision history for this message
Romuald C. (romualdc) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Romuald C. (romualdc) said :
#3

As said in automatic comment

  if isinstance(reg, Region) or isinstance(reg, JRegion): print "is a region"

does the trick.

Thanks for your help
Romuald