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:
>>> help(docstring_test)
Help on function docstring_test in module __main__:
docstring_test()
This is the docstring in triple quotes, right below the function definition line.
The help() function can also be used to inspect imported modules. For example:
import csv print(help(csv))
Which gives us lots of useful information about how to use the csv module, including descriptions of its classes, methods, and functions:
Help on module csv:
NAME
csv - CSV parsing and writing.
FILE
c:\python27\lib\csv.py
DESCRIPTION
This module provides classes that assist in the reading and writing
of Comma Separated Value (CSV) files, and implements the interface
described by PEP 305. Although many CSV files are simple to parse,
the format is not formally defined by a stable specification and
is subtle enough that parsing lines of a CSV file with something
like line.split(",") is bound to fail. The module supports three
basic APIs: reading, writing, and registration of dialects.
DIALECT REGISTRATION:
Readers and writers support a dialect argument, which is a convenient
handle on a group of settings. When the dialect argument is a string,
it identifies one of the dialects previously registered with the module.
If it is a class or instance, the attributes of the argument are used as
the settings for the reader or writer:
class excel:
delimiter = ','
quotechar = '"'
escapechar = None
doublequote = True
skipinitialspace = False
lineterminator = '\r\n'
quoting = QUOTE_MINIMAL
SETTINGS:
* quotechar - specifies a one-character string to use as the
quoting character. It defaults to '"'.
* delimiter - specifies a one-character string to use as the
field separator. It defaults to ','.
* skipinitialspace - specifies how to interpret whitespace which
immediately follows a delimiter. It defaults to False, which
means that whitespace immediately following a delimiter is part
of the following field.
* lineterminator - specifies the character sequence which should
terminate rows.
* quoting - controls when quotes should be generated by the writer.
It can take on any of the following module constants:
csv.QUOTE_MINIMAL means only when required, for example, when a
field contains either the quotechar or the delimiter
csv.QUOTE_ALL means that quotes are always placed around fields.
csv.QUOTE_NONNUMERIC means that quotes are always placed around
fields which do not parse as integers or floating point
numbers.
csv.QUOTE_NONE means that quotes are never placed around fields.
* escapechar - specifies a one-character string used to escape
the delimiter when quoting is set to QUOTE_NONE.
* doublequote - controls the handling of quotes inside fields. When
True, two consecutive quotes are interpreted as one during read,
and when writing, each quote character embedded in the data is
written as two quotes
CLASSES
Dialect
excel
excel_tab
DictReader
DictWriter
Sniffer
exceptions.Exception(exceptions.BaseException)
_csv.Error
class Dialect
| Describe an Excel dialect.
|
| This must be subclassed (see csv.excel). Valid attributes are:
| delimiter, quotechar, escapechar, doublequote, skipinitialspace,
| lineterminator, quoting.
|
| Methods defined here:
|
| __init__(self)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| delimiter = None
|
| doublequote = None
|
| escapechar = None
|
| lineterminator = None
|
| quotechar = None
|
| quoting = None
|
| skipinitialspace = None
class DictReader
| Methods defined here:
|
| __init__(self, f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
|
| __iter__(self)
|
| next(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| fieldnames
class DictWriter
| Methods defined here:
|
| __init__(self, f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
|
| writeheader(self)
|
| writerow(self, rowdict)
|
| writerows(self, rowdicts)
class Error(exceptions.Exception)
| Method resolution order:
| Error
| exceptions.Exception
| exceptions.BaseException
| __builtin__.object
|
| Data descriptors defined here:
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Methods inherited from exceptions.Exception:
|
| __init__(...)
| x.__init__(...) initializes x; see help(type(x)) for signature
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from exceptions.Exception:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
| ----------------------------------------------------------------------
| Methods inherited from exceptions.BaseException:
|
| __delattr__(...)
| x.__delattr__('name') <==> del x.name
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __getslice__(...)
| x.__getslice__(i, j) <==> x[i:j]
|
| Use of negative indices is not supported.
|
| __reduce__(...)
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __setattr__(...)
| x.__setattr__('name', value) <==> x.name = value
|
| __setstate__(...)
|
| __str__(...)
| x.__str__() <==> str(x)
|
| __unicode__(...)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from exceptions.BaseException:
|
| __dict__
|
| args
|
| message
class Sniffer
| "Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
| Returns a Dialect object.
|
| Methods defined here:
|
| __init__(self)
|
| has_header(self, sample)
|
| sniff(self, sample, delimiters=None)
| Returns a dialect (or None) corresponding to the sample
class excel(Dialect)
| Describe the usual properties of Excel-generated CSV files.
|
| Data and other attributes defined here:
|
| delimiter = ','
|
| doublequote = True
|
| lineterminator = '\r\n'
|
| quotechar = '"'
|
| quoting = 0
|
| skipinitialspace = False
|
| ----------------------------------------------------------------------
| Methods inherited from Dialect:
|
| __init__(self)
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from Dialect:
|
| escapechar = None
class excel_tab(excel)
| Describe the usual properties of Excel-generated TAB-delimited files.
|
| Method resolution order:
| excel_tab
| excel
| Dialect
|
| Data and other attributes defined here:
|
| delimiter = '\t'
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from excel:
|
| doublequote = True
|
| lineterminator = '\r\n'
|
| quotechar = '"'
|
| quoting = 0
|
| skipinitialspace = False
|
| ----------------------------------------------------------------------
| Methods inherited from Dialect:
|
| __init__(self)
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from Dialect:
|
| escapechar = None
FUNCTIONS
field_size_limit(...)
Sets an upper limit on parsed fields.
csv.field_size_limit([limit])
Returns old limit. If limit is not given, no new limit is set and
the old limit is returned
get_dialect(...)
Return the dialect instance associated with name.
dialect = csv.get_dialect(name)
list_dialects(...)
Return a list of all know dialect names.
names = csv.list_dialects()
reader(...)
csv_reader = reader(iterable [, dialect='excel']
[optional keyword args])
for row in csv_reader:
process(row)
The "iterable" argument can be any object that returns a line
of input for each iteration, such as a file object or a list. The
optional "dialect" parameter is discussed below. The function
also accepts optional keyword arguments which override settings
provided by the dialect.
The returned object is an iterator. Each iteration returns a row
of the CSV file (which can span multiple input lines):
register_dialect(...)
Create a mapping from a string name to a dialect class.
dialect = csv.register_dialect(name, dialect)
unregister_dialect(...)
Delete the name/dialect mapping associated with a string name.
csv.unregister_dialect(name)
writer(...)
csv_writer = csv.writer(fileobj [, dialect='excel']
[optional keyword args])
for row in sequence:
csv_writer.writerow(row)
[or]
csv_writer = csv.writer(fileobj [, dialect='excel']
[optional keyword args])
csv_writer.writerows(rows)
The "fileobj" argument can be any object that supports the file API.
DATA
QUOTE_ALL = 1
QUOTE_MINIMAL = 0
QUOTE_NONE = 3
QUOTE_NONNUMERIC = 2
__all__ = ['QUOTE_MINIMAL', 'QUOTE_ALL', 'QUOTE_NONNUMERIC', 'QUOTE_NO...
__version__ = '1.0'
VERSION
1.0
None
Another useful Python built-in function is dir(). This can be used to return a list of attributes in the module such as classes and functions. For example:
>>> dir(csv) ['Dialect', 'DictReader', 'DictWriter', 'Error', 'QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONE', 'QUOTE_NONNUMERIC', 'Sniffer', 'StringIO', '_Dialect', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', 'excel', 'excel_tab', 'field_size_limit', 'get_dialect', 'list_dialects', 're', 'reader', 'reduce', 'register_dialect', 'unregister_dialect', 'writer']
Here are a couple of other things you can do with dir():
- If you don’t pass an argument to
dir(), you can return just the current Python interpreter scope. For example:>>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'csv', u'pyscripter']
- Then you can inspect any of the items in this list. For example the
__builtins__:>>> dir(__builtins__) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
For more information see:
https://docs.python.org/2/library/functions.html#help
https://docs.python.org/2/library/functions.html#dir