且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何知道函数返回类型和参数类型?

更新时间:2023-11-26 18:25:16

这就是动态语言的工作原理.但这并不总是一件好事,尤其是在文档很差的情况下——有人试图使用文档不完善的 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!