What's better about Ruby than Python?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Mon Aug 18 22:03:04 EDT 2003


On 19 Aug 2003 01:46:20 +0100, Alexander Schmolck <a.schmolck at gmx.net>
wrote:

>Gonçalo Rodrigues <op73418 at mail.telepac.pt> writes:
>
>> On 18 Aug 2003 23:40:58 +0100, Alexander Schmolck <a.schmolck at gmx.net>
>> wrote:
>
>> >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?
>
>Well, yes, I propose that x.__class__ return the *new* definition of X (like
>in ruby, for example). I don't think that necessarily involves rebinding
>anything "automagical", depending on how the lookup of x.__class__ works.
>
>Can you give my any reason why you would *not* want all instances to be
>updated on class redefinition?
>

But in the example you gave you were not *redefining* the class
referenced by X! You are *rebinding* the name X to a different class,
two completely different things. So no, I do not want

rebinding the name X => Automagically change instances of X.__class__
to the new class

This strikes me as a very insane thing to do. That, or else I'm not
understanding a thing of what you are saying -- which given that it's
3am and I am tired, it's not that farfetched.

>
>> 
>> 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?
>
>Um, didn't you read what I wrote or was I just unclear?
>
>To recap: usually, if I change a class I'd like all pre-existing instances to
>become updated (Let's say you develop your program with a running python
>session; you notice a mistake in a method-definition but don't want to start
>over from scratch; you just want to update the class definition and have this
>update percolate to all the currently existing instances. This is an *very*
>reasonable wish if it doesn't just take seconds to get to the current state by
>rerunning everything, but possibly hours or days). AFAIK doing this in a
>general and painfree fashion is pretty much impossible in python (you have to
>first track down all instances -- not an easy task, as far as I can see and
>then updating their .__class__ might not quite work as expected either as I've
>already mentioned).
>
>I think this sucks big time, because it greatly devalues the interactive
>programming experience for certain tasks, IMHO, but maybe I'm just missing
>something since typically nobody complains.
>

Um, you were unclear. I still can't understand why the following does
not work for you:

>>> class X(object):
... 	def amethod(self):
... 		return "I am %r." % self
... 	
>>> x = X()
>>> x.amethod()
'I am <__main__.X object at 0x010D4C10>.'
>>> X.amethod = lambda self: "I am not who I am."
>>> x.amethod()
'I am not who I am.'
>>> 

It seems to cover the use case you describe above.

There are other, more "violent" means of mucking with a class
hierarchy, such as rebinding __class__ or __bases__ directly but that
does not always work and is fraught with dangers.

With my best regards,
G. Rodrigues




More information about the Python-list mailing list