Difference in Python and Ruby interactive shells

Diez B. Roggisch deets at nospam.web.de
Tue Apr 4 10:39:01 EDT 2006


> In both languages, you can start up the interactive interpreter
> ('python' and 'irb'), load source files and do stuff, like create
> objects and call their methods. When you want to change something, you
> can edit those same source files outside the environment and reload
> them from within the interactive environment. But, here is the
> difference: with Python, when you reload the source file (module in
> Python terms), it seems that your existing variables stay bound to the
> implementations from the old version of the module. In Ruby, after a
> reload, your existing variables point to the new implementation. At
> least, that is what happens with classes and their methods.  This means
> that in Python, if you have an application built up interactively, with
> references to objects, data structures pointing to objects, etc., you
> would have to reconstruct that application to get the new
> implementation. With Ruby, when you load the new implementation, you
> get it immediately.

I don't know ruby enough to comment on _how_ they achieve that. But  I know
python enough to tell you that your observation is correct, and perfectly
consistent with what python ususally does. 

The problem here stems from the fact that in python, there are names and
values. The latter (also referred to as objects) can be bound to a name. 

Now loading a module creates class-objects, and binds them under a name -
the class-name. But this name is by no means special. You can e.g. do

class Foo:
  pass

Foo = 10

That will create a class that is bound to the name Foo - and the rebind that
very name to something completely different(tm).

Now creating an instance of that class creates an object, that has a
_reference_ to its class - not its name! 

Which means that there is no connection to the _name_ of that class! Well,
one can get to that, because the class knows the name it was bound to when
it was created - but that is only for documentary purposes.

When reloading a module, a new class object is created. And bound to the
name same name as before - but all instances created before keep a
reference to their old class. Which makes perfect sense: in python, it is
possible to create classes individually for each instance, if one wants to
- by metaclasses, or factoryfunctions like this:

def createClass():
   class Foo:
      pass
   return Foo

So - if that works, there is no way how one can know that this reloading of
a module is anything special.

Regards,

Diez



More information about the Python-list mailing list