CreateFromModule(ModuleId: String; ClassName: String; Params: Array);
ModuleId. Identifier of the module with code in the Python language in repository.
ClassName. Class name. The name is case-sensitive.
Params. Array of parameter values that must be sent to class constructor.
The CreateFromModule constructor creates an object that is a class instance with the specified name.
If a default constructor is used in the Python class, do not specify the Params parameter.
Executing the example requires that the repository contains a Python module with the MOD_PYT identifier. The following class is implemented in the module:
class Door:
def __init__(self, color, height, width):
self.color = color
self.height = height
self.width = width
def getDoorDescription(self, comment):
return comment + ". Color: " + self.color + " Size: " + str(self.height) + "X" + str(self.width) + " mm"
Add links to the Metabase, Python system assemblies.
Sub UserProc;
Var
pObj: IPythonClassObject;
Result: Variant;
Begin
//Create an object of the Door class
pObj := New PythonClassObject.CreateFromModule("MOD_PYT", "Door", "Red", 2000, 800);
//Get value of the color attribute
Result := pObj.GetAttr("color");
Debug.WriteLine(Result);
//Change value of the color attribute
pObj.SetAttr("color", "White");
//Execute class instance function
Result := pObj.Invoke("getDoorDescription", "Interior door");
Debug.WriteLine(Result);
End Sub UserProc;
The following operations are executed on starting the example:
An instance of the Door class is created, which code is saved in the Python module in the repository. The constructor sends values of parameters that will be assigned to attributes.
Value of the "color" attribute is obtained for the created object. The value is displayed in the development environment console.
The new value is set for the "color" attribute.
The function of the getDoorDescription class instance is executed, the obtained result is displayed in the development environment console.
See also: