如何找到一个函数的所有 **args 的列表? [英] How to find a list of all **args of a function?

查看:21
本文介绍了如何找到一个函数的所有 **args 的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到一个函数的所有 **args 的列表?

How can I find a list of all **args of a function?

例如,我知道 symbols()positive=Truereal=True 等作为参数,我会喜欢查看这些可能参数的完整列表.但是,它们并未列在 Sympy 核心 doc 中页.

For example, I know that symbols() take positive=True, real=True, etc. as arguments, and I would like to see a full list of these possible arguments. However, they're not listed in the Sympy Core doc page.

而且我已经对源代码本身进行了挖掘,但我无法追踪和定位我要查找的内容.

And I've dug through the source code itself, but I can't trace and locate what I'm looking for.

推荐答案

symbols() 函数

正如其他答案所指出的那样 - 在 symbols 中使用 **args 的一种用途是传入关于正在创建的 Symbol 的假设.您可以通过的假设列表记录在 Assumptions 页面下作为 支持的谓词.

symbols() function

As other answers have noted - one use of **args in the symbols is to pass in Assumptions about the Symbol being created. The list of assumptions you can pass is documented under the Assumptions page as supported predicates.

但是,您还应该注意可以传入一些其他特殊命名参数.

However, you should also note that some other special named arguments can be passed in.

这些都记录在您链接的部分中,并且是:

These are both documented in the section that you link and are:

  1. cls=

尽管有它的名字,symbols() 可以创建类似符号的对象,如 Function 或 Wild 类的实例.为此,请将 cls 关键字参数设置为所需的类型:

Despite its name, symbols() can create symbol-like objects like instances of Function or Wild classes. To achieve this, set cls keyword argument to the desired type:

注意如果未指定,则使用默认的 Symbol 类.

N.B. If not specified, the default Symbol class is used.

seq=<真|假>

文档说:

如果单个符号需要可迭代容器,则设置seqTrue 的参数或用逗号终止符号名称

If an iterable container is needed for a single symbol, set the seq argument to True or terminate the symbol name with a comma

代码行走

您注意到您已经浏览了代码 - 所以我将向您展示这些在代码中的实现位置.如果您调用 symbols() 函数,它对其参数进行各种检查,包括 pop-ing clsseq 来自 **args 的参数 然后执行更多检查等,最后调用以实例化 Symbol 这里此处此处.这些调用 Symbol(或其通过 cls 传入的子类)的构造函数,以及 **args 中剩下的所有内容,它们都被解释为 assumptions 在构造函数. - 它们是 sanitized here 即非假设或不适用的命名参数是 在这一点上被抛出

Code walk

You note that you've looked through the code - so I'll show you where these are implemented in the code. If you call the symbols() function, it does various checks of its arguments, including pop-ing cls and seq arguments from **args it then performs more checks etc, before finally calling through to instantiate the Symbol here, here or here. These call the constructor of Symbol (or its subclass passed in via cls) with whatever is left in **args which are all interpreted as assumptions in the constructor. - they are sanitized here i.e. non-assumption or non-applicable named arguments are thrown out at this point!

这表明 Assumptions + cls + seq 形成了一组命名参数,可以在 **args 中传递给 符号()

This shows that Assumptions + cls + seq form the set of named arguments that can be passed in **args to symbols()

在我看来,symbols() 可能只是作为一个更一般问题的代表性示例.我希望以上内容已经让您确信所有可以有用地传递到 symbols() 的值都已记录在案.这可能会让您确信 SymPy 中的其他函数也是如此.

It occurs to me that symbols() may have simply served as a representative example of a more general question. I hope the above has convinced you that all the values that can be usefully passed into symbols() are documented. This may give you some confidence that the same is true for other functions within SymPy.

然而,在一般情况下,答案是很难向自己证明所有可以作为关键字参数传入的值都在any 库或函数.事实上,有时只故意记录一个子集,因为它们是库的公共 API",而实际代码可能采用其他参数,但出于某种原因,开发人员不想将它们公开 - 例如因为它们的可用性可能会发生变化,或者它们的功能未经测试.

However, in the general case, the answer is that it is rather hard to prove to yourself that all the values that can be passed in as keywordarguments are in the documentation of any library or function. Indeed, sometimes only a subset are documented deliberately as they are the "public API" for the library whereas the actual code may take other arguments, but for some reason the developer doesn't want to expose them to the public - e.g. because their availability may change, or their functionality is untested.

如果您确实传入无效参数,则您使用的库的行为可能会有所不同.一些库或函数会忽略它们,而另一些库或函数如果传入无效的关键字参数则会抛出错误.

If you do pass in invalid arguments, the behaviour of the library you are using may differ. Some libraries or functions will ignore them, while others will throw errors if you pass in invalid keyword arguments.

如果您想了解是否是这种情况(并且该库是开源的,例如 SymPy),那么您可以随时深入了解代码(如我在上面的 Code Walk 中所示).如果你这样做 - 你需要遵循执行路径,寻找 args.pop() 的出现.如果您担心 SymPy 中的其他功能,请在评论中告诉我 - 但希望这种通用方法对您有用.

If you want to find out whether that's the case (and the library is open source, like SymPy), then you can always dive through the code (as I show in the Code Walk above). If you do that - you need to follow the path of execution, looking for occurences of args.pop(). If there are other functions in SymPy that you are concerned about let me know in comments - but hopefully this general method will work for you.

我在上面假设您了解 *args**args 语法.如果这对您来说不是很清楚 - python 官方教程我处理它.

I'm assuming in the above that you understand the *args and **args syntax. If that's not totally clear for you - this section of the python official tutorial deals with it.

这篇关于如何找到一个函数的所有 **args 的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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