1 回答
TA贡献1851条经验 获得超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."""
就个人而言,我会创建两种不同的函数,一种是根据姓名和年龄创建人,另一种是使用对象base_person。在内部,他们可以使用正确的调用者签名调用您提供的函数。例如:
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)
由于缺少太多信息,很难说出什么是针对您的具体问题的正确解决方案。
添加回答
举报