What's better about Ruby than Python?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Tue Aug 19 01:30:42 CEST 2003


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

>Alex Martelli <aleaxit at yahoo.com> writes:
>
>> Alexander Schmolck wrote:
>>    ...
>> > I recall the following, roughly in order of importance (treat with
>> > caution, it's some time that I looked at Ruby):
>> > 
>> > 0. I it's possible to redefine classes at runtime without going bonkers
>> >    (instances automatically get updated to the new class definitions).
>> 
>> This is generally feasible in Python, too -- but not with built-in types
>> (e.g., you can't redefine what "+" means on integers, while in Ruby you
>> could).
>
>I don't care about redefining built-in types, I *do* care about redefining the
>behavior of all instances of a class automatically, rather than by hand and
>only for some cases (like classes without those damned __slots__).
> 
>> >    This, I think is by far python's greatest flaw, amongst other things it
>> >    greatly compromises its interactiveness, which is a key virtue. If
>> >    someone can explain to me how I'm wrong on this and python's behavior
>> >    really is sane, I'll be eternally grateful.
>> 
>> I don't think I understand what you're saying.  For example:
>> 
>> >>> class X:
>> ...   def amethod(self): return 'just a method'
>> ...
>> >>> x=X()
>> >>> x.amethod()
>> 'just a method'
>> >>> def abettermethod(self): return 'ah, now THIS is better!'
>> ...
>> >>> X.amethod = abettermethod
>> >>> x.amethod()
>> 'ah, now THIS is better!'
>> >>>
>
>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!'
>
>But of course what currently happens is:
>
>'just a method'
>

And rightly so,  you have just rebinded a name. If you type

x.__class__

you get the older X. Or are you proposing that rebinding X to
automagically rebind the __class__ of all the X instances?

But why can't you just

X.amethod = lambda self: "ah, now THIS is better!"

that is, mutate the class referenced by X instead of rebinding X to
some other class?

With my best regards,
G. Rodrigues





More information about the Python-list mailing list