0

I am using iPython in command prompt, Windows 7.

I thought this would be easy to find, I searched and found directions on how to use the inspect package but it seems like the inspect package is meant to be used for functions that are created by the programmer rather than functions that are part of a package.

My main goal to to be able to use the help files from within command prompt of iPython, to be able to look up a function such as csv.reader() and figure out all the possible arguments for it AND all possible values for these arguements.

In R programming this would simply be args(csv.reader())

I have tried googling this but they all point me to the inspect package, perhaps I'm misunderstanding it's use?

For example,

If I wanted to see a list of all possible arguments and the corresponding possible values for these arguments for the csv.reader() function (from the import csv package), how would I go about doing that?

I've tried doing help(csv.reader) but this doesn't provide me a list of all possible arguments and their potential values. 'Dialect' shows up but it doesn't tell me the possible values of the dialect argument of the csv.reader function.

I can easily go to the site: https://docs.python.org/3/library/csv.html#csv-fmt-params and see that the dialect options are: delimiter, doublequote, escapechar, etc.. etc..but is there a way to see this in Python console?

I've also tried dir(csv.reader) but this isn't what I was looking for either.

Going bald trying to figure this out....

2
  • 1
    I don't think this is a programming problem so much as a general frustration with documentation. help prints and formats the docstrings which are only as helpful as they've been written to be. There is no general solution to show "all possible arguments" and "all possible values for said arguments" in Python because that's not even enumerable in many cases (e.g., variadic functions). Commented Sep 4, 2015 at 19:06
  • Nearly all the documentation in library comes directly from the DocStrings: help(csv.Dialect) would give you the details you are looking for, unfortunately as pointed out the help is only as good as the docstrings and the docstring for csv.reader doesn't clearly point you to csv.Dialect. You can then look at the specific dialects, e.g. help(csv.excel). Introspection on the arguments doesn't inform you of what the valid values would be so I can't see how this would help. Commented Sep 4, 2015 at 19:15

2 Answers 2

1

There is no way to do this generically, help(<function>) will at a minimum return you the function signature (including the argument names), Python is dynamically typed so you don't get any types and arguments by themselves don't tell you what the valid values are. This is where a good docstring comes in.

However, the csv module does have a specific function for listing the dialects:

>>> csv.list_dialects()
['excel', 'excel-tab', 'unix']
>>> help(csv.excel)
Help on class excel in module csv:

class excel(Dialect)
 |  Describe the usual properties of Excel-generated CSV files.
 ... 
Sign up to request clarification or add additional context in comments.

Comments

0

The inspect module is extremely powerful. To get a list of classes, for example in the csv module, you could go:

import inspect, csv
from pprint import pprint

module = csv
mod_string = 'csv'
module_classes = inspect.getmembers(module, inspect.isclass)
for i in range(len(module_classes)):
  myclass = module_classes[i][0]
  myclass = mod_string+'.'+myclass
  myclass = eval(myclass)
  # could construct whatever query you want about this class here...
  # you'll need to play with this line to get what you want; it will failasis
  #line = inspect.formatargspect(*inspect.getfullargspec(myclass))
  pprint(myclass)

Hope this helps get you started!

3 Comments

Can't you do most of this without the inspect module: [x for x in csv.__dict__ if isinstance(csv.__dict__[x], csv.Dialect.__class__)] returns you a list of classes that have inherited from csv.Dialect.
For module_functions I could go module_functions = inspect.getmembers(module, inspect.isfunction)' and line = "def " + func + inspect.formatargspec(*inspect.getfullargspect(myfunc))' Try it with something like the SMTP module or Thread from threading
Depends on what he actually wants. But it was clear he was unfamiliar with inspect I think. With inspect, you could actually generate a list of constructors for each of the classes. If your solution gives him what he needs, great. If he needs more, then maybe he can take a deeper look at inspect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.