From: Paul Prescod Date: 2003-03-22T02:41:12+09:00 Subject: Re: Strong advantages over Python Lennon Day-Reynolds wrote: > Paul Prescod wrote: > > ... > The specific difference I was trying to illustrate was the ability to, > in Ruby, add a method to an existing class, and have that change then > cascade through to each instance of the class. To use a trivial example, > if I were to add an 'average' method to the array method like so... > > class Array > def average > sum = 0.0 > self.each {|i| sum += i} > sum / self.size > end > end > > ...any existing instances of Array would then have this method > available. In Python, however, I would have to re-define/load the class, > then re-instantiate any objects of that type in order to access the new > instance method. I demonstrated this in a post yesterday. class Foo: pass f = Foo() Foo.special = lambda(self) : "In dynamically assigned method" f.special() On the other hand, it is true that I couldn't do this to the built-in list type. Only user defined classes have writable attributes. I actually discussed this with Guido at one point. His feeling was that in a large system like Zope, with many people sharing an interpreter, (i.e. components written by hundreds of people) potentially across many threads, this would cause problems for basically the same reason that global variables cause problems. These mutable built-in classes _are_ global variables. That said, Python should probably be more uniform in its handling of builtins and user-defined classes. > ... > This actually isn't what I was thinking of at all -- the 'property' type > still requires hand-writing the get/set/del methods, and what I was > remembering (quite possibly incorrectly) was a type which could override > the assignment and value operatations to allow more complex logic to be > implemented. I still don't know what you mean so I'll just drop it. Paul Prescod