macro FAQ

Jacek Generowicz jacek.generowicz at cern.ch
Mon Aug 25 07:01:40 EDT 2003


Christos "TZOTZIOY" Georgiou <tzot at sil-tec.gr> writes:

> On 24 Aug 2003 16:34:08 +0200, rumours say that Jacek Generowicz
> <jacek.generowicz at cern.ch> might have written:
> 
> >The first thing you might try to do in Python is to add the fubar
> >method to the class source code, and re-evaluate it ... only to find
> >that your existing instances know nothing about the new method ... so
> >you have to go through the hassle of re-creating a gazillion of them,
> >before you can continue with what you were doing.
> 
> ##### code starts here #####
> 
> >>> class A(object):
> 	pass
> 
> >>> a= A()
> >>> a.test()
> Traceback (most recent call last):
>   File "<pyshell#21>", line 1, in ?
>     a.test()
> AttributeError: 'A' object has no attribute 'test'
> >>> def test(self):
> 	print id(self)
> 
> 	
> >>> A.test = test
> >>> a.test()
> 13111376
> 
> ##### code ends here #####

No, you are missing the point. The point is that you want your source
code to be in sync with your program state, and you want your source
code to look natural. By "natural" I mean that you want your source
code to look like this:

   class A(object):
       def test(self):
           print id(self)

rather than like this:

   class A(object):
       pass

   def test(self):
       print id(self)

   A.test = test

If you still don't understand the motivation, please read the original
thread; there's no point in repeating the arguments all over again in
this thread, which is about something completely different.




More information about the Python-list mailing list