Bob.Cowdery at Bob.Cowdery at
Wed Dec 22 18:16:28 CET 2004


Yes you understand exectly what I am trying to do, my test code was not much
more than you show. You are right that to do the simple thing of adding
attributes I can do that in-line as it were but there may be other
customisations that I want to do. I am probably just not understanding the
process here but in both the examples you give the code is executed on
'import' so I end up with a class that has fixed function depending on what
capability it picked up at 'import' time. What I want to do is when

class API(object):
    __metaclass__ = MetaAPI

is created that MetaAPI generates attributes from a given capability map and
not the one it picked up on 'import'. Does that make any sense?


-----Original Message-----
From: Robert Brewer [mailto:fumanchu at] 
Sent: 22 December 2004 17:01
To: Bob.Cowdery at; python-list at
Subject: RE: Metaclasses

Bob Cowdery wrote:
> I am trying to build a capability based API. That is,
> an instance of the api will reflect the capabilities
> of some underlying services. I could have several
> different instances of the api concurrently running
> against different back end services. A ui applet will
> bind to an api instance. I want to make this easy to
> use from the ui perspective so I envisage exposing a
> number of properties as the variable part can be
> represented by properties. I want a property to exist
> only if a capability is available so it is easily tested.
> I have tried using Property and Metaclasses to do this
> and although both work I can't figure out how to make multiple 
> different instances as both get executed on 'import' and thus only one 
> instance can be created. Inside the metaclass I have to get hold of 
> the capability map I want to build the instance against. The only way 
> I can see to do this at the moment is to have a metaclass
> for each capability type and hardcode the type inside it,
> then pick the appropriate metaclass when I build the
> implementation class.
I was with you until the last couple of sentences. :) I *think* you're
trying to write something like this:

class MetaAPI(type):
    def __init__(cls, name, bases, dct):
        for name, method in capabilities.iteritems():
            setattr(cls, name, method)

class API(object):
    __metaclass__ = MetaAPI

...where all capable behaviors are attached to the API _class_.

But then you start talking about "instances", and I'm not sure whether you
mean instances of the API class or not, because the metaclass doesn't have
much to do with that. If you want to conditionally attach methods to
instances of API, then just do it in API.__init__():

class API(object):
    def __init__(self, keyring):
        for name, method in capabilities(keyring).iteritems():
            setattr(self, name, method)

Can you show us some code? <:)

Robert Brewer
Amor Ministries
fumanchu at

*** Confidentiality Notice *** Proprietary/Confidential
Information belonging to CGI Group Inc. and its affiliates
may be contained in this message. If you are not a recipient
indicated or intended in this message (or responsible for
delivery of this message to such person), or you think for
any reason that this message may have been addressed to you
in error, you may not use or copy or deliver this message
to anyone else.  In such case, you should destroy this
message and are asked to notify the sender by reply email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list