1 回答

TA貢獻1851條經(jīng)驗 獲得超4個贊
如果你想使用numpydoc樣式,一種可能的方法是:
def create_person(name=None, age=None, base_person=None):
r"""A function to create a person.
Using this function, a person can be created using
either a combination of `name` and `age` or providing a `base_person`.
Parameters
----------
name : int [optional, default=None]
The name of the person to be created. Leave this as None to create a person
using the `base_person` keyword.
age : int [optional, default=None]
The age of the person to be created. Leave this as None to create a person
using the `base_person` keyword.
base_person : instance of Person class [optional, default=None]
If `age` and `name` are None, this keyword can be used to create a person
from an instance of the `Person` class."""
就個人而言,我會創(chuàng)建兩種不同的函數(shù),一種是根據(jù)姓名和年齡創(chuàng)建人,另一種是使用對象base_person。在內(nèi)部,他們可以使用正確的調(diào)用者簽名調(diào)用您提供的函數(shù)。例如:
def create_person_explicit(name, age):
return(create_person(name=name, age=age)
def create_person_from_object(base_person):
return(create_person(base_person=base_person)
由于缺少太多信息,很難說出什么是針對您的具體問題的正確解決方案。
添加回答
舉報