Hướng dẫn how do you pass variable arguments in python? - làm thế nào để bạn truyền các đối số biến trong python?

Trong bài viết này, chúng tôi sẽ đề cập đến những gì ** (Double Star/Asterisk) và * (Star/Asterisk) làm cho các tham số trong Python, & NBSP; Ở đây, chúng tôi cũng sẽ đề cập đến các ví dụ Args và Kwargs trong Python. Chúng ta có thể chuyển một số lượng khác nhau các đối số cho một hàm bằng các ký hiệu đặc biệt. & NBSP;args and kwargs examples in Python. We can pass a variable number of arguments to a function using special symbols. 

Có hai biểu tượng đặc biệt:

Hướng dẫn how do you pass variable arguments in python? - làm thế nào để bạn truyền các đối số biến trong python?

Các biểu tượng đặc biệt được sử dụng để vượt qua các đối số:-

  • *args (đối số không thay đổi)
  • ** kwargs (đối số từ khóa)

Lưu ý: Chúng tôi sử dụng các ký hiệu của Wild Roadcard, hoặc các ký hiệu * “We use the “wildcard” or “*” notation like this – *args OR **kwargs – as our function’s argument when we have doubts about the number of  arguments we should pass in a function.” 

Python *args là gì?

Cú pháp đặc biệt *Args trong các định nghĩa chức năng trong Python được sử dụng để truyền một số lượng đối số biến cho một hàm. Nó được sử dụng để vượt qua một danh sách đối số không thay đổi, không thay đổi. & NBSP;

  • Cú pháp là sử dụng ký hiệu * để lấy một số lượng đối số thay đổi; Theo quy ước, nó thường được sử dụng với từ Args.
  • Những gì *args cho phép bạn làm là đưa ra nhiều đối số hơn số lượng đối số chính thức mà bạn đã xác định trước đó. Với *Args, bất kỳ số lượng đối số bổ sung nào cũng có thể được giải quyết theo các tham số chính thức hiện tại của bạn (bao gồm cả các đối số bổ sung bằng không).
  • Ví dụ: chúng tôi muốn tạo một hàm nhân có bất kỳ số lượng đối số nào và có thể nhân tất cả chúng lại với nhau. Nó có thể được thực hiện bằng cách sử dụng *args.
  • Sử dụng *, biến mà chúng tôi liên kết với * trở thành một ý nghĩa không thể sai lầm mà bạn có thể làm những việc như lặp lại trên nó, chạy một số hàm bậc cao hơn như MAP và FILL, v.v.

Ví dụ 1:

Chương trình Python để minh họa *Args cho một số lượng đối số khác nhau

python3

