Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a list of all **args of a function?

Tags:

python

sympy

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

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.

like image 289
saintsfan342000 Avatar asked Dec 19 '22 01:12

saintsfan342000


2 Answers

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=<ClassName>

    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:

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

  2. seq=<True|False>

    The docs say:

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

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!

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


Other functions (general case)

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.

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.

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.

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.

like image 145
J Richard Snape Avatar answered Dec 26 '22 10:12

J Richard Snape


These arguments are named assumptions and can be found in the documentation: Sympy Assumptions

like image 30
rfkortekaas Avatar answered Dec 26 '22 11:12

rfkortekaas