Đối số tùy chọn trong Python là gì?

Trong Python, khi chúng ta định nghĩa các hàm với các giá trị mặc định cho một số tham số nhất định, người ta nói rằng các đối số của nó được đặt dưới dạng tùy chọn cho người dùng. Người dùng có thể chuyển các giá trị của họ hoặc có thể giả vờ hàm sử dụng các giá trị mặc định của họ được chỉ định

Theo cách này, người dùng có thể gọi hàm bằng cách truyền các tham số tùy chọn đó hoặc chỉ truyền các tham số bắt buộc.  

Có hai cách chính để truyền tham số tùy chọn trong python

  • Không sử dụng đối số từ khóa
  • Bằng cách sử dụng đối số từ khóa

Vượt qua mà không sử dụng đối số từ khóa

Một số điểm chính cần lưu ý khi chuyển mà không sử dụng đối số từ khóa là

  • Thứ tự của các tham số nên được duy trì i. e. thứ tự các tham số được xác định trong hàm nên được duy trì trong khi gọi hàm
  • Các giá trị cho các tham số không tùy chọn phải được chuyển nếu không nó sẽ gây ra lỗi
  • Giá trị của các đối số mặc định có thể được thông qua hoặc bỏ qua

Dưới đây là một số mã giải thích khái niệm này

ví dụ 1

Python3




# Here b is predefined and hence is optional.

def func[a, b=1098]:

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
1
GeeksForGeeks
GeeksForGeeks
2
GeeksForGeeks
GeeksForGeeks
3
GeeksForGeeks
GeeksForGeeks
4

 

 

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
6_______0_______7
GeeksForGeeks
GeeksForGeeks
8
GeeksForGeeks
GeeksForGeeks
7
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
0

 

first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
1

first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
2

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
6_______10_______5
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
0

đầu ra

4
1099

ví dụ 2. chúng ta cũng có thể truyền chuỗi

Python3




first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
7

def

first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
9=
first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
1]:

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
5_______20_______5
GeeksForGeeks
GeeksForGeeks
3
first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
7

 

 

first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
8

first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
9_______135_______0# Here b is predefined and hence is optional.1

 

# Here b is predefined and hence is optional.2

first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
9_______135_______0
GeeksForGeeks
GeeksForGeeks
8
first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
1# Here b is predefined and hence is optional.1

đầu ra

GeeksForGeeks
GeeksForGeeks

Vượt qua với các đối số từ khóa

Khi các chức năng được định nghĩa thì các tham số được viết dưới dạng “datatype keyword-name”. Vì vậy, python cung cấp một cơ chế để gọi hàm bằng cách sử dụng tên từ khóa để truyền các giá trị. Điều này giúp lập trình viên bằng cách giúp họ không phải học trình tự hoặc thứ tự các tham số được truyền

Một số điểm quan trọng chúng ta cần nhớ như sau

  • Trong trường hợp này, chúng tôi không bắt buộc phải duy trì thứ tự chuyển các giá trị
  • Không nên có sự khác biệt giữa tên từ khóa đã thông qua và đã khai báo

Dưới đây là mã để thực hiện nó

Python3




first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
7

def

first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
9=
first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
1]:

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
5_______20_______5
GeeksForGeeks
GeeksForGeeks
3
first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller
7

 

 

def9

func[a, b0_______138_______# Here b is predefined and hence is optional.0func[a, b3=20_______1# Here b is predefined and hence is optional.1

 

func[a, b7

func[a, b8

func[a, b0_______138_______135_______0# Here b is predefined and hence is optional.1

đầu ra

Như chúng ta có thể thấy rằng chúng ta không yêu cầu duy trì bất kỳ thứ tự nào trong ví dụ trên. Ngoài ra, chúng ta có thể thấy rằng khi chúng ta cố gắng chỉ chuyển các tham số tùy chọn thì nó sẽ phát sinh lỗi. Điều này xảy ra vì có thể bỏ qua các tham số tùy chọn vì chúng có giá trị mặc định đi kèm, nhưng chúng ta không thể bỏ qua các tham số bắt buộc [string1 trong trường hợp trên. ] Do đó, nó hiển thị lỗi với cờ. "thiếu 1 đối số bắt buộc"

Ví dụ này sẽ cung cấp một ý tưởng sâu sắc hơn về chủ đề trên

Python3




def =4==6]:

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
5_______139_______010981
GeeksForGeeks
GeeksForGeeks
81098310984

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
5_______139_______710981
GeeksForGeeks
GeeksForGeeks
810983]:1

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
5_______140_______410981
GeeksForGeeks
GeeksForGeeks
810983]:8

 

 

]:9

GeeksForGeeks
GeeksForGeeks
00

