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. Show 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ố:-
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;
Ví dụ 1: Chương trình Python để minh họa *Args cho một số lượng đối số khác nhau python3
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
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
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
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
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. Python3Ngườ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;
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
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;
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'}9Ví dụ 1: Chương trình Python để minh họa *Args cho một số lượng đối số khác nhau python3
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_______
First argument : Hello Next argument through *argv : Welcome Next argument through *argv : to Next argument through *argv : GeeksforGeeks37 Các
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
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
PythonFirst 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 PythonFirst 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 |