macro FAQ

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Mon Aug 25 09:50:06 CEST 2003


On 24 Aug 2003 16:34:08 +0200, rumours say that Jacek Generowicz
<jacek.generowicz at cern.ch> might have written:

[See at the end for an interactive session transcript -- a simple copy -
paste]

>> No, wait a minute -- instances, not classes??  Sounds like an abuse of
>> the language to me.  What *is* the underlying problem to be solved
>> here?
>
>It started in the Ruby thread: in Ruby classes are "open".
>
>Say you're developing a class (foo), make a gazillion instances of it,
>and play around with them as you are continuing to develop your
>program. Suddenly you realize "Darn, I really need a fubar method!"
>
>In Ruby you do something like:
>
>class foo
>   def fubar ...
>
>   end
>end
>
>All your existing gazillion instances of foo have just acquired a
>fubar method, and you can happily continue your exploration with all
>the state you built up still intact.
>
>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 #####
-- 
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.




More information about the Python-list mailing list