热搜: fiddler git ip 代理
历史搜索

函數定義中的神秘實體,你知道是什麼嗎?

游客2024-06-27 12:39:50

In , are named that an that a given can .

When , you may not be aware of all the use cases of your code, and may want to offer more for with the , or for users with the code. We can pass a of to a by using *args and ** in our code .

You have 3 and a set up on your or . If you don't have a set up, you can refer to the and setup for a local or for a on your for your (, , , etc.)

In , the - form of *args can be used as a to send a non- - list to . It is worth that the (*) is the here, as the word args is the idiom, it is not by the .

Info: To along with the code in this , open a shell on your local by the . Then you can copy, paste, or edit the by them after the >>> .

Let's look at a that uses two :

.py

def multiply(x, y):
    print (x * y)

Copy

In the code above, we built the with x and y as , and then when we call the , we need to use to with x and y. In this case, we will pass the 5 in for x and the 4 in for y:

.py

def multiply(x, y):
    print (x * y)
multiply(5, 4)

Copy

Now, we can run the above code:

  1. python lets_multiply.py

Copy

We'll the , that the 5 and 4 were as per the (x,y) :

Output
20

What if, later on, we that we would like to three than just two? If we try to add an to the , as shown below, we'll an error.

.py

def multiply(x, y):
    print (x * y)
multiply(5, 4, 3)

Copy

Output
TypeError: multiply() takes 2 positional arguments but 3 were given

So, if you that you may need to use more later on, you can make use of *args as your .

We can the same and code that we in the first , by x and y as , and them with *args:

.py

def multiply(*args):
    z = 1
    for num in args:
        z *= num
    print(z)
multiply(4, 5)
multiply(10, 9)
multiply(2, 3, 4)
multiply(3, 5, 10, 6)

Copy

When we run this code, we'll the for each of these calls:

Output
20 90 24 900

we used *args to send a - list to our , we were able to pass in as many as we into the calls.

With *args you can more code that a of non- your .

The form of ** is used to pass a , - to a . Again, the two (**) are the here, as the word is used, not by the .

Like *args, ** can take many you would like to to it. , ** from *args in that you will need to .

First, let's print out the ** that we pass to a . We'll a short to do this:

.py

def print_kwargs(**kwargs):
        print(kwargs)

Copy

Next, we'll call the with some into the :

.py

def print_kwargs(**kwargs):
        print(kwargs)
print_kwargs(kwargs_1="Shark", kwargs_2=4.5, kwargs_3=True)

Copy

Let's run the above and look at the :

  1. python print_kwargs.py

Copy

Output
{'kwargs_3': True, 'kwargs_2': 4.5, 'kwargs_1': 'Shark'}

on the of 3 you are using, the data type may be . In 3.6 and above, you'll the key-value pairs in order, but in , the pairs will be in a order.

What is to note is that a is and we can work with it just like we can work with other .

Let's short to show how we can make use of **. Here we'll a to greet a of names. First, we'll start with a of two names:

.py

def print_values(**kwargs):
    for key, value in kwargs.items():
        print("The value of {} is {}".format(key, value))
print_values(my_name="Sammy", your_name="Casey")

Copy

We can now run the and look at the :

  1. python print_values.py

Copy

Output
The value of your_name is Casey The value of my_name is Sammy

Again, may be , your may be with the name Casey first or with the name Sammy first.

Let's now pass to the to show that ** will many you would like to :

.py

def print_values(**kwargs):
    for key, value in kwargs.items():
        print("The value of {} is {}".format(key, value))
print_values(
            name_1="Alex",
            name_2="Gray",
            name_3="Harper",
            name_4="Phoenix",
            name_5="Remy",
            name_6="Val"
        )

Copy

When we run the at this point, we'll the , which may again be :

Output
The value of name_2 is Gray The value of name_6 is Val The value of name_4 is Phoenix The value of name_5 is Remy The value of name_3 is Harper The value of name_1 is Alex

Using ** us with to use in our . When we use ** as a , we don't need to know how many we would like to pass to a .

When a or call, need to occur in a order:

* **

In , when with along with *args and **, your would look like this:

def example(arg_1, arg_2, *args, **kwargs):
...

Copy

And, when with along with named in to *args and **, your would look like this:

def example2(arg_1, arg_2, *args, kw_1="shark", kw_2="blobfish", **kwargs):
...

Copy

It is to keep the order of in mind when so that you do not a error in your code.

We can also use *args and ** to pass into .

First, let's look at an with *args.

.py

def some_args(arg_1, arg_2, arg_3):
    print("arg_1:", arg_1)
    print("arg_2:", arg_2)
    print("arg_3:", arg_3)
args = ("Sammy", "Casey", "Alex")
some_args(*args)

Copy

In the above, there are three as arg_1, arg_, and arg_3. The will print out each of these . We then a that is set to an (in this case, a tuple), and can pass that into the with the .

When we run the with the .py , we'll the :

Output
arg_1: Sammy arg_2: Casey arg_3: Alex

We can also the above to an list data type with a name. Let's also the *args with a :

.py

def some_args(arg_1, arg_2, arg_3):
    print("arg_1:", arg_1)
    print("arg_2:", arg_2)
    print("arg_3:", arg_3)
my_list = [2, 3]
some_args(1, *my_list)

Copy

If we run the above, it will the :

Output
arg_1: 1 arg_2: 2 arg_3: 3

, the ** can be used to call a . We will set up a equal to a with 3 key-value pairs (we'll use here, but it can be you want), and pass it to a with 3 :

.py

def some_kwargs(kwarg_1, kwarg_2, kwarg_3):
    print("kwarg_1:", kwarg_1)
    print("kwarg_2:", kwarg_2)
    print("kwarg_3:", kwarg_3)
kwargs = {"kwarg_1": "Val", "kwarg_2": "Harper", "kwarg_3": "Remy"}
some_kwargs(**kwargs)

Copy

Let's run the above with the .py :

Output
kwarg_1: Val kwarg_2: Harper kwarg_3: Remy

When a , you can use *args and ** to pass .

We can use the of *args and ** a in order to pass a of to the .

that *args and ** are best used in where you that the of the list will small. The use of *args and ** is to and , but be done with care.