Design by Contract for Python

Terence Way terry at wayforward.net
Thu May 15 14:26:51 EDT 2003


You would actually, umm, be correct.  My contribution would have been,
umm, wrong.  Instance methods do have their own dict, but for some reason
you can't add attributes after the class def, even by using setattr.  In
your example after the prints, you can't suddenly say:
>>> print C.f.x
1
>>> C.f.y = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'instance method' object has no attribute 'y'

Which is what I meant to say.  Really.  :-)

Note:
>>> C.f.im_func.y = 42
>>> C.f.y
42

On Thu, 15 May 2003 14:38:18 -0400, Aahz wrote:

> In article <mailman.1053002903.4843.python-list at python.org>, Terence Way
>  <terry at wayforward.net> wrote:
>>
>>Although I like the "grouping" that you propose.  But I'm not sure it
>>would work with inheritance ("virtual" functions).  Or variable
>>arguments.  I wish instance methods could have their own dict, just like
>>static functions.  Then you could do:
>>     class foo:
>>          def func(self, a, b):
>>                ...
>>          func.pre = ...
>>          func.post = ...
> 
> Huh?
> 
> class C(Exception):
>     def f(self):
>         pass
>     f.x = 1
> 
> print C.f.x
> print C().f.x




More information about the Python-list mailing list