Add(Item: Variant);
Add(Item: Object);
Item. The value that will be added to the list.
The Add method adds the specified value to the list.
Python lists may contain different types of values that is why the Add method can be used to add both simple type values and various objects that are Python class instances to the list.
Executing the example requires a folder with units: d:\Work\Python\. The folder contains the sample.en unit. The following function is implemented in the unit:
def getSizeOfList(l):
return l.__sizeof__()
Add a link to the Python system assembly.
Sub UserProc;
Var
pUtils: IPythonUtils;
pList: IPythonList;
Result: Variant;
Begin
pUtils := New PythonUtils.Create;
pUtils.AddFolderToPythonPath("d:\Work\Python\");
//Create a list
pList := New PythonList.Create(0, "A", Char.Chr(169)); //169 code of the © sign
//Get information about the list
Result := pUtils.Invoke("sample", "getSizeOfList", pList);
Debug.WriteLine("Number of list elements: " + pList.Length.ToString);
Debug.WriteLine("Size of memory used (bites): " + Result);
//Add elements
pList.Add(Integer.MinValue);
pList.Add(Double.MaxValue);
pList.Add(DateTime.Now.ToString);
//Repeat getting information about list
Result := pUtils.Invoke("sample", "getSizeOfList", pList);
Debug.WriteLine("Number of list elements: " + pList.Length.ToString);
Debug.WriteLine("Size of memory used (bites): " + Result);
End Sub UserProc;
Imports Prognoz.Platform.Interop.Python;
Public Shared Sub Main(Params: StartParams);
Var
pUtils: IPythonUtils = New PythonUtilsClass();
pList: IPythonList = New PythonListClass();
Result: Object;
Begin
pUtils.AddFolderToPythonPath("d:\Work\Python\");
//Create a list
pList.Create(0, "A", Char.ConvertFromUtf32(169)); //169 code of the © sign
//Get information about the list
Result := pUtils.Invoke("sample", "getSizeOfList", pList);
System.Diagnostics.Debug.WriteLine("Number of elements in the list: " + pList.Length.ToString());
System.Diagnostics.Debug.WriteLine("Size of memory used (bites): " + Result);
//Add elements
pList.Add(Integer.MinValue);
pList.Add(Double.MaxValue);
pList.Add(DateTime.Now.ToString());
//Repeat getting information about list
Result := pUtils.Invoke("sample", "getSizeOfList", pList);
System.Diagnostics.Debug.WriteLine("Number of elements in list: " + pList.Length.ToString());
System.Diagnostics.Debug.WriteLine("Size of memory used (bites): " + Result);
End Sub;
On executing the example an object containing a Python list is created. The list is sent to the getSizeOfList function to get RAM size that it occupies. After this several elements are added to the list, and the getSizeOfList function is called again. The number of elements and size of occupied RAM after creating and adding new elements are displayed in the development environment console.
See also: