Hi everyone,
I have been playing with metaclasses in Python and thought the
following code was cool...
It is the signal.py demo with a metaclass. I bet it is pretty easy to
add the metaclass attribute to the C PyGObject class somewhere in
gobjectmodule.c... then we could forget about type_register and the
__metaclass__ attribute.
Of course you can also look at the 'dict' from the class statment and
create signals and and properties according to __signals__,
__properties__,
or something like that, but I understand those features are already in
CVS,
though I don't know if they were implemented using a metaclass or not.
-Rob
import gobject
class PyGObjectSubClass(type):
# called before __init__ to actually create an object
# here we are creating a class (or type) object
def __new__(t, cname, bases, dict):
# super is probably overkill here, type.__new__ would be fine
klass = super(PyGObjectSubClass, t).__new__(t, cname, bases,
dict)
print 'Created new class, now registering it.'
gobject.type_register(klass)
return klass
class C(gobject.GObject):
# we must tell Python to create this class as an 'instance' of
# PyGObjectSubclass, rather than plain type. We
# could also inherit the metaclass attribute from gobject.GObject
__metaclass__ = PyGObjectSubClass
def __init__(self):
self.__gobject_init__() # default constructor using our new
GType
def do_my_signal(self, arg):
print "C: class closure for `my_signal' called with argument",
arg
# gobject.type_register(C)
gobject.signal_new("my-signal", C, gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, (gobject.TYPE_INT, ))
class D(C):
def do_my_signal(self, arg):
print "D: class closure for `my_signal' called. Chaining up to
C"
C.do_my_signal(self, arg)
# gobject.type_register(D)
def my_signal_handler(object, arg, *extra):
print "handler for `my_signal' called with argument", arg, \
"and extra args", extra
inst = C()
inst2 = D()
inst.connect("my_signal", my_signal_handler, 1, 2, 3)
inst.emit("my_signal", 42)
inst2.emit("my_signal", 42)
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk