| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
============== |
| 4 |
GObject.Object |
| 5 |
============== |
| 6 |
|
| 7 |
Compare to other types, :obj:`GObject.Object` has the best integration between |
| 8 |
the GObject and Python type system. |
| 9 |
|
| 10 |
1) It is possible to subclass a :obj:`GObject.Object`. Subclassing |
| 11 |
creates a new :obj:`GObject.GType` which is connected to the new Python |
| 12 |
type. This means you can use it with API which takes :obj:`GObject.GType`. |
| 13 |
2) The Python wrapper instance for a :obj:`GObject.Object` is always the same. |
| 14 |
For the same C instance you will always get the same Python instance. |
| 15 |
|
| 16 |
|
| 17 |
In addition :obj:`GObject.Object` has support for :any:`signals <signals>` and |
| 18 |
:any:`properties <properties>` |
| 19 |
|
| 20 |
.. note:: |
| 21 |
Due to how PyGObject deals with GObject internally, it's not possible to use |
| 22 |
`slots <https://docs.python.org/3/reference/datamodel.html#slots>`_ (``__slots__``). |
| 23 |
|
| 24 |
.. toctree:: |
| 25 |
:titlesonly: |
| 26 |
:maxdepth: 1 |
| 27 |
:hidden: |
| 28 |
|
| 29 |
signals |
| 30 |
properties |
| 31 |
weakrefs |
| 32 |
|
| 33 |
|
| 34 |
Examples |
| 35 |
-------- |
| 36 |
|
| 37 |
Subclassing: |
| 38 |
|
| 39 |
.. code:: pycon |
| 40 |
|
| 41 |
>>> from gi.repository import GObject |
| 42 |
>>> class A(GObject.Object): |
| 43 |
... pass |
| 44 |
... |
| 45 |
>>> A() |
| 46 |
<__main__.A object at 0x7f9113fc3280 (__main__+A at 0x559d9861acc0)> |
| 47 |
>>> A.__gtype__ |
| 48 |
<GType __main__+A (94135355573712)> |
| 49 |
>>> A.__gtype__.name |
| 50 |
'__main__+A' |
| 51 |
>>> |
| 52 |
|
| 53 |
In case you want to specify the GType name we have to provide a |
| 54 |
``__gtype_name__``: |
| 55 |
|
| 56 |
.. code:: pycon |
| 57 |
|
| 58 |
>>> from gi.repository import GObject |
| 59 |
>>> class B(GObject.Object): |
| 60 |
... __gtype_name__ = "MyName" |
| 61 |
... |
| 62 |
>>> B.__gtype__ |
| 63 |
<GType MyName (94830143629776)> |
| 64 |
>>> |
| 65 |
|
| 66 |
:obj:`GObject.Object` only supports single inheritance, this means you can |
| 67 |
only subclass one :obj:`GObject.Object`, but multiple Python classes: |
| 68 |
|
| 69 |
.. code:: pycon |
| 70 |
|
| 71 |
>>> from gi.repository import GObject |
| 72 |
>>> class MixinA(object): |
| 73 |
... pass |
| 74 |
... |
| 75 |
>>> class MixinB(object): |
| 76 |
... pass |
| 77 |
... |
| 78 |
>>> class MyClass(GObject.Object, MixinA, MixinB): |
| 79 |
... pass |
| 80 |
... |
| 81 |
>>> instance = MyClass() |
| 82 |
|
| 83 |
|
| 84 |
Here we can see how we create a :obj:`Gio.ListStore` for our new subclass and |
| 85 |
that we get back the same Python instance we put into it: |
| 86 |
|
| 87 |
.. code:: pycon |
| 88 |
|
| 89 |
>>> from gi.repository import GObject, Gio |
| 90 |
>>> class A(GObject.Object): |
| 91 |
... pass |
| 92 |
... |
| 93 |
>>> store = Gio.ListStore.new(A) |
| 94 |
>>> instance = A() |
| 95 |
>>> store.append(instance) |
| 96 |
>>> store.get_item(0) is instance |
| 97 |
True |
| 98 |
>>> |