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:
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 : GeeksforGeeks0____11
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks2
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks4
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks5
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks7
myFun[
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks9
first == Geeks mid == for last == Geeks0
first == Geeks mid == for last == Geeks1
first == Geeks mid == for last == Geeks0
first == Geeks mid == for last == Geeks3
first == Geeks mid == for last == Geeks0
first == Geeks mid == for last == Geeks5
first == Geeks mid == for last == Geeks6
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 == Geeks8
*
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks3
first == Geeks mid == for last == Geeks4
first == Geeks mid == for last == Geeks5
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0____11
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks2
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks4
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks5
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks3
arg1: Geeks arg2: for arg3: Geeks arg1: Geeks arg2: for arg3: Geeks4
arg1: Geeks arg2: for arg3: Geeks arg1: Geeks arg2: for arg3: Geeks5
myFun[
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks9
first == Geeks mid == for last == Geeks0
first == Geeks mid == for last == Geeks1
first == Geeks mid == for last == Geeks0
first == Geeks mid == for last == Geeks3
first == Geeks mid == for last == Geeks0
first == Geeks mid == for last == Geeks5
first == Geeks mid == for last == Geeks6
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 == Geeks6
Output:
first == Geeks mid == for last == Geeks
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 == Geeks8
*
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 == Geeks6
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 : GeeksforGeeks10
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks14
first == Geeks mid == for last == Geeks5
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks19
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks20
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks24
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks25
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks26
myFun[
2 first == Geeks mid == for last == Geeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks29_______
myFun[
*
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks37
Các
myFun[
*
*
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks56
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 : GeeksforGeeks60
*
*
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 : GeeksforGeeks0
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks67
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks68
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks3
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks72
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks73
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 : GeeksforGeeks90
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks91
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0____
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks94
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks95
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks96
*
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks98
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks5
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks95______201
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks5
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks95
first == Geeks mid == for last == Geeks08
myFun[
2first == Geeks mid == for last == Geeks03
first == Geeks mid == for last == Geeks11
first == Geeks mid == for last == Geeks12
first == Geeks mid == for last == Geeks13
myFun[
2first == Geeks mid == for last == Geeks15
first == Geeks mid == for last == Geeks16
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks96
first == Geeks mid == for last == Geeks18__
first == Geeks mid == for last == Geeks20
myFun[
2first == Geeks mid == for last == Geeks15
first == Geeks mid == for last == Geeks23
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks96
first == Geeks mid == for last == Geeks25
first == Geeks mid == for last == Geeks6
first == Geeks mid == for last == Geeks27
myFun[
2first == Geeks mid == for last == Geeks15
first == Geeks mid == for last == Geeks30
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks96
first == Geeks mid == for last == Geeks32
first == Geeks mid == for last == Geeks6
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks35
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks37
Với ** kwargs
Python
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks90
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks91
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks0____
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks94
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks95
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks96
*
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks98
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks5
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks95______201
first == Geeks mid == for last == Geeks13
myFun[
2first == Geeks mid == for last == Geeks15
first == Geeks mid == for last == Geeks16
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks96
first == Geeks mid == for last == Geeks18__
Với ** kwargs
first == Geeks mid == for last == Geeks20
myFun[
2first == Geeks mid == for last == Geeks64
myFun[
2first == Geeks mid == for last == Geeks23
first == Geeks mid == for last == Geeks67
myFun[
2first == Geeks mid == for last == Geeks25
first == Geeks mid == for last == Geeks6
first == Geeks mid == for last == Geeks27
myFun[
2first == Geeks mid == for last == Geeks64
myFun[
2first == Geeks mid == for last == Geeks30
first == Geeks mid == for last == Geeks67
myFun[
2first == Geeks mid == for last == Geeks32
first == Geeks mid == for last == Geeks6
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks35
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks6
first == Geeks mid == for last == Geeks37