How to pass true or false to COM objects?

Mark Hammond mhammond at skippinet.com.au
Mon Aug 19 22:05:51 EDT 2002


Duncan Booth wrote:
> sjoshi at ingr.com (Sunit Joshi) wrote in 
> news:8f8ffe67.0208161214.4a39ce42 at posting.google.com:
> 
> 
>>I'm using MSXML4.0 and need to pass 'true' or 'false' values to the
>>COM object. Could someone tell me how to do that..??
> 
> 
> If my memory serves (and it might not), then passing True or False as 
> values will work providing you are using Python 2.2.1 or later. In earlier 
> versions you should make sure you pass in the result of a comparison: e.g. 
> add:
>    True, False = 1==1, 1==0
> somewhere in your code and you now have suitable values to pass in.

For the sake of completeness:

If the COM server reports to Python the types of the parameters, then 
almost any true/false value will work.  Integer 0 and 1 are commonly 
used in this case.

If the COM server reports nothing about the types (or reports a generic 
VARIANT) then Python will try and *guess* the type to pass.  In this 
case, passing a simple integer may cause the COM object see an integer 
param - in this case, passing the result of the expression as Duncan 
notes will force Python to assume boolean.

A reasonable paradigm may be to use code such as:

try:
   True, False
except NameError:
   True, False = 1==1, 1==0

This should work for all Python versions, using the builting True and 
False names if they exist.

Still not sure what to do about that new boolean builtin "Maybe" ;)

Mark.




More information about the Python-list mailing list