Discussion:
[python-win32] Why doesn't dir() list COM object properties, only methods?
Jeremy Herbert
2014-09-15 15:53:04 UTC
Permalink
Hi all,

I'm currently struggling a little with pywin32, and to me it seems
that the default python dir() behaviour is broken. For the record, I
am forcing early-binding.

To explain, I think some code would be best:

class A(object):
def __init__(self):
self.member = 1
def hello(self):
print "test"

tmp = A()
dir(tmp)

###
The following is printed in iPython:

Out[4]:
['__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'hello',
'member']

Note that both member functions and variables are present. When I run
dir() on a COM object instance however, it does not show any of the
member properties, just the member functions. So if the A class was
instantiated as an COM object, it would only show "hello" as well as
the builtins in the printout, and "member" would be missing.

Given that I am trying to interact with Autodesk Inventor, and 90% of
the functionality of the COM interface is via properties, this is
quite frustrating! Is there any way to list all of the properties of a
COM object instance?

Thanks,
Jeremy Herbert
Tim Roberts
2014-09-15 17:33:18 UTC
Permalink
Post by Jeremy Herbert
I'm currently struggling a little with pywin32, and to me it seems
that the default python dir() behaviour is broken. For the record, I
am forcing early-binding.
"Broken" is too harsh of a word. What you're seeing here is more or
less an impedance mismatch between two very different interfaces.

The Python dir() function knows about Python objects living in the
Python object space. COM objects live in an entirely different space.
Python, being cross-platform, doesn't have any internal knowledge about
COM, which is strictly a Windows concept.

The win32com code does its best to provide a bridge between those
worlds, by providing a simulated Python object that satisfies most of
the Python object requirements, but proxies its activities to the COM
object it is wrapping. In the case of early binding, the only way
win32com can do that is by parsing the TLB to figure out the object
layout. If you look at the Python file generated by makepy, do you see
the properties there?

One interesting side note is that COM doesn't really have the notion of
properties, because many languages don't support them. When an object
supports a Color property, that's actually implemented by having methods
called get_Color and set_Color. You might see if those are in the
makepy file.
Post by Jeremy Herbert
Note that both member functions and variables are present. When I run
dir() on a COM object instance however, it does not show any of the
member properties, just the member functions. So if the A class was
instantiated as an COM object, it would only show "hello" as well as
the builtins in the printout, and "member" would be missing.
You do need to remember that you're not really looking at a COM object
instance. You're looking at a win32com proxy that is communicating with
the COM object. It's doing the best it can with the information it can
extract, but if the TLB doesn't include everything, makepy can't make it up.
Post by Jeremy Herbert
Given that I am trying to interact with Autodesk Inventor, and 90% of
the functionality of the COM interface is via properties, this is
quite frustrating! Is there any way to list all of the properties of a
COM object instance?
Check the makepy file and see if they are present.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Jeremy Herbert
2014-09-15 18:43:52 UTC
Permalink
Hi Tim,

Sorry, I did not intend for my message to be interpreted as overly
harsh :) Is there any way to introspect the properties of the objects
at runtime using python? They are present in the makepy file under
_prop_map_get_.

Thanks,
Jeremy Herbert
Post by Tim Roberts
Post by Jeremy Herbert
I'm currently struggling a little with pywin32, and to me it seems
that the default python dir() behaviour is broken. For the record, I
am forcing early-binding.
"Broken" is too harsh of a word. What you're seeing here is more or
less an impedance mismatch between two very different interfaces.
The Python dir() function knows about Python objects living in the
Python object space. COM objects live in an entirely different space.
Python, being cross-platform, doesn't have any internal knowledge about
COM, which is strictly a Windows concept.
The win32com code does its best to provide a bridge between those
worlds, by providing a simulated Python object that satisfies most of
the Python object requirements, but proxies its activities to the COM
object it is wrapping. In the case of early binding, the only way
win32com can do that is by parsing the TLB to figure out the object
layout. If you look at the Python file generated by makepy, do you see
the properties there?
One interesting side note is that COM doesn't really have the notion of
properties, because many languages don't support them. When an object
supports a Color property, that's actually implemented by having methods
called get_Color and set_Color. You might see if those are in the
makepy file.
Post by Jeremy Herbert
Note that both member functions and variables are present. When I run
dir() on a COM object instance however, it does not show any of the
member properties, just the member functions. So if the A class was
instantiated as an COM object, it would only show "hello" as well as
the builtins in the printout, and "member" would be missing.
You do need to remember that you're not really looking at a COM object
instance. You're looking at a win32com proxy that is communicating with
the COM object. It's doing the best it can with the information it can
extract, but if the TLB doesn't include everything, makepy can't make it up.
Post by Jeremy Herbert
Given that I am trying to interact with Autodesk Inventor, and 90% of
the functionality of the COM interface is via properties, this is
quite frustrating! Is there any way to list all of the properties of a
COM object instance?
Check the makepy file and see if they are present.
--
Providenza & Boekelheide, Inc.
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
Tim Roberts
2014-09-15 19:46:51 UTC
Permalink
Post by Jeremy Herbert
Is there any way to introspect the properties of the objects
at runtime using python? They are present in the makepy file under
_prop_map_get_.
Well, clearly some combination of _prop_map_get and _prop_map_put will
give you this information.

If Mark Hammond is listening in, he's the real guru in this area, and
may know of a more "blessed" way of getting that info.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Loading...