[python-win32] getting underlying OLE object identity for win32com objects
Tim Roberts
timr at probo.com
Wed Oct 1 19:55:42 CEST 2014
Dan Lenski wrote:
> I'm trying to figure out if there is a canonical way to get the identity
> of an object that has been wrapped by the win32com module, so that it
> can be compared to other objects.
>
> For example, I can use this code to get a handle to the same object
> twice in the JMP application, but the two tables do not have the same
> "object identity" at the Python level:
Right. They are two separate Python objects.
print table
print same_table
print table is same_table
"is" will obviously fail, because it is checking for two identical
Python objects. However, did you try the == operator? The source code
seems to imply that the __eq__ operator is delegated to the _oleobj_
properties, which do know about COM object identity.
> In order to confirm that two objects refer to the same underlying OLE
> object, I've resorted to parsing the `repr()` strings and comparing the
> hexadecimal addresses ("`obj at 0x...`").
>
> Is there a better way to do this?
Assuming the straight "==" didn't work, I would think a better way would
be simply to compare the _oleobj_:
if table._oleobj_ == same_table._oleobj_:
The source code in dynamic.py says the _oleobj_ members know how to
compare themselves.
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32
mailing list