GLib.Variant

測試環境

  • Xubuntu 16.04 64位元

執行

$ lsb_release -a

顯示

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04 LTS
Release:    16.04
Codename:   xenial

執行

$ python3 -V

顯示

Python 3.5.1+

執行

$ dpkg -l python3-gi

顯示

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                     Version           Architecture      Description
+++-========================-=================-=================-======================================================
ii  python3-gi               3.20.0-0ubuntu1   amd64             Python 3 bindings for gobject-introspection libraries

進入 Python Console

執行

$ python3

顯示

Python 3.5.1+ (default, Mar 30 2016, 22:46:26)
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

載入相關模組

載入「GLib」

>>> from gi.repository import GLib

觀看 help

觀看「GLib.Variant」Help

>>> help(GLib.Variant)

查詢「new

輸入下面,按下「Enter」

/__new__

可以找到如下列的文字

|  __new__(cls, format_string, value)
|      Create a GVariant from a native Python object.
|      
|      format_string is a standard GVariant type signature, value is a Python
|      object whose structure has to match the signature.
|      
|      Examples:
|        GLib.Variant('i', 1)
|        GLib.Variant('(is)', (1, 'hello'))
|        GLib.Variant('(asa{sv})', ([], {'foo': GLib.Variant('b', True),
|                                        'bar': GLib.Variant('i', 2)}))

...略...

輸入「q」,回到「Python console」。(操作上應該跟less類似)

輸入

>>> print(GLib.Variant.__doc__)

顯示

:Constructors:

::

    new_array(child_type:GLib.VariantType=None, children:list=None) -> GLib.Variant
    new_boolean(value:bool) -> GLib.Variant
    new_byte(value:int) -> GLib.Variant
    new_bytestring(string:list) -> GLib.Variant
    new_bytestring_array(strv:list) -> GLib.Variant
    new_dict_entry(key:GLib.Variant, value:GLib.Variant) -> GLib.Variant
    new_double(value:float) -> GLib.Variant
    new_fixed_array(element_type:GLib.VariantType, elements, n_elements:int, element_size:int) -> GLib.Variant
    new_from_bytes(type:GLib.VariantType, bytes:GLib.Bytes, trusted:bool) -> GLib.Variant
    new_from_data(type:GLib.VariantType, data:list, trusted:bool, notify:GLib.DestroyNotify, user_data) -> GLib.Variant
    new_handle(value:int) -> GLib.Variant
    new_int16(value:int) -> GLib.Variant
    new_int32(value:int) -> GLib.Variant
    new_int64(value:int) -> GLib.Variant
    new_maybe(child_type:GLib.VariantType=None, child:GLib.Variant=None) -> GLib.Variant
    new_object_path(object_path:str) -> GLib.Variant
    new_objv(strv:list) -> GLib.Variant
    new_signature(signature:str) -> GLib.Variant
    new_string(string:str) -> GLib.Variant
    new_strv(strv:list) -> GLib.Variant
    new_tuple(children:list) -> GLib.Variant
    new_uint16(value:int) -> GLib.Variant
    new_uint32(value:int) -> GLib.Variant
    new_uint64(value:int) -> GLib.Variant
    new_variant(value:GLib.Variant) -> GLib.Variant

使用範例

輸入

>>> GLib.Variant('i', 1)

顯示

GLib.Variant('i', 1)

輸入

>>> GLib.Variant('()', ())

顯示

GLib.Variant('()', ())

輸入

>>> GLib.Variant('(s)', ('chewing',))

顯示

GLib.Variant('(s)', ('chewing',))

輸入

>>> args = GLib.Variant('(s)', ('chewing',))

輸入

>>> print(args)

顯示

('chewing',)

輸入

>>> fruit = GLib.Variant('(ss)', ('apple', 'banana'))

輸入

fruit

顯示

GLib.Variant('(ss)', ('apple', 'banana'))

輸入

>>> print(fruit)

顯示

('apple', 'banana')

參考文件

Api

另外可以從「Gnome Developer / GLib Reference Manual / GVariant Format Strings

找到

Symbols

...略...

()      Used for building or deconstructing tuples. See Tuples below.

...略...

還有找到

Tuples

Characters: ()

Tuples are handled by handling each item in the tuple, in sequence. Each item is handled in the usual way.
Examples

...略...