[IPython-user] Is ipython too nosy?

Alexander Schmolck a.schmolck@gmx....
Wed Feb 28 19:20:01 CST 2007


I would have expected that ipython's (default) policy when the user enters a
line that constitutes a valid python expression and presses return would be to
evaluate the whole expression and not attempt any form of introspection on any
of the constituents.

Not so:

    Python 2.4.4 (#2, Jan 13 2007, 17:50:26) 
    Type "copyright", "credits" or "license" for more information.

    IPython 0.7.3 -- An enhanced Interactive Python.
    ?       -> Introduction to IPython's features.
    %magic  -> Information about IPython's 'magic' % functions.
    help    -> Python's own help system.
    object? -> Details about 'object'. ?object also works, ?? prints more.

    In [1]: class foo(object):
        def __getattr__(self,attr):
            print 'getattr: ', attr; return lambda *args, **kwargs: 'nada'

       ...:    ...:    ...: 
    In [2]: f=foo()

    In [3]: f.bar()
    getattr:  bar()
    getattr:  bar
    Out[3]: 'nada'

    In [4]: f.this('is bad')
    getattr:  this('is
    getattr:  this
    Out[4]: 'nada'

This looks like a bug to me -- is this intended? 

IMHO ipython shouldn't do unpredictable getattr calls when I try to evaluate a
syntactically valid python line, and especially not getattr calls that use
non-valid "attributes" such as "('is" -- or is there any reason this is needed
for ipython syntactic extensions or somesuch?

It certainly causes undesirable behavior in mlabwrap (see my other post),
which I can work around easily enough (and presumably will have to, given that
enough potential users of mlabwrap are likely to have an ipython version that
does this), but I'm also curious to know whether this is a bug or a feature
(and if the later how it can be disabled).

Using pdb_settrace I find the offending bit to be Magic.py's _ofind (line 213,
which is called by iplib's _prefiter); a simple kludge to avoid looking up
things that can't possibly be attributes would be to add something like

        if re.search(r'\W', attr): continue

but unless it's intended I'd rather ipython didn't use getattr calls at all in
above examples.



More information about the IPython-user mailing list