Obtaining a callable class method object from a specific class
cocoa at khiltd.com
Thu Apr 10 23:41:22 CEST 2008
On Apr 10, 2008, at 1:25 PM, python-list-request at python.org wrote:
> won't question why you want to do this...
> Here is a solution base on a metaclass, but it feels wrong.
> class MetaScore(type):
> def __new__(meta, name, bases, attrs):
> attrs.setdefault('score', score)
> return type.__new__(meta, name, bases, attrs)
So you're saying to get rid of the inherited __dict__ entry altogether
when the class is defined? I'm worried that this might be somewhat
problematic since there's actually quite a lot of metaclass interplay
going on already that might lead to one stepping on another's toes,
but I may give it a shot.
Some more digging with different keywords turned up this obscure nugget:
"When a user-defined method object is created by retrieving a class
method object from a class or instance, its im_self attribute is the
class itself (the same as the im_class attribute), and its
im_funcattribute is the function object underlying the class method."
I was under the impression that these were completely unbound, but it
looks like I was misinformed. This appears to do what I need it to,
but further testing is in order:
matchfunc = getattr(base, "Score", None)
if matchfunc and matchfunc.im_self == base:
score += matchfunc(arg)
More information about the Python-list