Peter Otten wrote:
>glen.coates.bigworld at gmail.com wrote:
>> You define one base type with a custom metaclass and inherit from that. Your
> example then becomes:
>> import sys
>> class ExposedType( type ):
> def __init__( cls, *args, **kw ):
> # Track marked exposed methods
> cls.s_exposedMethods = []
> for superclass in cls.__mro__:
> for name, meth in superclass.__dict__.items():
> if hasattr( meth, "exposed" ):
> cls.s_exposedMethods.append( name )
>> class Exposed:
> __metaclass__ = ExposedType
> def getExposedMethods(self):
> return self.s_exposedMethods
> def bar(self):
> print "bar\n"
> @staticmethod
> def expose( f ):
> f.exposed = True
> return f
>> class Process( Exposed):
> @Exposed.expose
> def foo( self ):
> pass
> def bar( self ):
> print "BAR"
>>> class BotProcess( Process ):
> @Exposed.expose
> def addBots( self ):
> pass
>> p = Process()
> p.bar()
>> This prints "BAR" as expected.
>> Peter
Thanks Peter. Yeah I had thought of that earlier, but wasn't sure if
this is a standard design pattern for what I'm trying to achieve. It
seems ugly to me to use 2 classes when you are essentially describing a
single type. Is the Splat/SplatType pairing really a common design
pattern when trying to use metaclasses in this way?
Also ... as for the 'inspect' based solution, yeah I can see that would
work, but it seems very hacky to me and my gut reaction is to avoid
that kind of thing ...
Cheers,
Glen