vb2Py - Class Modules

Contents of this page:

Options:

Special Features:

General

Class modules are translated into Python code modules with a single class whose name is the nane of the VB class. By default, this class is a new style Python class (inherits from Object). All methods in the class are converted to unbound methods of the class. Properties are converted to Python properties but an error is raised if the property has both Let and Set decorators. Since Python has no equivalent of the Set keyword, the Property Set method is treated in the same way as a Property Let.

Attributes defined at the class level are assumed to be class attributes in the Python class. By default, the conversion respects the Public/Private scope of both attributes and methods but this can be disabled if desired.

Default Conversion

VBPython

Public Name As String
Public Age As Single
Private ID As Long

Public Sub checkAge()
If Age = 0 Then Age = 1
End Sub
'
Private Sub setUp()
ID = Rnd()
If ID = 0 Then setUp
End Sub


class MyClass(Object):
""""""

Name = String()
Age = Single()
__ID = Long()

def checkAge(self):
if self.Age == 0:
self.Age = 1

def __setUp(self):
self.__ID = Rnd()
if self.__ID == 0:
self.__setUp()

List of Options

Here are the options in the INI file:

[Classes]
# Yes or No, whether to use new style classes for all classes
UseNewStyleClasses = Yes

In addition to these specific options, some General options apply:

[General]
# Yes or No, whether to respect Private status of variables
RespectPrivateStatus = Yes
# Prefix to use to tag data as private (Python normally uses __ but VB convention is m)
PrivateDataPrefix = __
    # Yes or No, whether to try to automatically extract docstrings from the code
    TryToExtractDocStrings = Yes

UseNewStyleClasses

By default, all classes are created as new style Python classes (inheriting from Object). Old style classes can be created by setting the UseNewStyleClasses option to No.

VBPython

' VB2PY-GlobalSet: Classes.UseNewStyleClasses = No
Public Name As String
Public Age As Single
Private ID As Long

Public Sub checkAge()
If Age = 0 Then Age = 1
End Sub
'
Private Sub setUp()
ID = Rnd()
If ID = 0 Then setUp
End Sub
' VB2PY-Unset: Classes.UseNewStyleClasses


class MyClass:
""""""

Name = String()
Age = Single()
__ID = Long()

def checkAge(self):
if self.Age == 0:
self.Age = 1

def __setUp(self):
self.__ID = Rnd()
if self.__ID == 0:
self.__setUp()

RespectPrivateStatus

Syntax: RespectPrivateStatus = Yes | No

By default, variables or methods defined as Private (which is the default in VB), will be marked as private in the Python module also. Private Python variables will be prefixed with a private marker (two underscores by default). Since Private is the default in VB, this can lead to a lot of hidden variables in the Python code. The RespectPrivateStatus option allows you to turn off the Private/Public switch.

VBPython

' VB2PY-GlobalSet: General.RespectPrivateStatus = No
Public Name As String
Public Age As Single
Private ID As Long

Public Sub checkAge()
If Age = 0 Then Age = 1
End Sub
'
Private Sub setUp()
ID = Rnd()
If ID = 0 Then setUp
End Sub
' VB2PY-Unset: General.RespectPrivateStatus


class MyClass(Object):
""""""

Name = String()
Age = Single()
ID = Long()

def checkAge(self):
if self.Age == 0:
self.Age = 1

def setUp(self):
self.ID = Rnd()
if self.ID == 0:
self.setUp()

PrivateDataPrefix

Syntax: PrivateDataPrefix = prefix

If RespectPrivateStatus is set then each Private variable will be prefixed with the string specified by the PrivateDataPrefix option. By default this is two underscores, __, which means that Python will use name mangling to ensure that the names really are private. Changing this option allows names to converted to some other convention (eg m) which marks names but does not enforce privacy.

VBPython

' VB2PY-GlobalSet: General.PrivateDataPrefix = m
Public Name As String
Public Age As Single
Private ID As Long

Public Sub checkAge()
If Age = 0 Then Age = 1
End Sub
'
Private Sub setUp()
ID = Rnd()
If ID = 0 Then setUp
End Sub
' VB2PY-Unset: General.PrivateDataPrefix


class MyClass(Object):
""""""

Name = String()
Age = Single()
mID = Long()

def checkAge(self):
if self.Age == 0:
self.Age = 1

def msetUp(self):
self.mID = Rnd()
if self.mID == 0:
self.msetUp()

TryToExtractDocStrings

Syntax: TryToExtractDocStrings = Yes | No

If TryToExtractDocStrings is set then any contiguous block of comment lines found at the start of the module are interpretted as a docstring and added to the class definition. The docstring terminates with the first non-comment line.

VBPython

' VB2PY-GlobalSet: General.TryToExtractDocStrings = Yes
' This is the documentation for the module
' This line is also documentation
' So is this one
' And this is the last

Public Name As String
Public Age As Single
Private ID As Long

Public Sub checkAge()
If Age = 0 Then Age = 1
End Sub
'
Private Sub setUp()
ID = Rnd()
If ID = 0 Then setUp
End Sub
' VB2PY-Unset: General.TryToExtractDocStrings


class MyClass(Object):
""" This is the documentation for the module
This line is also documentation
So is this one
And this is the last

"""

Name = String()
Age = Single()
__ID = Long()

def checkAge(self):
if self.Age == 0:
self.Age = 1

def __setUp(self):
self.__ID = Rnd()
if self.__ID == 0:
self.__setUp()

Special Features

Properties

Property Let, Get and Set methods are grouped using Python 2.2's property decorator. The accessor functions are automatically called get<Name> and set<Name>. No checking is performed to ensure that these names do not collide with other class methods.

VBPython

Dim mName As String
Dim mAge As Single

Public Property Let Name(Value)
mName = Value
End Property
'
Public Property Get Name()
Name = mName
End Property


class MyClass(Object):
""""""

__mName = String()
__mAge = Single()


def setName(self, Value):
self.__mName = Value

def getName(self):
_ret = None
_ret = self.__mName
return _ret
Name = property(fset=setName, fget=getName)

Class_Initialize

If the VB class includes a Class_Initialize method, then this is translated to an __init__ method in the Python class.

VBPython

Dim mName As String
Dim mAge As Single

Public Sub Class_Initialize()
mAge = 0
End Sub


class MyClass(Object):

__mName = String()
__mAge = Single()

def Class_Initialize(self):
self.__mAge = 0

Class_Terminate

If the VB class includes a Class_Terminate method, then this is translated to an __del__ method in the Python class. Although the Python __del__ method will be called upon object removal the exact details of when this is called are not guaranteed to match those in the VB program.

VBPython

Dim mObj As New Collection

Public Sub Class_Terminate()
Set mObj = Nothing
End Sub


class MyClass(Object):

__mObj = Collection()

def Class_Terminate(self):
self.__mObj = None