In Python 2, is there a canonical way to document that a method may be more than one type?
Here's how I've done it:
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)
where I used the notation (int or str)
to indicate that the argument may be either an integer or a string.
How do Python 2 programs usually document that a method argument can be of more than one type? Is there a standard or best practice?
Due to forces beyond my control, I cannot use Python 3 and therefore cannot use annotations
The way you did it is fine. There is no exact format that is necessary; you just need to make the intent clear using commas or "or". See for instance various functions from matplotlib
or pandas
which take arguments documented "str or None", "float, sequence or None", "list-like or integer", etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With