如何记录采用多种类型的参数

Way*_*rad 6 python comments python-2.x

在Python 2中,是否有一种规范的方法来记录方法可能不止一种类型?

这是我如何做到的:

def __init__(self, work_order_number):
    """This message communicates that a job is being rerun.  Olio will
    forget everything it knows about the job.

    Args:
        work_order_number (int or str): Work order number
    """
    payload = {
        'work_order_number': str(work_order_number),
    }
    self.content = json.dumps(payload)
Run Code Online (Sandbox Code Playgroud)

我使用符号(int or str)表示参数可以是整数或字符串.

Python 2程序通常如何记录方法参数可以是多种类型?有标准或最佳做法吗?

由于我无法控制的力量,我无法使用Python 3,因此无法使用注释

Bre*_*arn 3

你这样做的方式没问题。没有必要指定确切的格式;你只需要使用逗号或“或”来明确意图。例如,请参阅来自matplotlibpandas采用记录为“str 或 None”、“float、sequence 或 None”、“list-like or integer”等参数的各种函数。