Print or write to a file the source code for a NumPy object. The source code is only returned for objects written in Python. Many functions and classes are defined in C and will therefore not return useful information. Parameters ---------- object : numpy object
(object, output=sys.stdout)
| 694 | |
| 695 | @set_module('numpy') |
| 696 | def source(object, output=sys.stdout): |
| 697 | """ |
| 698 | Print or write to a file the source code for a NumPy object. |
| 699 | |
| 700 | The source code is only returned for objects written in Python. Many |
| 701 | functions and classes are defined in C and will therefore not return |
| 702 | useful information. |
| 703 | |
| 704 | Parameters |
| 705 | ---------- |
| 706 | object : numpy object |
| 707 | Input object. This can be any object (function, class, module, |
| 708 | ...). |
| 709 | output : file object, optional |
| 710 | If `output` not supplied then source code is printed to screen |
| 711 | (sys.stdout). File object must be created with either write 'w' or |
| 712 | append 'a' modes. |
| 713 | |
| 714 | See Also |
| 715 | -------- |
| 716 | lookfor, info |
| 717 | |
| 718 | Examples |
| 719 | -------- |
| 720 | >>> np.source(np.interp) #doctest: +SKIP |
| 721 | In file: /usr/lib/python2.6/dist-packages/numpy/lib/function_base.py |
| 722 | def interp(x, xp, fp, left=None, right=None): |
| 723 | \"\"\".... (full docstring printed)\"\"\" |
| 724 | if isinstance(x, (float, int, number)): |
| 725 | return compiled_interp([x], xp, fp, left, right).item() |
| 726 | else: |
| 727 | return compiled_interp(x, xp, fp, left, right) |
| 728 | |
| 729 | The source code is only returned for objects written in Python. |
| 730 | |
| 731 | >>> np.source(np.array) #doctest: +SKIP |
| 732 | Not available for this object. |
| 733 | |
| 734 | """ |
| 735 | # Local import to speed up numpy's import time. |
| 736 | import inspect |
| 737 | try: |
| 738 | print("In file: %s\n" % inspect.getsourcefile(object), file=output) |
| 739 | print(inspect.getsource(object), file=output) |
| 740 | except Exception: |
| 741 | print("Not available for this object.", file=output) |
| 742 | |
| 743 | |
| 744 | # Cache for lookfor: {id(module): {name: (docstring, kind, index), ...}...} |
no test coverage detected