7

In an IPython session, I usually define custom functions...problem is, sometimes I want to see the actual code for these functions. So far, I have not been able to find a way to display this. Using ? and ?? simply returns "Dynamically generated function. No source code available." Is there any way to display source code for user-defined functions?

5
  • What makes the function "custom"? Can you provide some source that indicates how you're creating them? Commented Apr 18, 2012 at 17:59
  • something like: def blah(d,b): return (d >10 and b <10) (Interactively-defined would have been better, sorry) Commented Apr 18, 2012 at 18:12
  • 2
    What version of IPython do you have? In recent versions, f?? works on interactively defined functions. Commented Apr 18, 2012 at 18:20
  • 2
    You can try it in IPython 0.12 and see that it works ;-) pythonanywhere.com/try-ipython Commented Apr 18, 2012 at 18:23
  • 1
    @ThomasK that certainly did work, thanks. Time to update IPython... Commented Apr 18, 2012 at 18:30

4 Answers 4

2

If you are looking for the source for an interactively-defined function, you can find the most recent definition of it in iPython's history:

[i for (i, l) in  enumerate(_ih) if l.startswith('def foo(')][-1]

Then you can edit that line:

%edit 42

It'll open up Notepad or another text editor with the function definition in it.

Or you can save to a file:

%save foo.py 42
Sign up to request clarification or add additional context in comments.

1 Comment

This works, although in this specific case I used a %paste so all it showed was the magic paste command for that line. Good solution otherwise though
2

I am using python 2.7.4 and iPython 0.13.2.

I can see the source of a dynamically created function in iPython using ?? before the function name:

In [8]: ??sort_words
Type:       function
String Form:<function sort_words at 0x189b938>
File:       /tmp/ipython_edit_yc1TIo.py
Definition: sort_words(s)
Source:
def sort_words(s):
    x = re.findall(r'\W+(\w+)', s)
    x.sort()
    for i in x:
        print i

Maybe this is now available in newer versions of iPython? What version are you using?

1 Comment

yes this is correct; I asked this question when (I think) 0.12 was not the stable version, and this was not available in 0.11. See Thomas K's comment on the question
2

Try searching history:

In [22]: for l in _ih:
   ....:     if l.startswith('def f'):
   ....:         print l
   ....:         
   ....:         
def f():
    a= 1
    b = 2
    return a*b

though this is very basic, a better version would parse/run history and see if you can get a function back

Comments

0

Fumctions in IPython are considered dynamically generated if:

  • they are metaprogrammed - they are created by another function. in this case ther is no source code, it simply does not exist.

  • they are referenced in a different namespace to which they were created and there is no information as to what namespace they are originally in. in this case find out where they originally were and run ?? from there.

1 Comment

I'm not sure I understand your "meta-programmed" case: in IPython 1.1.0, one can see with ?? the code of functions created inside another function (def f(): def g():… return g).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.