falstaff
falstaff

Reputation: 3693

Pack a GLib.Variant in Python

The PyGObject API allows to convert a GLib Variant to a Python object using the unpack() method (see GLib.Variant.unpack).

However, is there a direct way to do the reverse?

E.g. I can create arbitrary GLib Variant objects using the Variant constructors:

>>> myglibvariant = GLib.Variant("a{sv}", { 'test': GLib.Variant('s', 'value')})

And use the unpack() function to create a Python object in one step:

>>> myglibvariant.unpack()
{'test': 'value'}

However, is there a way to do the reverse in one step? E.g.

>>> myglibvariant = GLib.Variant.pack({'test': 'value'})

Upvotes: 2

Views: 787

Answers (2)

ptomato
ptomato

Reputation: 57910

No, because you have to specify the type. (In the {'test': 'value'} example, the pack function would have to guess whether the type is a{ss} or a{sv}, or possibly {ss} or {sv} as well.

Upvotes: 1

Related Questions