'How can I get the docstring of a CSV Dialect object?

I have a program that lets the user choose a CSV dialect. I wrote a bit of code to list all registered dialects and display their docstrings, but it shows the docstring for the parent class (Dialect), not the subclass (excel, excel_tab, etc). Here's some code:

>>> from csv import list_dialects, get_dialect
>>> from inspect import getdoc
>>> for name in list_dialects():
...     print(name, repr(getdoc(get_dialect(name))))
...
excel 'CSV dialect\n\nThe Dialect type records CSV parsing and generation options.'
excel-tab 'CSV dialect\n\nThe Dialect type records CSV parsing and generation options.'
unix 'CSV dialect\n\nThe Dialect type records CSV parsing and generation options.'

Based on the source code, this is what I was hoping to get:

excel 'Describe the usual properties of Excel-generated CSV files.'
excel-tab 'Describe the usual properties of Excel-generated TAB-delimited files.'
unix 'Describe the usual properties of Unix-generated CSV files.'


Solution 1:[1]

csv.get_dialect is pretty weird. It doesn't actually return an instance of the classes you saw in the source code. In fact, it doesn't even return an instance of csv.Dialect!

For some reason, csv.get_dialect returns an instance of _csv.Dialect. Note the underscore - that's a different module and a different Dialect class that is not csv.Dialect and has no inheritance relationship with csv.Dialect.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2357112