• 23
  • Feb

前些日子刚刚领会了Python decorator的神奇之处,现在看到PyGObject支持它了。

import gobject

class MyObject(gobject.GObject):

    foo = gobject.property(type=str, default='bar')
    boolprop = gobject.property(type=bool, default=False)

    def __init__(self):
        gobject.GObject.__init__(self)

    @gobject.property
    def readonly(self):
        return 'readonly'

gobject.type_register(MyObject)

print "MyObject properties: ", list(MyObject.props)

obj = MyObject()

print "obj.foo ==", obj.foo

obj.foo = 'spam'
print "obj.foo = spam"

print "obj.foo == ", obj.foo

print "obj.boolprop == ", obj.boolprop

print obj.readonly
obj.readonly = 'does-not-work'

实际上跟Python内置的property一样,只不过现在的底层变成了GObject。

Tag: gobject, Python.

» You can leave a comment.

No Comment

Leave a Comment