Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document python function parameter types?

Tags:

I know that the parameters can be any object but for the documentation it is quite important to specify what you would expect.

First is how to specify a parameter types like these below?

  • str (or use String or string?)
  • int
  • list
  • dict
  • function()
  • tuple
  • object instance of class MyClass

Second, how to specify params that can be of multiple types like a function that can handle a single parameter than can be int or str?

Please use the below example to demonstrate the syntax needed for documenting this with your proposed solution. Mind that it is desired to be able to hyperlink reference to the "Image" class from inside the documentation.

def myMethod(self, name, image):     """     Does something ...      name String: name of the image     image Image: instance of Image Class or a string indicating the filename.      Return True if operation succeeded or False.     """     return True 

Note, you are welcome to suggest the usage of any documentation tool (sphinx, oxygen, ...) as long it is able to deal with the requirements.

Update:

It seams that there is some kind of support for documenting parameter types in doxygen in. general. The code below works but adds an annoying $ to the param name (because it was initially made for php).

    @param str $arg description     @param str|int $arg description 
like image 547
sorin Avatar asked Oct 07 '11 16:10

sorin


People also ask

How do you specify parameter data type in Python?

Python is a strongly-typed dynamic language in which we don't have to specify the data type of the function return value and function argument. It relates type with values instead of names. The only way to specify data of specific types is by providing explicit datatypes while calling the functions.

How do you document a function in Python?

To document functions in Python, use docstrings (triple quotation marks). For example: def greet(name): """ Greets a person with their name.

Can you specify types in Python?

Python is a dynamically-typed language, which means that we don't have to specify data types when we create variables and functions. While this reduces the amount of code we need to write, the workload that we save is in turn added to the next developer that needs to understand and debug the existing function!


2 Answers

There is a better way. We use

def my_method(x, y):     """     my_method description      @type x: int     @param x: An integer      @type y: int|string     @param y: An integer or string      @rtype: string     @return: Returns a sentence with your variables in it     """      return "Hello World! %s, %s" % (x,y) 

That's it. In the PyCharm IDE this helps a lot. It works like a charm ;-)

like image 117
Tony Melony Avatar answered Oct 02 '22 12:10

Tony Melony


You need to add an exclamation mark at the start of the Python docstring for Doxygen to parse it correctly.

def myMethod(self, name, image):     """!     Does something ...      @param name String: name of the image     @param image Image: instance of Image Class or a string indicating the filename.      @return Return True if operation succeeded or False.     """     return True 
like image 37
docu Avatar answered Oct 02 '22 12:10

docu