[Zope-dev] RFC: RelationAware class for relations between objects

Steve Alexander steve@cat-box.net
Tue, 29 Apr 2003 15:59:47 +0200


> The Relation objects need to be
> created before the classes are created. 

Not any more!


> There needs to be a call that
> tells the Relation about all the classes that participate in the
> Relation.  (Maybe it could be done when the classes are created via a
> custom metaclass, but that seems to messy.)

There's a way to allow a descriptor (or other thing defined inside a 
class suite) to do things to the class immediately after the class is 
created.

Phillip Eby has checked code inspired by my prototype, in turn inspired 
by a conversation I had with GvR.
   (That's the credits out of the way :-) ):

It is available from Phillip's PEAK project, and will probably be 
checked into Zope 3 soon also.


http://cvs.eby-sarna.com/PEAK/src/peak/util/advice.py?rev=HEAD&content-type=text/vnd.viewcvs-markup

And here are some unit tests for it:

http://cvs.eby-sarna.com/PEAK/src/peak/util/tests/advice.py?rev=HEAD&content-type=text/vnd.viewcvs-markup



Using the API looks like this:

   def implements(*interfaces):

       def callback(klass):
           klass.__implements__ = interfaces
           return klass

       addClassAdvisor(callback)


class Foo:

   implements(IFoo)

This adds an __implements__ attribute to the class just after it has 
been created, but before any other code gets to see it.

--
Steve Alexander