Difference in Python and Ruby interactive shells

dmh2000 dmh2000 at gmail.com
Tue Apr 4 10:00:04 EDT 2006


I am experimenting with the interactive interpreter environments of
Python and Ruby and I ran into what seems to be a fundamental
difference. However I may be doing something wrong in Python. Please
comment and correct me if I am wrong

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.

here is a simple example:

-------------------------------------------------------------------------------
Python interactive shell (python.exe)

C:\home\dh0072\rq>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

# load a local file b.py for test

>>> import b

# instantiate an object and call a method

>>> x = b.B()
>>> x.p()
B

# edit b.py offline to change method 'p' to do something different then
reload

>>> reload(b)

>>> x.p()
B

# binding of variable 'x' IS NOT changed. points to old version

>>> y = b.B()
>>> y.p()
BBBBB

# new instance of 'b' points to new version

>>>
-------------------------------------------------------------------------------
Ruby interactive shell (irb.exe)

C:\home\dh0072\rq>irb

# load a local file b.py for test

irb(main):001:0> load "b.rb"
=> true

# instantiate an object and call a method

irb(main):002:0> x = B.new
=> #
irb(main):003:0> x.p
B
=> nil

# edit b.py offline to change method 'p' to do something different then
reload

irb(main):004:0> load "b.rb"
=> true
irb(main):005:0> x.p
BBBB
=> nil

# binding of variable 'x' IS changed. points to new version

irb(main):006:0> y = B.new
=> #
irb(main):007:0> y.p
BBBB
=> nil

#  new instance of 'b' points to new version




More information about the Python-list mailing list