Forcing Early or Late Binding

When you use the win32com.client.Dispatch() method, the PythonCOM framework automatically selects the best available binding method; if MakePy support for an object exists, it provides early binding; otherwise the dynamic dispatch method provides late binding. In some cases, you may wish to get explicit control over the binding method.

The win32com.client.Dispatch() method achieves this functionality by initially checking to see if MakePy support exists for the object. If MakePy support doesn’t exist, the Python module win32com.client.dynamic is called to perform the late-bound functionality. To force late binding for your objects, use the win32com.client.dynamic module directly, bypassing any MakePy-generated objects.

The win32com.client.dynamic module contains only one function designed to be used by Python programmers, win32com.client.dynamic.Dispatch() . This function is used in the same way as win32com.client.Dispatch(), except that MakePy support is never used for the returned object.

To force the use of early binding to access COM objects, you must force the MakePy process in your code. Once you have ensured the MakePy support exists, use win32com.client.Dispatch() as usual. It always returns the MakePy-supported wrappers for your COM object.

To force the MakePy process, the win32com.client.gencache module is used. This module contains the code that manages the directory of MakePy-generated source files: the generated cache, or gencache. There ...

Get Python Programming On Win32 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.