VariantDict

Added in version 2.40.

class VariantDict(**kwargs)
Constructors:

new(from_asv:GLib.Variant=None) -> GLib.VariantDict

Constructors

class VariantDict
classmethod new(from_asv: Variant | None = None) VariantDict

Allocates and initialises a new VariantDict.

You should call unref() on the return value when it is no longer needed. The memory will not be automatically freed by any other call.

In some cases it may be easier to place a VariantDict directly on the stack of the calling function and initialise it with init(). This is particularly useful when you are using VariantDict to construct a Variant.

Added in version 2.40.

Parameters:

from_asv – the Variant with which to initialise the dictionary

Methods

class VariantDict
clear() None

Releases all memory associated with a VariantDict without freeing the VariantDict structure itself.

It typically only makes sense to do this on a stack-allocated VariantDict if you want to abort building the value part-way through. This function need not be called if you call end() and it also doesn’t need to be called on dicts allocated with g_variant_dict_new (see unref() for that).

It is valid to call this function on either an initialised VariantDict or one that was previously cleared by an earlier call to clear() but it is not valid to call this function on uninitialised memory.

Added in version 2.40.

contains(key: str) bool

Checks if key exists in dict.

Added in version 2.40.

Parameters:

key – the key to look up in the dictionary

end() Variant

Returns the current value of dict as a Variant of type %G_VARIANT_TYPE_VARDICT, clearing it in the process.

It is not permissible to use dict in any way after this call except for reference counting operations (in the case of a heap-allocated VariantDict) or by reinitialising it with init() (in the case of stack-allocated).

Added in version 2.40.

insert_value(key: str, value: Variant) None

Inserts (or replaces) a key in a VariantDict.

value is consumed if it is floating.

Added in version 2.40.

Parameters:
  • key – the key to insert a value for

  • value – the value to insert

lookup_value(key: str, expected_type: VariantType | None = None) Variant | None

Looks up a value in a VariantDict.

If key is not found in dictionary, None is returned.

The expected_type string specifies what type of value is expected. If the value associated with key has a different type then None is returned.

If the key is found and the value has the correct type, it is returned. If expected_type was specified then any non-None return value will have this type.

Added in version 2.40.

Parameters:
  • key – the key to look up in the dictionary

  • expected_type – a VariantType, or None

remove(key: str) bool

Removes a key and its associated value from a VariantDict.

Added in version 2.40.

Parameters:

key – the key to remove

Returns:

0 if the file was successfully removed, -1 if an error occurred