IPythonDictionary.Update

Syntax

Update(Key: Variant; Value: Variant);

Parameters

Key. Key of added element.

Value. Value of added element.

Description

The Update method updates the dictionary by adding a new element to it containing the specified key-value pair.

Executing the example requires a folder with modules: d:\Work\Python\. The folder contains the sample.py module. The following function is implemented in the module:

def getSizeOfDictionary(d):

return d.__sizeof__()

Add a link to the Python system assembly.

Sub UserProc;
Var
    pUtils: IPythonUtils;
    pDict: IPythonDictionary;
    Result: Variant;
Begin
    pUtils := 
New PythonUtils.Create;
    pUtils.AddFolderToPythonPath(
"d:\Work\Python\");
    
// Create and fill in dictionary
    pDict := New PythonDictionary.Create(Null);
    pDict.Update(
1"A");
    pDict.Update(
2"B");
    pDict.Update(
3"C");

    // Get information about dictionary
    Result := pUtils.Invoke("sample""getSizeOfDictionary", pDict);
    Debug.WriteLine(
"Number of elements in the list: " + pDict.Length.ToString);
    Debug.WriteLine(
"Amount of occupied memory (bytes): " + Result);
    
// Add elements
    pDict.Update(4"D");
    pDict.Update(
5"E");
    pDict.Update(
6"F");
    
// Get information about dictionary
    Result := pUtils.Invoke("sample""getSizeOfDictionary", pDict);
    Debug.WriteLine(
"Number of elements in the list: " + pDict.Length.ToString);
    Debug.WriteLine(
"Amount of occupied memory (bytes): " + Result);
    
// Remove elements
    pDict.Pop(1);
    pDict.Pop(
2);
    pDict.Pop(
6);
    
// Get information about dictionary
    Result := pUtils.Invoke("sample""getSizeOfDictionary", pDict);
    Debug.WriteLine(
"Number of elements in the list: " + pDict.Length.ToString);
    Debug.WriteLine(
"Amount of occupied memory (bytes): " + Result);
End Sub UserProc;

After executing the example an object containing a Python dictionary will be created. The dictionary will be sent to the getSizeOfDictionary function to get RAM size that it occupies. After that, three elements will be added to the dictionary and then will be removed from it. The getSizeOfDictionary function is called at each stage. The number of elements in the list and the amount of used memory after creating, adding, and removing elements will be displayed in the development environment console.

See also:

IPythonDictionary