[IPython-user] ?? subtleties / confusion

Jouni K. Seppänen jks at iki.fi
Thu Jan 11 04:28:53 CST 2007

belinda thom <bthom at cs.hmc.edu> writes:

> The deal is this: where ever IPython gets its
>    Source: ...
> listing, it seems to be coming from disk (as opposed to what's really  
> in the interactive namespace). 

Yes, and the problem is compounded by the fact that old objects retain
their method definitions and are not updated when the class is
redefined. That is, if you do x = MyClass() and then change the
definition of MyClass.mymethod(), x.mymethod() calls the old method no
matter what you do to reload the new definition of MyClass. 

One trick that I have sometimes used to get around this is

  from cPickle import loads, dumps
  x = loads(dumps(x))

which serializes x into a string and then creates a new object (which
will belong to the current definition of the class) from the
serialization. This is probably not quite kosher, as any referenced
objects will be re-created too:

| In [103]:class One: pass
|    .....:
| In [104]:class Two: pass
|    .....:
| In [105]:x = One(); y = Two(); x.two = y
| In [106]:x.two is y; x = loads(dumps(x)); x.two is y
| Out[106]:True
| Out[106]:False

I wonder if there are any other potential problems caused by this? 
(Of course if you change the data members of the class, this will
cause problems, but that's to be expected.)

> I am wondering if it would be difficult to attach the actual code  
> that objects have, so that this introspection could be more useful  

I think this would require changes to Python itself.

Jouni K. Seppänen

More information about the IPython-user mailing list