[IPython-user] ?? subtleties / confusion
belinda thom
bthom at cs.hmc.edu
Thu Jan 11 03:01:24 CST 2007
Hello again,
I've been playing more w/namespaces, an exercise Fernando's assured
me would be to my benefit, and it was. It's also revealed to me some
behavior that is confusing.
It is great to use IPython's ?? feature, but it can also be very
misleading.
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). On the one hand, IPython's ?? is a
great addition (wish Python had it). But on the other hand, it can't
be relied upon to describe what code an instance's method is really
executing. If you'd like to see the gory details, I've appended two
cases, one in python (where I don't know how to look at code) and one
in IPython (where I can).
I am wondering if it would be difficult to attach the actual code
that objects have, so that this introspection could be more useful
when trying to better understand what's going on when developing
dynamically and interactively in the IPython shell?
Thanks from your loyal IPython test guru,
--b
-------------------------------
Example in python
-------------------------------
15 % python
Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
history mechanism set up
>>> import test
>>> reload test
File "<stdin>", line 1
reload test
^
SyntaxError: invalid syntax
>>> reload(test)
<module 'test' from 'test.pyc'>
>>> del(test)
>>> import test as T
>>> t = T.foo()
initializing
>>> t.foo()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "test.py", line 21, in foo
print outcome
NameError: global name 'outcome' is not defined
>>> import test
>>> reload(test)
<module 'test' from 'test.py'>
>>> import test as T
>>> t.foo()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "test.py", line 21, in foo
print outcome
NameError: global name 'outcome' is not defined
>>> t = T.foo()
initializing
>>> t.foo()
hello
>>>
-------------------------------
Example in IPython
-------------------------------
18 % ipython
Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
...
IPython 0.7.4.svn.r2010 -- An enhanced Interactive Python.
...
In [1]: import test
In [2]: reload(test)
Out[2]: <module 'test' from 'test.pyc'>
In [3]: del(test)
In [4]: import test as T
In [5]: T??
Type: module
Base Class: <type 'module'>
String Form: <module 'test' from 'test.pyc'>
Namespace: Interactive
File: /Users/bthom/belinda/mills/aicourse/python_exs/test.py
Source:
class foo :
def __init__(self) :
print "initializing"
def foo(self) :
#outcome = "hello"
print outcome
In [6]: t = T.foo()
initializing
In [7]: t.foo()
------------------------------------------------------------------------
---
exceptions.NameError Traceback (most
recent call last)
/Users/bthom/belinda/mills/aicourse/python_exs/<ipython console>
/Users/bthom/belinda/mills/aicourse/python_exs/test.py in foo(self)
17 print "initializing"
18
19 def foo(self) :
20 #outcome = "hello"
---> 21 print outcome
NameError: global name 'outcome' is not defined
In [8]: import test
In [10]: reload(test)
Out[10]: <module 'test' from 'test.py'>
In [11]: del(test)
In [12]: import test as T
In [13]: T??
Type: module
Base Class: <type 'module'>
String Form: <module 'test' from 'test.py'>
Namespace: Interactive
File: /Users/bthom/belinda/mills/aicourse/python_exs/test.py
Source:
class foo :
def __init__(self) :
print "initializing"
def foo(self) :
outcome = "hello"
print outcome
In [14]: t.foo??
Type: instancemethod
...
Source:
def foo(self) :
outcome = "hello"
print outcome
In [15]: t.foo()
------------------------------------------------------------------------
---
exceptions.NameError Traceback (most
recent call last)
/Users/bthom/belinda/mills/aicourse/python_exs/<ipython console>
/Users/bthom/belinda/mills/aicourse/python_exs/test.py in foo(self)
17 print "initializing"
18
19 def foo(self) :
20 outcome = "hello"
---> 21 print outcome
NameError: global name 'outcome' is not defined
In [16]: t = T.foo()
initializing
In [17]: t.foo()
hello
More information about the IPython-user
mailing list