GeeksForGeeks
GeeksForGeeks
01

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
04# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
06
GeeksForGeeks
GeeksForGeeks
7
GeeksForGeeks
GeeksForGeeks
8
GeeksForGeeks
GeeksForGeeks
09_______0_______8
GeeksForGeeks
GeeksForGeeks
11# Here b is predefined and hence is optional.1

 

GeeksForGeeks
GeeksForGeeks
13

GeeksForGeeks
GeeksForGeeks
14

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
17# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
06
GeeksForGeeks
GeeksForGeeks
7
GeeksForGeeks
GeeksForGeeks
8
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
5# Here b is predefined and hence is optional.1

 

GeeksForGeeks
GeeksForGeeks
24

GeeksForGeeks
GeeksForGeeks
25

GeeksForGeeks
GeeksForGeeks
26

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
29# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
31=
GeeksForGeeks
GeeksForGeeks
7
GeeksForGeeks
GeeksForGeeks
34_______138_______
GeeksForGeeks
GeeksForGeeks
36
GeeksForGeeks
GeeksForGeeks
37=138_______6# Here b is predefined and hence is optional.1

đầu ra

first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 

Vì vậy, về cơ bản, các cuộc gọi hàm python chỉ kiểm tra xem số lượng tham số chức năng cần thiết có được chuyển hay không

Dưới đây cho thấy trường hợp người dùng cố gắng chuyển đối số theo cả hai cách được thảo luận ở trên cùng với biện pháp phòng ngừa đã đưa ra

Python3




def

GeeksForGeeks
GeeksForGeeks
42=
GeeksForGeeks
GeeksForGeeks
7]:

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
47
GeeksForGeeks
GeeksForGeeks
48

GeeksForGeeks
GeeksForGeeks
49_______0_______5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
52# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
47
GeeksForGeeks
GeeksForGeeks
56

GeeksForGeeks
GeeksForGeeks
49_______0_______5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
60# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
0
GeeksForGeeks
GeeksForGeeks
47
GeeksForGeeks
GeeksForGeeks
64==
GeeksForGeeks
GeeksForGeeks
67

GeeksForGeeks
GeeksForGeeks
49_______0_______5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
71# Here b is predefined and hence is optional.1

 

 

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
04# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
77
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
5# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
17# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
77
GeeksForGeeks
GeeksForGeeks
7
GeeksForGeeks
GeeksForGeeks
8
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
5# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
03
GeeksForGeeks
GeeksForGeeks
29# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
93_______138_______10_______5
GeeksForGeeks
GeeksForGeeks
37_______138_______
GeeksForGeeks
GeeksForGeeks
98
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
5# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
5
GeeksForGeeks
GeeksForGeeks
03
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
03# Here b is predefined and hence is optional.1

GeeksForGeeks
GeeksForGeeks
77
GeeksForGeeks
GeeksForGeeks
98
first call
2 type is 
z type is 
2.0 type is 
second call
2 type is 
1 type is 
geeks type is 
third call
geeks type is 
3 type is 
2 type is 
5
GeeksForGeeks
GeeksForGeeks
34=10_______10# Here b is predefined and hence is optional.1

đầu ra

first call
first parameter is smaller
second call
second parameter is smaller
third call
first parameter is smaller
fourth call
first parameter is smaller

Vì vậy, một điều chúng ta nên nhớ rằng đối số từ khóa nên được sử dụng sau khi tất cả các đối số vị trí được thông qua. Do đó, đây là một điều quan trọng mà chúng ta phải ghi nhớ khi truyền tham số theo cả hai cách cho cùng một chức năng

Đối số tùy chọn là gì?

Đối số tùy chọn cho phép bạn bỏ qua đối số cho một số tham số . Cả hai kỹ thuật đều có thể được sử dụng với các phương thức, bộ chỉ mục, hàm tạo và đại biểu. Khi bạn sử dụng các đối số được đặt tên và tùy chọn, các đối số được đánh giá theo thứ tự xuất hiện trong danh sách đối số, không phải danh sách tham số.

Tùy chọn trong Python là gì?

Đối số tùy chọn Python là đối số có giá trị mặc định. Bạn có thể chỉ định một giá trị mặc định cho một đối số bằng toán tử gán. Không cần chỉ định giá trị cho đối số tùy chọn khi bạn gọi một hàm. Điều này là do giá trị mặc định sẽ được sử dụng nếu không được chỉ định

* arg trong Python là gì?

*args cho phép chúng tôi chuyển một số lượng biến đối số không phải từ khóa cho hàm Python . Trong hàm, chúng ta nên sử dụng dấu hoa thị [ * ] trước tên tham số để truyền số lượng đối số thay đổi.

4 loại đối số trong Python là gì?

Trong Python, chúng ta có 4 loại đối số hàm sau. .
Đối số mặc định
Đối số từ khóa [đối số được đặt tên]
đối số vị trí
Đối số tùy ý [đối số có độ dài thay đổi *args và **kwargs ]

Chủ Đề