What's better about Ruby than Python?

Michele Simionato mis6 at pitt.edu
Mon Aug 18 21:39:25 EDT 2003


Alexander Schmolck <a.schmolck at gmx.net> wrote in message news:<yfs65kubnd1.fsf at black132.ex.ac.uk>...
> I want something like:
> 
> >>> class X:
> ...   def amethod(self): return 'just a method'
> ...
> >>> x=X()
> >>> class X:
> ...   def amethod(self): return 'ah, now THIS is better!'
> ...
> >>> x.amethod()
> 'ah, now THIS is better!'
> 

Yeah, I see what you mean. I have dreamed of redefining a base
classes and having all its children automatically updated. This
would be a potential source of confusion, however (i.e. I redefine my 
class at line 1242 of my code and the behavior of all the classes defined 
in the previous thousand of lines is magically altered, making harder
to understand what the program is doing).

Currently you can add methods to a base class instead, and it does work
under inheritance too:

>>> class B(object):
...    pass
>>> class C(B):
...     pass
>>> c=C()
>>> B.m=lambda self:None
>>> c.m()

You can also change the class of an instance:

>>> c.__class__=B

However this does not work always in Python 2.3 (there is an issue
with "heap" classes, a limitation to avoid redefining the boolean type).

> 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.
> 
> Since I know of no such mechanism and python's current semantics seem pretty
> moronic to me, I'm sure I must be missing something (python has an amazingly
> low rate of "obviously stupid" design decisions, that still strike you as
> stupid after you understood their rationale). So it's maybe about time 
> someone put me straight.

Regular instances are automatically updated when you modify their class;
however if I remember correctly I had problems updating classes which
metaclass was modified (don't remember what problem exactly).

               Michele




More information about the Python-list mailing list