Display Function Code Body?

Discussion in 'Python' started by Haibao Tang, Jan 7, 2005.

  1. Haibao Tang

    Haibao Tang Guest

    Hail Python pals! I played with the R (http://r-project.cran.org) last
    night to do some statistics and it has an interactive session too, and
    I found a feature that is quite useful.

    I found by actually typing a function name, the R gives you a code body
    output.

    > f1 = function(x){x*x}
    > f1

    function(x){x*x}

    # more examples (you can try some)

    > sd

    function (x, na.rm = FALSE)
    {
    if (is.matrix(x))
    apply(x, 2, sd, na.rm = na.rm)
    else if (is.vector(x))
    sqrt(var(x, na.rm = na.rm))
    else if (is.data.frame(x))
    sapply(x, sd, na.rm = na.rm)
    else sqrt(var(as.vector(x), na.rm = na.rm))
    }
    <environment: namespace:stats>

    # ------------------------------------------------
    While our python gives an output which is more pro but less
    informational.

    >>> def f1(x):return x*x
    >>> f1

    <function f1 at 0x00F522B0>

    What I would like to do is to write a function like disp(), when typed,
    it can give you the code infomation.

    >>> disp(f1)

    <function f1 at 0x00F522B0>
    def f1(x):
    return x*x
    <environment: interactive>

    # or any shallow function code from a file
    >>> import calendar; disp(calendar.isleap)

    <function isleap at 0x00F795B0>
    def isleap(year):
    """Return 1 for leap years, 0 for non-leap years."""
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
    <environment: C:\Python23\Lib\calendar.py>

    # surely the compiled function code cannot be displayed
    >>> disp(blabla)

    <function isleap at 0x00F79B30>
    : internal/compiled function
    <environment: C:\Python23\DLLs\_blabla.pyd>

    Can someone please point out how this can be achieved nicely. I've
    tried some text searching approach, too dirty I must say.
    Oh! Thank you ...


    Haibao Tang
    Haibao Tang, Jan 7, 2005
    #1
    1. Advertising

  2. Haibao Tang wrote:
    > What I would like to do is to write a function like disp(), when typed,
    > it can give you the code infomation.


    Check the docs entitled "Retrieving source code":

    http://docs.python.org/lib/inspect-source.html

    Depending on what you want, you may be able to use inspect.getsource:

    py> import inspect
    py> import string
    py> print inspect.getsource(string.split)
    def split(s, sep=None, maxsplit=-1):
    """split(s [,sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string s, using sep as the
    delimiter string. If maxsplit is given, splits at no more than
    maxsplit places (resulting in at most maxsplit+1 words). If sep
    is not specified or is None, any whitespace string is a separator.

    (split and splitfields are synonymous)

    """
    return s.split(sep, maxsplit)

    However, this won't work for functions you've defined interactively I
    don't think. On the other hand, if you defined them interactively, you
    can just scroll up. ;)

    Steve
    Steven Bethard, Jan 7, 2005
    #2
    1. Advertising

  3. Haibao Tang wrote:

    >
    > Hail Python pals! I played with the R (http://r-project.cran.org) last
    > night to do some statistics and it has an interactive session too, and
    > I found a feature that is quite useful.


    [...]

    > # or any shallow function code from a file
    >>>> import calendar; disp(calendar.isleap)

    > <function isleap at 0x00F795B0>
    > def isleap(year):
    > """Return 1 for leap years, 0 for non-leap years."""
    > return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
    > <environment: C:\Python23\Lib\calendar.py>
    >
    > # surely the compiled function code cannot be displayed
    >>>> disp(blabla)

    > <function isleap at 0x00F79B30>
    > : internal/compiled function
    > <environment: C:\Python23\DLLs\_blabla.pyd>
    >
    > Can someone please point out how this can be achieved nicely. I've
    > tried some text searching approach, too dirty I must say.
    > Oh! Thank you ...


    [~/tmp]> ipython
    Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
    Type "copyright", "credits" or "license" for more information.

    IPython 0.6.7_rc1 -- 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]: import calendar

    In [2]: calendar.isleap??
    Type: function
    Base Class: <type 'function'>
    String Form: <function isleap at 0x403bb6bc>
    Namespace: Interactive
    File: /usr/src/build/475206-i386/install/usr/lib/python2.3/calendar.py
    Definition: calendar.isleap(year)
    Source:
    def isleap(year):
    """Return 1 for leap years, 0 for non-leap years."""
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)


    Note that the source display won't work for interactively defined functions,
    because their source is thrown away by the bytecode compiler. There are
    discussions of a PEP for adding a __source__ attribute to functions which would
    solve this limitation, but that is still in the future.

    Cheers,

    f
    Fernando Perez, Jan 9, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sgane
    Replies:
    1
    Views:
    690
    Paul Lutus
    Oct 11, 2004
  2. Morgan Cheng
    Replies:
    65
    Views:
    1,147
  3. London Boy
    Replies:
    2
    Views:
    330
    Keith Bowes
    Jan 12, 2004
  4. Replies:
    5
    Views:
    209
    A. Sinan Unur
    Sep 8, 2005
  5. Peng Yu
    Replies:
    3
    Views:
    288
    Rainer Weikusat
    Dec 10, 2012
Loading...

Share This Page