440,092 Members | 1,597 Online Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

# Display Function Code Body?

 P: n/a 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)) } # ------------------------------------------------ While our python gives an output which is more pro but less informational. def f1(x):return x*x f1 What I would like to do is to write a function like disp(), when typed, it can give you the code infomation. disp(f1) def f1(x): return x*x # or any shallow function code from a file import calendar; disp(calendar.isleap) 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) # surely the compiled function code cannot be displayed disp(blabla) : internal/compiled function 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 Jul 18 '05 #1 