Skip to content

Commit

Permalink
Update GObject property decorator to class
Browse files Browse the repository at this point in the history
The GObject.Property deprecated the GObject.property as described
in _propertyhelper.py.
  • Loading branch information
mozbugbox committed May 23, 2016
1 parent f94c13c commit 1917aca
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions source/objects.txt
Expand Up @@ -115,16 +115,16 @@ Create new properties

A property is defined with a name and a type. Even if python itself is
dynamically typed, you can't change the type of a property once it is defined. A
property can be created using :func:`GObject.property`.
property can be created using :class:`GObject.Property`.

.. code-block:: python

from gi.repository import GObject

class MyObject(GObject.GObject):

foo = GObject.property(type=str, default='bar')
property_float = GObject.property(type=float)
foo = GObject.Property(type=str, default='bar')
property_float = GObject.Property(type=float)
def __init__(self):
GObject.GObject.__init__(self)

Expand All @@ -139,12 +139,12 @@ Flags are :const:`GObject.PARAM_READABLE` (only read access for external code),

.. code-block:: python

foo = GObject.property(type=str, flags = GObject.PARAM_READABLE) # not writable
bar = GObject.property(type=str, flags = GObject.PARAM_WRITABLE) # not readable
foo = GObject.Property(type=str, flags = GObject.PARAM_READABLE) # not writable
bar = GObject.Property(type=str, flags = GObject.PARAM_WRITABLE) # not readable


You can also define new read-only properties with a new method decorated with
:func:`GObject.property`:
:class:`GObject.Property`:

.. code-block:: python

Expand All @@ -155,7 +155,7 @@ You can also define new read-only properties with a new method decorated with
def __init__(self):
GObject.GObject.__init__(self)

@GObject.property
@GObject.Property
def readonly(self):
return 'This is read-only.'

Expand All @@ -167,6 +167,27 @@ You can get this property using:
print(my_object.readonly)
print(my_object.get_property("readonly"))

The API of :class:`GObject.Property` is similar to the builtin :py:func:`property`. You can create property setter in a way similar to Python property:

.. code-block:: python

class AnotherObject(GObject.Object):
value = 0

@GObject.Property
def prop(self):
'Read only property.'
return 1

@GObject.Property(type=int)
def propInt(self):
'Read-write integer property.'
return self.value

@propInt.setter
def propInt(self, value):
self.value = value

There is also a way to define minimum and maximum values for numbers, using a more verbose form:

.. code-block:: python
Expand Down

0 comments on commit 1917aca

Please sign in to comment.