G
glen.coates.bigworld
I'm developing a library at the moment that involves many classes, some
of which have "exposed" capabilities. I'm trying to design a nice
interface for both exposing those capabilities, and inspecting
instances to find out what capabilities they have.
At the moment, I'm leaning towards a superclass (Exposed) that defines
a static method which is a decorator (expose) such that any derived
class can mark a method with @Exposed.expose and it will then be later
returned by getExposedMethods(), a la:
class Exposed:
@staticmethod
def expose( f ):
...
def getExposedMethods( self ):
...
class Person( Exposed ):
@Exposed.expose
def talk( self, ... ):
...
I'm trying to implement the decorator by having it populate a static
member list of whatever class it's in with a reference to the method.
getExposedMethods() would then return the contents of each of those
lists from itself back to Exposed in the class hierarchy. The first
problem was that having a reference to the method (i.e. talk()) does
not allow you to get a reference to the enclosing class (I had hoped
im_class would lead me there). The real hiccup was that explicitly
passing the class as an argument to the decorator generates a undefined
global name error, presumably because at that point of execution the
class object hasn't been fully created/initialised.
So how can this be done? It doesn't seem like it's possible to pass a
reference to the enclosing class into the decorator, which in turn
means that static tracking of the list of exposed methods is impossible
(at least, if I want to use decorators).
Any ideas that will enable my initial design, or suggestions for an
elegant, workable alternative would be much appreciated.
Cheers,
Glen
of which have "exposed" capabilities. I'm trying to design a nice
interface for both exposing those capabilities, and inspecting
instances to find out what capabilities they have.
At the moment, I'm leaning towards a superclass (Exposed) that defines
a static method which is a decorator (expose) such that any derived
class can mark a method with @Exposed.expose and it will then be later
returned by getExposedMethods(), a la:
class Exposed:
@staticmethod
def expose( f ):
...
def getExposedMethods( self ):
...
class Person( Exposed ):
@Exposed.expose
def talk( self, ... ):
...
I'm trying to implement the decorator by having it populate a static
member list of whatever class it's in with a reference to the method.
getExposedMethods() would then return the contents of each of those
lists from itself back to Exposed in the class hierarchy. The first
problem was that having a reference to the method (i.e. talk()) does
not allow you to get a reference to the enclosing class (I had hoped
im_class would lead me there). The real hiccup was that explicitly
passing the class as an argument to the decorator generates a undefined
global name error, presumably because at that point of execution the
class object hasn't been fully created/initialised.
So how can this be done? It doesn't seem like it's possible to pass a
reference to the enclosing class into the decorator, which in turn
means that static tracking of the list of exposed methods is impossible
(at least, if I want to use decorators).
Any ideas that will enable my initial design, or suggestions for an
elegant, workable alternative would be much appreciated.
Cheers,
Glen