Hooks, aspect-oriented programming, and design by contract

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Tue Jan 22 18:27:02 CET 2002


"Gustavo Niemeyer" <niemeyer at conectiva.com> wrote:

>> Hi,
>> 	maybe I'm just reinventing the wheel, but I just noted that Alex's
>> Aspecter function can easily be twitched to enforce some kind of design
>> by contract (which of course can be considered an aspect). Here's my
>> first shot at it. Corrections and improvements are much welcome.
> 
> Here is some code I've written to play around with metaclasses. It may
> give you some ideas.
> 
<code snipped for brievety>

How do you set up an attributes in your classes ???
How should a class be initialized ?
Notice that :
 - 'print 1' is not executed,
 - A is ContractMeta object,
 - a is a ContractInstance
Shouldn't there be an initialisation function to be called
when creating a ContractInstance ?

The following extension from your exemple fails because 'x' is not found.

if __name__ == "__main__":
    # Usage example
    class A(Contract):

        def __init__(self, *args):
            print 1
            Contract(self, *args)
            self.x = 0

        def invariant(self):
            assert self.x == 0
            print "invariant()"
        
        def a(self):
            self.x = 1
            print "a()"
        
        def a_pre(self):
            print "a_pre()"
        
        def a_post(self, result):
            print "a_post()"
    
    a = A()
    a.a()


Regards,
-- 

Pedro





More information about the Python-list mailing list