如何知道函数返回类型和参数类型? [英] How to know function return type and argument types?

查看:25
本文介绍了如何知道函数返回类型和参数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道 Python 的鸭子类型概念,但有时我会为函数的参数类型或函数返回值的类型而烦恼.

While I am aware of the duck-typing concept of Python, I sometimes struggle with the type of arguments of functions, or the type of the return value of the function.

现在,如果我自己编写函数,我确实知道类型.但是如果有人想使用和调用我的函数,他/她怎么知道类型?我通常将类型信息放在函数的文档字符串中(例如:"...id 参数应该是一个整数...""...该函数将返回一个 (string,[整数]) 元组.")

Now, if I wrote the function myself, I DO know the types. But what if somebody wants to use and call my functions, how is he/she expected to know the types? I usually put type information in the function's docstring (like: "...the id argument should be an integer..." and "... the function will return a (string, [integer]) tuple.")

但是在文档字符串中查找信息(并将其放在那里,作为编码员)真的是它应该完成的方式吗?

But is looking up the information in the docstring (and putting it there, as a coder) really the way it is supposed to be done?

虽然大多数答案似乎都指向是的,文档!"我觉得这对于复杂"类型来说并不总是很容易.
例如:如何在文档字符串中简洁地描述一个函数返回一个元组列表,每个元组的形式为 (node_id, node_name, uptime_minutes) 并且元素分别是字符串、字符串和整数?
docstring PEP 文档没有对此提供任何指导.
我想反驳是在那种情况下应该使用类,但我发现 python 非常灵活,因为它允许使用列表和元组传递这些东西,即没有类.

While the majority of answers seem to direct towards "yes, document!" I feel this is not always very easy for 'complex' types.
For example: how to describe concisely in a docstring that a function returns a list of tuples, with each tuple of the form (node_id, node_name, uptime_minutes) and that the elements are respectively a string, string and integer?
The docstring PEP documentation doesn't give any guidelines on that.
I guess the counterargument will be that in that case classes should be used, but I find python very flexible because it allows passing around these things using lists and tuples, i.e. without classes.

推荐答案

这就是动态语言的工作原理.但这并不总是一件好事,尤其是在文档很差的情况下——有人试图使用文档不完善的 Python 框架吗?有时你不得不重新阅读源代码.

This is how dynamic languages work. It is not always a good thing though, especially if the documentation is poor - anyone tried to use a poorly documented python framework? Sometimes you have to revert to reading the source.

以下是一些避免鸭子输入问题的策略:

Here are some strategies to avoid problems with duck typing:

  • 为您的问题域创建一种语言
  • 这将帮助您正确命名内容
  • 使用类型来表示领域语言中的概念
  • 使用领域语言词汇命名函数参数

另外,最重要的一点:

  • 尽可能将数据保存在本地!

应该只传递一些定义良好且记录在案的类型.通过查看代码,其他任何事情都应该是显而易见的:不要有从远处传来的奇怪参数类型,而您通过查看代码附近无法弄清楚...

There should only be a few well-defined and documented types being passed around. Anything else should be obvious by looking at the code: Don't have weird parameter types coming from far away that you can't figure out by looking in the vicinity of the code...

相关(也与文档字符串相关),python 中有一种技术叫做 doctests.使用它来记录您的方法将如何使用 - 同时具有良好的单元测试覆盖率!

Related, (and also related to docstrings), there is a technique in python called doctests. Use that to document how your methods are expected to be used - and have nice unit test coverage at the same time!

这篇关于如何知道函数返回类型和参数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