python函数支持哪些参数类型,如何记录python函数参数类型?

I know that the parameters can be any object but for the documentation it is quite important to specify what you would expect.

First is how to specify a parameter types like these below?

str (or use String or string?)

int

list

dict

function()

tuple

object instance of class MyClass

Second, how to specify params that can be of multiple types like a function that can handle a single parameter than can be int or str?

Please use the below example to demonstrate the syntax needed for documenting this with your proposed solution. Mind that it is desired to be able to hyperlink reference to the "Image" class from inside the documentation.

def myMethod(self, name, image):

"""

Does something ...

name String: name of the image

image Image: instance of Image Class or a string indicating the filename.

Return True if operation succeeded or False.

"""

return True

Note, you are welcome to suggest the usage of any documentation tool (sphinx, oxygen, ...) as long it is able to deal with the requirements.

Update:

It seams that there is some kind of support for documenting parameter types in doxygen in. general. The code below works but adds an annoying $ to the param name (because it was initially made for php).

@param str $arg description

@param str|int $arg description

解决方案

There is a better way. We use

def my_method(x, y):

"""

my_method description

@type x: int

@param x: An integer

@type y: int|string

@param y: An integer or string

@rtype: string

@return: Returns a sentence with your variables in it

"""

return "Hello World! %s, %s" % (x,y)

That's it. In the PyCharm IDE this helps a lot. It works like a charm ;-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值