GetAttr(AttrName: String): Variant;
GetAttr(AttrName: String): Object;
AttrName. Class argument name. The name is case-sensitive.
The GetAttr method gets values of the argument with the specified name from class instance.
If the class instance does not contain an argument named AttrName, an exception is thrown.
Executing the example requires a folder with units: d:\Work\Python\. The folder contains the sample.en unit. The following class is implemented in the unit:
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 a link to the Python system assembly.
Sub UserProc;
Var
pUtils: IPythonUtils;
pObj: IPythonClassObject;
Result: Variant;
Begin
pUtils := New PythonUtils.Create;
pUtils.AddFolderToPythonPath("d:\Work\Python\");
//Create object of the Door class
pObj := New PythonClassObject.Create("sample", "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 function of the class instance
Result := pObj.Invoke("getDoorDescription", "Connecting door");
Debug.WriteLine(Result);
End Sub UserProc;
Imports Prognoz.Platform.Interop.Python;
Public Shared Sub Main(Params: StartParams);
Var
pUtils: IPythonUtils = New PythonUtilsClass();
pObj: IPythonClassObject = New PythonClassObjectClass();
Result: Object;
Begin
pUtils.AddFolderToPythonPath("d:\Work\Python\");
//Create object of the Door class
pObj.Create("sample", "Door", "Red", 2000, 800);
//Get value of the color attribute
Result := pObj.GetAttr("color");
System.Diagnostics.Debug.WriteLine(Result);
//Change value of the color attribute
pObj.SetAttr("color", "White");
//Execute function of the class instance
Result := pObj.Invoke("getDoorDescription", "Connecting door");
System.Diagnostics.Debug.WriteLine(Result);
End Sub;
The following operations are executed on starting the example:
An instance of the Door class is created. 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: