Access to static members from inside a method decorator?
Bruno Desthuilliers
onurb at xiludom.gro
Thu Oct 5 13:31:02 EDT 2006
glen.coates.bigworld at gmail.com wrote:
> Bruno Desthuilliers wrote:
(snip)
>> class Exposing(object):
>> @classmethod
>> def get_exposed_methods(cls):
>> try:
>> exposeds = cls._exposed_methods
>> except AttributeError:
>> exposeds = []
>> for name in dir(cls):
>> obj = getattr(cls, name)
>> if exposed(obj):
>> exposeds.append(obj)
>> cls._exposed_methods = exposeds
>> return exposeds
>>
>> class Parrot(Exposing):
>> @expose
>> def parrot(self, what):
>> return "%s says %s" % (self, str(what))
>
> Thanks Bruno. I came up with a similar solution today at work, which
> involves an 'init' method which is called at the bottom of each module
> that defines subclasses of Exposed and sets up static mappings for the
> exposed methods. I guess my solution is slightly less elegant because
> it requires this ugly explicit init call outside the classes that it
> actually deals with,
This is only a problem in a framework-style use.
> however it is more efficient because the dir()
> pass happens once on module load, instead of every time I want the list
> of exposed methods.
I think you have missed a point in my solution : the 'collect' pass
happens only once for each class - the first time the
get_exposed_methods is called on the class (or any instance of). It's
then memoized for latter calls.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list