Ivan Voras a écrit : > While using PyGTK, I want to try and define signal handlers > automagically, without explicitly writing the long dictionary (i.e. I > want to use signal_autoconnect()). > > To do this, I need something that will inspect the current "self" and > return a dictionary that looks like: > > { > "method_name" : self.method_name > } > > Class.__dict__ does something very similar, but when I use it, either > I'm doing something wrong or it doesn't return methods bound to "self", > and python complains a wrong number of arguments is being passed to the > methods (one instead of two).
You're not doing anything wrong, that's just how Python works. "methods" are wrapper objects around function objects attributes. The wrapping only happens at lookup time, and returns different kind of "method" wrapper (resp. unbound or bound methods) if the attribute is looked up on an instance or a class (there are also the staticmethod/classmethod things, but that's not your problem here). You can build the dict you're looking for using dir() and getattr(): from types import MethodType autoconnect_dict = {} for name in dir(self): attr = getattr(self, name) if isinstance(attr, MethodType): autoconnect_dict[name] = attr return autoconnect_dict > instance.__dict__ on the other hand returns an empty dictionary. the instance's __dict__ only stores per-instance attributes. While it's technically possible to attach methods per-instance, it's definitively a corner case. -- http://mail.python.org/mailman/listinfo/python-list