Home  >  Article  >  Computer Tutorials  >  How to view parameter information of a Python function

How to view parameter information of a Python function

王林
王林forward
2024-01-15 14:45:121489browse

How to check what parameters a function has in python

There are four ways to view function parameters in Python:

1. F(arg1,arg2,…)

This is a common way to define a function, and any number of parameters can be defined. Use commas to separate parameters. When calling a function of this type, you must provide the same number of values ​​(actual arguments) in parentheses after the function name, and in the same order. In other words, in this calling method, the number of formal parameters and actual parameters must be consistent and must correspond one-to-one, that is, the first formal parameter corresponds to the first actual parameter. For example:

code show as below:

def a(x,y):print x,y

Call this function, if a(1,2), x takes 1, y takes 2, and the formal parameters correspond to the actual parameters. If a(1) or a(1,2,3), an error will be reported.

2. F(arg1,arg2=value2,…)

This method is an improved version of the first method, providing default values, for example:

code show as below:

def a(x,y=3):print x,y

When calling this function, a(1,2) will still take 1 for x and 2 for y, but if a(1), no error will be reported. At this time, x will still be 1 and y will be the default 3. In the above two methods, you can also change the parameter position. For example, a(y=4,x=3) can also be used in this form.

3. F(*arg1)

The above two methods are to pass in as many actual parameters as there are formal parameters, but sometimes you are not sure how many parameters there are. In this case, the third method is more useful. It is preceded by an *. The number of actual parameters of this function is expressed in the form of formal parameter names, which may be 0 or n. One thing to note is that no matter how many there are, they are all stored in tuples with formal parameter names as identifiers inside the function.

code show as below:

def a(*x):print x

>>> a(1,2,3)

(1, 2, 3)

>>> a(x=1,y=2,z=3)

Traceback (most recent call last):

File """, line 1, in TypeError: a() got an unexpected keyword argument 'x' 4. F(**arg1) Add two * in front of the formal parameter name to indicate that the parameters will be stored inside the function. In a dictionary whose form is named identifier, the method of calling the function needs to be in the form arg1=value1, arg2=value2. The code is as follows: def a(**x):print x >>> a(x=1,y=2,z=3) {'y': 2, 'x': 1, 'z': 3} # Stored in the dictionary >>> a(1,2,3) #This kind of call will report an error Traceback (most recent call last): File """, line 1, in TypeError: a() takes exactly 0 arguments (3 given)

How to check function parameters in python

During development, we can use related plug-ins or use the Python built-in function "help()" to view the parameter description of a certain function. Take viewing the built-in function sorted() as an example:

How to view parameter information of a Python function

Function parameters include: required parameters, default parameters, optional parameters, and keyword parameters.

1. Default parameters: placed after the required parameters, the function to calculate x square:

How to view parameter information of a Python function

In this case, the function must be rewritten every time a different power function is calculated, which is very troublesome. You can use the following code to calculate:

How to view parameter information of a Python function

The biggest advantage of default parameters is to reduce the difficulty of calling functions.

2. Variable parameters: The number of parameters passed in is variable. It can be 1, 2, or any number, or 0. Add * in front of the parameters to indicate variable parameters. Inside the function, the parameter numbers receives a tuple. When calling the function, any number of parameters can be passed in, including 0 parameters:

How to view parameter information of a Python function

You can also assemble a dict similar to variable parameters, and then convert the dict into keyword parameters and pass them in:

How to view parameter information of a Python function

What should I do if I want to view the external functions of the DLL and their parameters?

Yes, it is recommended to repost

Okay, I'll go look elsewhere

But points. . . .

Depends that come with VC~

Mention a method to check the number of parameters, but it is impossible to determine the type unless you carefully follow each instruction

PROC lpFunction = GetProcAddress(hModule,"fucntion name");_asm{jmp lpFunction; track the function from here}

The function should be (if it is stdcall written in C)

(Don’t execute it, something may go wrong, please read it slowly) push ebpmov ebp,esp

sub esp,XXX (multiple of 4)

add esp,XXX

mov esp,ebppop ebpret XXXX (also a multiple of four)

Number of parameters = XXXX/4

The above is the detailed content of How to view parameter information of a Python function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete