Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify input and output data types in python comments?

I have seen several standards for writing comments about the kind of data a function expects and returns in Python. Is there a consensus on which one is best-practice?

Is the new functionality in http://www.python.org/dev/peps/pep-3107/ something I should start using for this?

like image 622
Joakim Lundborg Avatar asked Jan 28 '09 10:01

Joakim Lundborg


People also ask

How do you specify input data type in Python?

There are two functions that can be used to read data or input from the user in python: raw_input() and input(). The results can be stored into a variable. raw_input() – It reads the input or command and returns a string. input() – Reads the input and returns a python type like list, tuple, int, etc.

How do you define input and output in Python?

The syntax for input is input(prompt_message); the python will automatically identify whether the user entered a string, number, or list; if the input entered from the user is not correct, then python will throw a syntax error. And the syntax for the output in python is print(), normally used to print the output.

How do you comment out code in Python?

Comment Syntax Comments in Python begin with a hash mark ( # ) and whitespace character and continue to the end of the line.

What are the types of comments in Python?

What Are the Different Types of Comments in Python? There are three types of comments: single-line, multi-line, and docstring comments.


1 Answers

Function annotations are not for a specific use, they can be used for anything.

Tools can be written to extract information from the annotations and do anything you want, including checking types or generating documentation. But python itself does not do anything with the information. You could use to a completely different purpose, i.e. to provide a function that will be called on the parameter or to declare a string of possible return values.

Annotations can be any object:

def somefunc(param1: "string annotation", 
             param2: 151631,  
             param3: any_object): -> "some information here":

and you can retrieve the objects using:

print (somefunc.func_annotations)
{'param1': "string annotation", 
 'param2': 151631,  
 'param3': <object any_object>,
 'return': "some information here"}

Use case suggestions provided by the PEP:

  • Providing typing information
    • Type checking
    • Let IDEs show what types a function expects and returns
    • Function overloading / generic functions
    • Foreign-language bridges
    • Adaptation
    • Predicate logic functions
    • Database query mapping
    • RPC parameter marshaling
  • Other information
    • Documentation for parameters and return values

Since function annotation syntax is too new, it is really not used for any production tools.

I suggest using other methods to document that. I use epydoc to generate my documentation, and it can read parameter typing information from docstrings:

def x_intercept(m, b):
    """
    Return the x intercept of the line M{y=m*x+b}.  The X{x intercept}
    of a line is the point at which it crosses the x axis (M{y=0}).

    This function can be used in conjuction with L{z_transform} to
    find an arbitrary function's zeros.

    @type  m: number
    @param m: The slope of the line.
    @type  b: number
    @param b: The y intercept of the line.  The X{y intercept} of a
              line is the point at which it crosses the y axis (M{x=0}).
    @rtype:   number
    @return:  the x intercept of the line M{y=m*x+b}.
    """
    return -b/m

This example is from epydoc's website. It can generate documentation in a variety of formats, and can generate good graphs from your classes and call profiles.

like image 88
nosklo Avatar answered Sep 28 '22 03:09

nosklo