def myFun(*

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0____11
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
2
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
4

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
5
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
7

myFun(

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
9
first == Geeks
mid == for
last == Geeks
0
first == Geeks
mid == for
last == Geeks
1
first == Geeks
mid == for
last == Geeks
0
first == Geeks
mid == for
last == Geeks
3
first == Geeks
mid == for
last == Geeks
0
first == Geeks
mid == for
last == Geeks
5
first == Geeks
mid == for
last == Geeks
6

Output:

Hello
Welcome
to
GeeksforGeeks

Ví dụ 2:

Chương trình Python để minh họa *args với một đối số phụ đầu tiên

Python3

def

first == Geeks
mid == for
last == Geeks
8*

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
3
first == Geeks
mid == for
last == Geeks
4
first == Geeks
mid == for
last == Geeks
5

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0____11
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
2
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
4

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
5
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
3
arg1: Geeks
arg2: for
arg3: Geeks
arg1: Geeks
arg2: for
arg3: Geeks
4
arg1: Geeks
arg2: for
arg3: Geeks
arg1: Geeks
arg2: for
arg3: Geeks
5

myFun(

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
9
first == Geeks
mid == for
last == Geeks
0
first == Geeks
mid == for
last == Geeks
1
first == Geeks
mid == for
last == Geeks
0
first == Geeks
mid == for
last == Geeks
3
first == Geeks
mid == for
last == Geeks
0
first == Geeks
mid == for
last == Geeks
5
first == Geeks
mid == for
last == Geeks
6

Output:

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks

Ví dụ 2:

Chương trình Python để minh họa *args với một đối số phụ đầu tiên

  • def
    first == Geeks
    mid == for
    last == Geeks
    8*
  • Python ** kwargs là gì

Cú pháp đặc biệt ** kwargs trong các định nghĩa chức năng trong Python được sử dụng để vượt qua danh sách đối số dài, có độ dài thay đổi. Chúng tôi sử dụng tên kwargs với ngôi sao đôi. Lý do là ngôi sao kép cho phép chúng ta vượt qua các đối số từ khóa (và bất kỳ số lượng nào trong số chúng). 

Đối số từ khóa là nơi bạn cung cấp tên cho biến khi bạn chuyển nó vào hàm.

Python3

Người ta có thể nghĩ về KWARGS là một từ điển ánh xạ từng từ khóa theo giá trị mà chúng ta vượt qua bên cạnh nó. Đó là lý do tại sao khi chúng tôi lặp lại Kwargs, dường như không có bất kỳ thứ tự nào mà chúng được in ra.

Ví dụ 1: & nbsp;

Chương trình Python để minh họa *kwargs cho một số lượng khác nhau của các đối số từ khóa. Tại đây ** KWARGS chấp nhận đối số độ dài biến từ khóa được truyền bởi cuộc gọi chức năng. Đối với First = xông Geek, đầu tiên là chìa khóa và ‘Geek, là một giá trị. Nói một cách đơn giản, những gì chúng ta gán là giá trị và người mà chúng ta gán là chìa khóa. & nbsp;

myFun(1myFun(2myFun(3myFun(4myFun(2myFun(6myFun(7myFun(2myFun(3

first == Geeks
mid == for
last == Geeks
6

Output:

first == Geeks
mid == for
last == Geeks

Hướng dẫn how do you pass variable arguments in python? - làm thế nào để bạn truyền các đối số biến trong python?

Ví dụ 2:

Chương trình Python để minh họa *args với một đối số phụ đầu tiên

Python3

def

first == Geeks
mid == for
last == Geeks
8*

Ví dụ 1: & nbsp;

Chương trình Python để minh họa *kwargs cho một số lượng khác nhau của các đối số từ khóa. Tại đây ** KWARGS chấp nhận đối số độ dài biến từ khóa được truyền bởi cuộc gọi chức năng. Đối với First = xông Geek, đầu tiên là chìa khóa và ‘Geek, là một giá trị. Nói một cách đơn giản, những gì chúng ta gán là giá trị và người mà chúng ta gán là chìa khóa. & nbsp;

myFun(argv):8argv):9myFun(2myFun(3myFun(4myFun(2myFun(6myFun(7myFun(2myFun(3

first == Geeks
mid == for
last == Geeks
6

Output:

first == Geeks
mid == for
last == Geeks

def myFun(**args: ('geeks', 'for', 'geeks') kwargs: {'first': 'Geeks', 'mid': 'for', 'last': 'Geeks'}9

Ví dụ 1:

Chương trình Python để minh họa *Args cho một số lượng đối số khác nhau

python3

def

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
10

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
14
first == Geeks
mid == for
last == Geeks
5

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
19
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
20

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
24
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
25

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
26myFun(2
first == Geeks
mid == for
last == Geeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
29_______

myFun(*

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
37

Các

myFun(**

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
56

Output:

arg1: Geeks
arg2: for
arg3: Geeks
arg1: Geeks
arg2: for
arg3: Geeks

Ví dụ 2:

Ở đây, chúng tôi đang vượt qua *args và ** kwargs như một đối số trong hàm myfun. trong đó ‘Geek,’ for, ‘Geek, được truyền qua là *args, và đầu tiên = được truyền dưới dạng ** kwargs và in trong cùng một dòng.‘geeks’, ‘for’, ‘geeks’ is passed as *args, and first=”Geeks”, mid=”for”, last=”Geeks”  is passed as **kwargs and printing in the same line.

python3

def myFun(*

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
60**
args: ('geeks', 'for', 'geeks')
kwargs: {'first': 'Geeks', 'mid': 'for', 'last': 'Geeks'}
9

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
67
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
68

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
3
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
72
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
73

Các

Output:

args: ('geeks', 'for', 'geeks')
kwargs: {'first': 'Geeks', 'mid': 'for', 'last': 'Geeks'}

Sử dụng *args và ** kwargs để đặt các giá trị của đối tượng

  • *Args nhận đối số dưới dạng một mảng
  • ** Kwargs nhận đối số như một từ điển

Python

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
90
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
91

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0____
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
94
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
95
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
96*
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
98

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
5
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
95______201

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
5
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
95
first == Geeks
mid == for
last == Geeks
08myFun(2
first == Geeks
mid == for
last == Geeks
03
first == Geeks
mid == for
last == Geeks
11
first == Geeks
mid == for
last == Geeks
12

first == Geeks
mid == for
last == Geeks
13myFun(2
first == Geeks
mid == for
last == Geeks
15
first == Geeks
mid == for
last == Geeks
16
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
96
first == Geeks
mid == for
last == Geeks
18__

first == Geeks
mid == for
last == Geeks
20myFun(2
first == Geeks
mid == for
last == Geeks
15
first == Geeks
mid == for
last == Geeks
23
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
96
first == Geeks
mid == for
last == Geeks
25
first == Geeks
mid == for
last == Geeks
6

first == Geeks
mid == for
last == Geeks
27myFun(2
first == Geeks
mid == for
last == Geeks
15
first == Geeks
mid == for
last == Geeks
30
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
96
first == Geeks
mid == for
last == Geeks
32
first == Geeks
mid == for
last == Geeks
6

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
35

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
37

Với ** kwargs

Python

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
90
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
91

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
0____
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
94
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
95
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
96*
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
98

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
5
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
95______201

first == Geeks
mid == for
last == Geeks
13myFun(2
first == Geeks
mid == for
last == Geeks
15
first == Geeks
mid == for
last == Geeks
16
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
96
first == Geeks
mid == for
last == Geeks
18__

Với ** kwargs

first == Geeks
mid == for
last == Geeks
20myFun(2
first == Geeks
mid == for
last == Geeks
64myFun(2
first == Geeks
mid == for
last == Geeks
23
first == Geeks
mid == for
last == Geeks
67myFun(2
first == Geeks
mid == for
last == Geeks
25
first == Geeks
mid == for
last == Geeks
6

first == Geeks
mid == for
last == Geeks
27myFun(2
first == Geeks
mid == for
last == Geeks
64myFun(2
first == Geeks
mid == for
last == Geeks
30
first == Geeks
mid == for
last == Geeks
67myFun(2
first == Geeks
mid == for
last == Geeks
32
first == Geeks
mid == for
last == Geeks
6

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
35

First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
6
first == Geeks
mid == for
last == Geeks
37