{"id":521,"date":"2017-09-28T14:25:02","date_gmt":"2017-09-28T19:25:02","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=521"},"modified":"2017-09-28T14:26:51","modified_gmt":"2017-09-28T19:26:51","slug":"python-how-to-print-function-details-iteratively-using-inspect-and-getmembers","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/09\/28\/python-how-to-print-function-details-iteratively-using-inspect-and-getmembers\/","title":{"rendered":"Python: How to print function details iteratively using inspect and getmembers"},"content":{"rendered":"<p>As described in a previous post, the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">help()<\/code> function is great for seeing details about any given module function, one at at time.<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"lYO4XaVz8R\"><p><a href=\"http:\/\/bluegalaxy.info\/codewalk\/2017\/08\/19\/python-how-to-use-the-built-in-help-and-dir-functions\/\">Python: How to use the built-in help( ) and dir( ) functions<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/bluegalaxy.info\/codewalk\/2017\/08\/19\/python-how-to-use-the-built-in-help-and-dir-functions\/embed\/#?secret=lYO4XaVz8R\" data-secret=\"lYO4XaVz8R\" width=\"600\" height=\"338\" title=\"&#8220;Python: How to use the built-in help( ) and dir( ) functions&#8221; &#8212; Chris Nielsen Code Walk\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">print help(imageio.mimsave)<\/pre>\n<p>However, sometimes we want to see details for all of the functions in an imported module. Here is one way to do this programmatically. For example, getting details about all of the functions in the imageio module:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from inspect import getmembers, isfunction\r\n\r\nimport imageio\r\nprint dir(imageio)\r\n\r\nfunctions_list = [o for o in getmembers(imageio) if isfunction(o[1])]\r\nfor f in functions_list:\r\n    print \"\\n-------------------------------------------\\n\"\r\n    print help(f[1])\r\n    print \"\\nDefined arguments:\\n\", f[0], f[1].__code__.co_varnames<\/pre>\n<ul>\n<li>Load tuples of all function names and object references into a list using getmembers and isfunction. (See line 6)<\/li>\n<li>Iterate through the functions list. The first element of every tuple is the function name and the second element is the object reference. We can simply use\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">help()<\/code> on every object reference.<\/li>\n<li>To generate a list of defined argument names, we can then use <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">.__code__.co_varnames<\/code>.\u00a0 (Line 10 in the code example.)<\/li>\n<\/ul>\n<p>Example of output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">-------------------------------------------\r\n\r\nHelp on function volwrite in module imageio.core.functions:\r\n\r\nvolwrite(uri, im, format=None, **kwargs)\r\n    volwrite(uri, vol, format=None, **kwargs)\r\n    \r\n    Write a volume to the specified file.\r\n    \r\n    Parameters\r\n    ----------\r\n    uri : {str, file}\r\n        The resource to write the image to, e.g. a filename or file object,\r\n        see the docs for more info.\r\n    vol : numpy.ndarray\r\n        The image data. Must be NxMxL (or NxMxLxK if each voxel is a tuple).\r\n    format : str\r\n        The format to use to read the file. By default imageio selects\r\n        the appropriate for you based on the filename and its contents.\r\n    kwargs : ...\r\n        Further keyword arguments are passed to the writer. See :func:`.help`\r\n        to see what arguments are available for a particular format.\r\n\r\nNone\r\n\r\nDefined arguments:\r\nvolwrite ('uri', 'im', 'format', 'kwargs', 'writer')<\/pre>\n<p>For more information, see:<br \/>\n<a href=\"https:\/\/docs.python.org\/2\/library\/inspect.html\">https:\/\/docs.python.org\/2\/library\/inspect.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As described in a previous post, the help() function is great for seeing details about any given module function, one at at time. Python: How to use the built-in help( ) and dir( ) functions For example: print help(imageio.mimsave) However, sometimes we want to see details for all of the functions in an imported module. &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/09\/28\/python-how-to-print-function-details-iteratively-using-inspect-and-getmembers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python: How to print function details iteratively using inspect and getmembers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[41,40,4],"class_list":["post-521","post","type-post","status-publish","format-standard","hentry","category-python-language","tag-getmembers","tag-inspect","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=521"}],"version-history":[{"count":5,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/521\/revisions"}],"predecessor-version":[{"id":526,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/521\/revisions\/526"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}