{"id":359,"date":"2017-08-19T21:50:29","date_gmt":"2017-08-20T02:50:29","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=359"},"modified":"2017-08-19T21:54:43","modified_gmt":"2017-08-20T02:54:43","slug":"python-how-to-use-the-built-in-help-and-dir-functions","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/19\/python-how-to-use-the-built-in-help-and-dir-functions\/","title":{"rendered":"Python: How to use the built-in help( ) and dir( ) functions"},"content":{"rendered":"<p>In addition to the <code>.__doc__<\/code> function which is useful for seeing the <a href=\"http:\/\/bluegalaxy.info\/codewalk\/2017\/08\/19\/python-how-to-set-and-read-docstrings\/\">docstring<\/a> of a function or class object, Python has a couple of other useful built-in help functions called <code>help()<\/code> and <code>dir()<\/code>.<\/p>\n<p>The <code>help()<\/code> function can be used to inspect functions. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">&gt;&gt;&gt; help(docstring_test)\r\nHelp on function docstring_test in module __main__:\r\n\r\ndocstring_test()\r\n    This is the docstring in triple quotes, right below the function definition line.\r\n<\/pre>\n<p>The <code>help()<\/code> function can also be used to inspect imported modules. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import csv\r\nprint(help(csv))<\/pre>\n<p>Which gives us lots of useful information about how to use the csv module, including descriptions of its classes, methods, and functions:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">Help on module csv:\r\n\r\nNAME\r\n    csv - CSV parsing and writing.\r\n\r\nFILE\r\n    c:\\python27\\lib\\csv.py\r\n\r\nDESCRIPTION\r\n    This module provides classes that assist in the reading and writing\r\n    of Comma Separated Value (CSV) files, and implements the interface\r\n    described by PEP 305.  Although many CSV files are simple to parse,\r\n    the format is not formally defined by a stable specification and\r\n    is subtle enough that parsing lines of a CSV file with something\r\n    like line.split(\",\") is bound to fail.  The module supports three\r\n    basic APIs: reading, writing, and registration of dialects.\r\n    \r\n    \r\n    DIALECT REGISTRATION:\r\n    \r\n    Readers and writers support a dialect argument, which is a convenient\r\n    handle on a group of settings.  When the dialect argument is a string,\r\n    it identifies one of the dialects previously registered with the module.\r\n    If it is a class or instance, the attributes of the argument are used as\r\n    the settings for the reader or writer:\r\n    \r\n        class excel:\r\n            delimiter = ','\r\n            quotechar = '\"'\r\n            escapechar = None\r\n            doublequote = True\r\n            skipinitialspace = False\r\n            lineterminator = '\\r\\n'\r\n            quoting = QUOTE_MINIMAL\r\n    \r\n    SETTINGS:\r\n    \r\n        * quotechar - specifies a one-character string to use as the \r\n            quoting character.  It defaults to '\"'.\r\n        * delimiter - specifies a one-character string to use as the \r\n            field separator.  It defaults to ','.\r\n        * skipinitialspace - specifies how to interpret whitespace which\r\n            immediately follows a delimiter.  It defaults to False, which\r\n            means that whitespace immediately following a delimiter is part\r\n            of the following field.\r\n        * lineterminator -  specifies the character sequence which should \r\n            terminate rows.\r\n        * quoting - controls when quotes should be generated by the writer.\r\n            It can take on any of the following module constants:\r\n    \r\n            csv.QUOTE_MINIMAL means only when required, for example, when a\r\n                field contains either the quotechar or the delimiter\r\n            csv.QUOTE_ALL means that quotes are always placed around fields.\r\n            csv.QUOTE_NONNUMERIC means that quotes are always placed around\r\n                fields which do not parse as integers or floating point\r\n                numbers.\r\n            csv.QUOTE_NONE means that quotes are never placed around fields.\r\n        * escapechar - specifies a one-character string used to escape \r\n            the delimiter when quoting is set to QUOTE_NONE.\r\n        * doublequote - controls the handling of quotes inside fields.  When\r\n            True, two consecutive quotes are interpreted as one during read,\r\n            and when writing, each quote character embedded in the data is\r\n            written as two quotes\r\n\r\nCLASSES\r\n    Dialect\r\n        excel\r\n            excel_tab\r\n    DictReader\r\n    DictWriter\r\n    Sniffer\r\n    exceptions.Exception(exceptions.BaseException)\r\n        _csv.Error\r\n    \r\n    class Dialect\r\n     |  Describe an Excel dialect.\r\n     |  \r\n     |  This must be subclassed (see csv.excel).  Valid attributes are:\r\n     |  delimiter, quotechar, escapechar, doublequote, skipinitialspace,\r\n     |  lineterminator, quoting.\r\n     |  \r\n     |  Methods defined here:\r\n     |  \r\n     |  __init__(self)\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Data and other attributes defined here:\r\n     |  \r\n     |  delimiter = None\r\n     |  \r\n     |  doublequote = None\r\n     |  \r\n     |  escapechar = None\r\n     |  \r\n     |  lineterminator = None\r\n     |  \r\n     |  quotechar = None\r\n     |  \r\n     |  quoting = None\r\n     |  \r\n     |  skipinitialspace = None\r\n    \r\n    class DictReader\r\n     |  Methods defined here:\r\n     |  \r\n     |  __init__(self, f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)\r\n     |  \r\n     |  __iter__(self)\r\n     |  \r\n     |  next(self)\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Data descriptors defined here:\r\n     |  \r\n     |  fieldnames\r\n    \r\n    class DictWriter\r\n     |  Methods defined here:\r\n     |  \r\n     |  __init__(self, f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)\r\n     |  \r\n     |  writeheader(self)\r\n     |  \r\n     |  writerow(self, rowdict)\r\n     |  \r\n     |  writerows(self, rowdicts)\r\n    \r\n    class Error(exceptions.Exception)\r\n     |  Method resolution order:\r\n     |      Error\r\n     |      exceptions.Exception\r\n     |      exceptions.BaseException\r\n     |      __builtin__.object\r\n     |  \r\n     |  Data descriptors defined here:\r\n     |  \r\n     |  __weakref__\r\n     |      list of weak references to the object (if defined)\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Methods inherited from exceptions.Exception:\r\n     |  \r\n     |  __init__(...)\r\n     |      x.__init__(...) initializes x; see help(type(x)) for signature\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Data and other attributes inherited from exceptions.Exception:\r\n     |  \r\n     |  __new__ = &lt;built-in method __new__ of type object&gt;\r\n     |      T.__new__(S, ...) -&gt; a new object with type S, a subtype of T\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Methods inherited from exceptions.BaseException:\r\n     |  \r\n     |  __delattr__(...)\r\n     |      x.__delattr__('name') &lt;==&gt; del x.name\r\n     |  \r\n     |  __getattribute__(...)\r\n     |      x.__getattribute__('name') &lt;==&gt; x.name\r\n     |  \r\n     |  __getitem__(...)\r\n     |      x.__getitem__(y) &lt;==&gt; x[y]\r\n     |  \r\n     |  __getslice__(...)\r\n     |      x.__getslice__(i, j) &lt;==&gt; x[i:j]\r\n     |      \r\n     |      Use of negative indices is not supported.\r\n     |  \r\n     |  __reduce__(...)\r\n     |  \r\n     |  __repr__(...)\r\n     |      x.__repr__() &lt;==&gt; repr(x)\r\n     |  \r\n     |  __setattr__(...)\r\n     |      x.__setattr__('name', value) &lt;==&gt; x.name = value\r\n     |  \r\n     |  __setstate__(...)\r\n     |  \r\n     |  __str__(...)\r\n     |      x.__str__() &lt;==&gt; str(x)\r\n     |  \r\n     |  __unicode__(...)\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Data descriptors inherited from exceptions.BaseException:\r\n     |  \r\n     |  __dict__\r\n     |  \r\n     |  args\r\n     |  \r\n     |  message\r\n    \r\n    class Sniffer\r\n     |  \"Sniffs\" the format of a CSV file (i.e. delimiter, quotechar)\r\n     |  Returns a Dialect object.\r\n     |  \r\n     |  Methods defined here:\r\n     |  \r\n     |  __init__(self)\r\n     |  \r\n     |  has_header(self, sample)\r\n     |  \r\n     |  sniff(self, sample, delimiters=None)\r\n     |      Returns a dialect (or None) corresponding to the sample\r\n    \r\n    class excel(Dialect)\r\n     |  Describe the usual properties of Excel-generated CSV files.\r\n     |  \r\n     |  Data and other attributes defined here:\r\n     |  \r\n     |  delimiter = ','\r\n     |  \r\n     |  doublequote = True\r\n     |  \r\n     |  lineterminator = '\\r\\n'\r\n     |  \r\n     |  quotechar = '\"'\r\n     |  \r\n     |  quoting = 0\r\n     |  \r\n     |  skipinitialspace = False\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Methods inherited from Dialect:\r\n     |  \r\n     |  __init__(self)\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Data and other attributes inherited from Dialect:\r\n     |  \r\n     |  escapechar = None\r\n    \r\n    class excel_tab(excel)\r\n     |  Describe the usual properties of Excel-generated TAB-delimited files.\r\n     |  \r\n     |  Method resolution order:\r\n     |      excel_tab\r\n     |      excel\r\n     |      Dialect\r\n     |  \r\n     |  Data and other attributes defined here:\r\n     |  \r\n     |  delimiter = '\\t'\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Data and other attributes inherited from excel:\r\n     |  \r\n     |  doublequote = True\r\n     |  \r\n     |  lineterminator = '\\r\\n'\r\n     |  \r\n     |  quotechar = '\"'\r\n     |  \r\n     |  quoting = 0\r\n     |  \r\n     |  skipinitialspace = False\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Methods inherited from Dialect:\r\n     |  \r\n     |  __init__(self)\r\n     |  \r\n     |  ----------------------------------------------------------------------\r\n     |  Data and other attributes inherited from Dialect:\r\n     |  \r\n     |  escapechar = None\r\n\r\nFUNCTIONS\r\n    field_size_limit(...)\r\n        Sets an upper limit on parsed fields.\r\n            csv.field_size_limit([limit])\r\n        \r\n        Returns old limit. If limit is not given, no new limit is set and\r\n        the old limit is returned\r\n    \r\n    get_dialect(...)\r\n        Return the dialect instance associated with name.\r\n        dialect = csv.get_dialect(name)\r\n    \r\n    list_dialects(...)\r\n        Return a list of all know dialect names.\r\n        names = csv.list_dialects()\r\n    \r\n    reader(...)\r\n        csv_reader = reader(iterable [, dialect='excel']\r\n                                [optional keyword args])\r\n            for row in csv_reader:\r\n                process(row)\r\n        \r\n        The \"iterable\" argument can be any object that returns a line\r\n        of input for each iteration, such as a file object or a list.  The\r\n        optional \"dialect\" parameter is discussed below.  The function\r\n        also accepts optional keyword arguments which override settings\r\n        provided by the dialect.\r\n        \r\n        The returned object is an iterator.  Each iteration returns a row\r\n        of the CSV file (which can span multiple input lines):\r\n    \r\n    register_dialect(...)\r\n        Create a mapping from a string name to a dialect class.\r\n        dialect = csv.register_dialect(name, dialect)\r\n    \r\n    unregister_dialect(...)\r\n        Delete the name\/dialect mapping associated with a string name.\r\n        csv.unregister_dialect(name)\r\n    \r\n    writer(...)\r\n        csv_writer = csv.writer(fileobj [, dialect='excel']\r\n                                    [optional keyword args])\r\n            for row in sequence:\r\n                csv_writer.writerow(row)\r\n        \r\n            [or]\r\n        \r\n            csv_writer = csv.writer(fileobj [, dialect='excel']\r\n                                    [optional keyword args])\r\n            csv_writer.writerows(rows)\r\n        \r\n        The \"fileobj\" argument can be any object that supports the file API.\r\n\r\nDATA\r\n    QUOTE_ALL = 1\r\n    QUOTE_MINIMAL = 0\r\n    QUOTE_NONE = 3\r\n    QUOTE_NONNUMERIC = 2\r\n    __all__ = ['QUOTE_MINIMAL', 'QUOTE_ALL', 'QUOTE_NONNUMERIC', 'QUOTE_NO...\r\n    __version__ = '1.0'\r\n\r\nVERSION\r\n    1.0\r\n\r\n\r\nNone<\/pre>\n<p>Another useful Python built-in function is <code>dir()<\/code>. This can be used to return a list of attributes in the module such as classes and functions. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">&gt;&gt;&gt; dir(csv)\r\n['Dialect',\r\n 'DictReader',\r\n 'DictWriter',\r\n 'Error',\r\n 'QUOTE_ALL',\r\n 'QUOTE_MINIMAL',\r\n 'QUOTE_NONE',\r\n 'QUOTE_NONNUMERIC',\r\n 'Sniffer',\r\n 'StringIO',\r\n '_Dialect',\r\n '__all__',\r\n '__builtins__',\r\n '__doc__',\r\n '__file__',\r\n '__name__',\r\n '__package__',\r\n '__version__',\r\n 'excel',\r\n 'excel_tab',\r\n 'field_size_limit',\r\n 'get_dialect',\r\n 'list_dialects',\r\n 're',\r\n 'reader',\r\n 'reduce',\r\n 'register_dialect',\r\n 'unregister_dialect',\r\n 'writer']<\/pre>\n<p>Here are a couple of other things you can do with <code>dir()<\/code>:<\/p>\n<ol>\n<li>If you don&#8217;t pass an argument to <code>dir()<\/code>, you can return just the current Python interpreter scope. For example:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">&gt;&gt;&gt; dir()\r\n['__builtins__', '__doc__', '__name__', '__package__', 'csv', u'pyscripter']<\/pre>\n<\/li>\n<li>Then you can inspect any of the items in this list. For example the <code>__builtins__<\/code>:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">&gt;&gt;&gt; dir(__builtins__)\r\n['__class__',\r\n '__cmp__',\r\n '__contains__',\r\n '__delattr__',\r\n '__delitem__',\r\n '__doc__',\r\n '__eq__',\r\n '__format__',\r\n '__ge__',\r\n '__getattribute__',\r\n '__getitem__',\r\n '__gt__',\r\n '__hash__',\r\n '__init__',\r\n '__iter__',\r\n '__le__',\r\n '__len__',\r\n '__lt__',\r\n '__ne__',\r\n '__new__',\r\n '__reduce__',\r\n '__reduce_ex__',\r\n '__repr__',\r\n '__setattr__',\r\n '__setitem__',\r\n '__sizeof__',\r\n '__str__',\r\n '__subclasshook__',\r\n 'clear',\r\n 'copy',\r\n 'fromkeys',\r\n 'get',\r\n 'has_key',\r\n 'items',\r\n 'iteritems',\r\n 'iterkeys',\r\n 'itervalues',\r\n 'keys',\r\n 'pop',\r\n 'popitem',\r\n 'setdefault',\r\n 'update',\r\n 'values',\r\n 'viewitems',\r\n 'viewkeys',\r\n 'viewvalues']<\/pre>\n<\/li>\n<\/ol>\n<p>For more information see:<br \/>\n<a href=\"https:\/\/docs.python.org\/2\/library\/functions.html#help\">https:\/\/docs.python.org\/2\/library\/functions.html#help<\/a><br \/>\n<a href=\"https:\/\/docs.python.org\/2\/library\/functions.html#dir\">https:\/\/docs.python.org\/2\/library\/functions.html#dir<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In addition to the .__doc__ function which is useful for seeing the docstring of a function or class object, Python has a couple of other useful built-in help functions called help() and dir(). The help() function can be used to inspect functions. For example: &gt;&gt;&gt; help(docstring_test) Help on function docstring_test in module __main__: docstring_test() This &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/19\/python-how-to-use-the-built-in-help-and-dir-functions\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python: How to use the built-in help( ) and dir( ) functions<\/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":[27,4],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-python-language","tag-built-in-help-functions","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/359","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=359"}],"version-history":[{"count":10,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/359\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}