python开发com组件_Python生成COM组件(原创)

经过一段对的使用,发现它确实是一门比较优秀的语言,语法简练,类库丰富且调用简单,在数据库,文本处理,网络编程方面都很棒。所以就一直想在实际工作中,确切地来说是现在正在进行的自动化测试使用Python,但是由于测试工具不支持python语言,所以没法直接在工具中使用。就考虑怎么在C#或者VBS中引用Python,一直没有太好的思路。偶然间阅读了《Python programming on win32》,里面提到了把Python的类注册成为Com组件供VB,Delphi实用的例子,顿时豁然开朗。51Testing软件测试网7a

W0J

yzn

下面就该书中Implementing COM Objects with Python章节进行分析,首先给出原文:51Testing软件测试网p+Qa}P"m0X

Implementing COM Objects with Python@?0LDw5f5X:Ow

LN$C0

In this section, we discuss how to implement COM objects using Python and a small sample of such an object. We also present some Visual Basic code that uses our Python implemented object.51Testing软件测试网J,@(r_iS4pu"D

For this demonstration, you'll write a simple COM object that supports a number of string operations. As Visual Basic is somewhat lacking in the string-processing department where Python excels, it's a good candidate. The sample provides a COM object with a single method, SplitString(). This method has semantics identical to the standard Python function string.split(); the first argument is a string to split, and the second optional argument is a string holding the character to use to make the split. As you have no doubt guessed, the method won't do much more than call the Python string.split() function.jd@h?1xO&qG)uF0

There are two steps to implement COM objects in Python:51Testing软件测试网+elH6Vw$UEc8]Define a Python class with the methods an

你可能感兴趣的:(python开发com组件)