![]() ![]() It's also useful to access functions with their corresponding name as a string key, among other usages: locals()() # foo() Locals() shows variables defined in a function. To dump all variables contained in the global or local scope simply use: pprint(globals()) Objects with a dict attribute can be dumped nicely using pprint() mixed with vars(), which returns the _dict_ attribute for a module, class, instance, etc.: from pprint import pprint Pprint might be in high-demand by developers with a PHP background who are looking for an alternative to var_dump(). Instances are included, as well as many other built-in objects whichĪre not representable as Python constants. May be the case if objects such as files, sockets, classes, or If the formatted structures include objects which are notįundamental Python types, the representation may not be loadable. Python data structures in a form which can be used as input to the ![]() The pprint module provides a capability to “pretty-print” arbitrary While pprint has been mentioned already by others I'd like to add some context. Inspect.getargspec is also frequently useful if you're dealing with wrapping or manipulating functions, as it will give the names and default values of function parameters. IOError is raised if the source code cannot be retrieved.""" The source code is returned as a single string. The argument may be a module, class, method, function, traceback, frame, """Return the text of the source code for an object. To fetch the source of a function, use " inspect.getsource" eg, applying it to itself: > print inspect.getsource(inspect.getsource) that make it very handy for this sort of exploratory programming.įor more programmatic use of introspection, the basic builtins like dir(), vars(), getattr etc will be useful, but it is well worth your time to check out the inspect module. There are also a lot of additional conveniences, like tab completion, pretty printing of results, result history etc. For instance appending a "?" to a function will give the help for the object (effectively a shortcut for "help(obj)", wheras using two ?'s (" func?") will display the sourcecode if it is available. ![]() This adds various shortcuts to obtain an objects documentation, properties and even source code. If this is for exploration to see what's going on, I'd recommend looking at IPython. ![]()
0 Comments
Leave a Reply. |