What's better about Ruby than Python?

Pedro Werneck pedro.werneck at bol.com.br
Mon Aug 18 17:28:16 EDT 2003


On 18 Aug 2003 23:40:58 +0100
Alexander Schmolck <a.schmolck at gmx.net> wrote:

> Does it make sense now? I say "something like" because I could also live with
> something like ``sys.update_all_instances_of(X)``. What I can't live with is
> the (according to my current understanding) entirely brain-dead ``x.__class__
> = X`` for every `x` I can lay my hands on, with quite a bit of labour (and
> which furthermore tends to break pickling etc). I guess I must be overlooking
> something here, because to me it seems entirely obvious that there should be a
> straightforward mechanism for updating all instances of a class when the class
> is redefined.

Well... I think you're overlooking the fact that you're not exactly _redefining_ the class. You're creating an entirely new class object and binding it to the same name, but the old class is still lying there, and all instances have their __class__ references pointing to it. 

class X:
    def amethod(self): return "just a method"

# and
import types

def amethod(self): return "just a method"

X = types.ClassType('X', (), {'amethod':amethod})

#

So, I may be completely wrong, but I think that redefining the entire class rather than methods on the former class, is not possible in Python, unless you keep references of instances and iterate over them updating their __class__ to point to the new class object. 





More information about the Python-list mailing list