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 GeeksForGeeks0
GeeksForGeeks GeeksForGeeks1
GeeksForGeeks GeeksForGeeks2
GeeksForGeeks GeeksForGeeks3
GeeksForGeeks GeeksForGeeks4
GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks6_______0_______7
GeeksForGeeks GeeksForGeeks8
GeeksForGeeks GeeksForGeeks7
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 is0
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 is1
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 is2
GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks6_______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 is0
đầ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 is7
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 is9
=
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller1
]:
GeeksForGeeks GeeksForGeeks0
GeeksForGeeks GeeksForGeeks5_______20_______5
GeeksForGeeks GeeksForGeeks3
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller7
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller8
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller9_______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 smaller9_______135_______0
GeeksForGeeks GeeksForGeeks8
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller1
# 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 is7
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 is9
=
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller1
]:
GeeksForGeeks GeeksForGeeks0
GeeksForGeeks GeeksForGeeks5_______20_______5
GeeksForGeeks GeeksForGeeks3
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller7
def
9
func[a, b
0_______138_______# Here b is predefined and hence is optional.
0func[a, b
3=
20_______1# Here b is predefined and hence is optional.
1
func[a, b
7
func[a, b
8
func[a, b
0_______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 GeeksForGeeks0
GeeksForGeeks GeeksForGeeks5_______139_______0
1098
1GeeksForGeeks GeeksForGeeks8
1098
31098
4GeeksForGeeks GeeksForGeeks0
GeeksForGeeks GeeksForGeeks5_______139_______7
1098
1GeeksForGeeks GeeksForGeeks8
1098
3]:
1GeeksForGeeks GeeksForGeeks0
GeeksForGeeks GeeksForGeeks5_______140_______4
1098
1GeeksForGeeks GeeksForGeeks8
1098
3]:
8
]:
9
GeeksForGeeks GeeksForGeeks00
GeeksForGeeks GeeksForGeeks01
GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks04
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks06
GeeksForGeeks GeeksForGeeks7
GeeksForGeeks GeeksForGeeks8
GeeksForGeeks GeeksForGeeks09_______0_______8
GeeksForGeeks GeeksForGeeks11
# Here b is predefined and hence is optional.
1
GeeksForGeeks GeeksForGeeks13
GeeksForGeeks GeeksForGeeks14
GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks17
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks06
GeeksForGeeks GeeksForGeeks7
GeeksForGeeks GeeksForGeeks8
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 is5
# Here b is predefined and hence is optional.
1
GeeksForGeeks GeeksForGeeks24
GeeksForGeeks GeeksForGeeks25
GeeksForGeeks GeeksForGeeks26
GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks29
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks31
=
GeeksForGeeks GeeksForGeeks7
GeeksForGeeks GeeksForGeeks34_______138_______
GeeksForGeeks GeeksForGeeks36
GeeksForGeeks GeeksForGeeks37
=
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 GeeksForGeeks42
=
GeeksForGeeks GeeksForGeeks7
]:
GeeksForGeeks GeeksForGeeks0
GeeksForGeeks GeeksForGeeks47
GeeksForGeeks GeeksForGeeks48
GeeksForGeeks GeeksForGeeks49_______0_______5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks52
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks0
GeeksForGeeks GeeksForGeeks47
GeeksForGeeks GeeksForGeeks56
GeeksForGeeks GeeksForGeeks49_______0_______5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks60
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks0
GeeksForGeeks GeeksForGeeks47
GeeksForGeeks GeeksForGeeks64
=
=
GeeksForGeeks GeeksForGeeks67
GeeksForGeeks GeeksForGeeks49_______0_______5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks71
# Here b is predefined and hence is optional.
1
GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks04
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks77
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 is5
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks17
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks77
GeeksForGeeks GeeksForGeeks7
GeeksForGeeks GeeksForGeeks8
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 is5
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks03
GeeksForGeeks GeeksForGeeks29
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks93_______138_______10_______5
GeeksForGeeks GeeksForGeeks37_______138_______
GeeksForGeeks GeeksForGeeks98
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 is5
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks5
GeeksForGeeks GeeksForGeeks03
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 is03
# Here b is predefined and hence is optional.
1GeeksForGeeks GeeksForGeeks77
GeeksForGeeks GeeksForGeeks98
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 is5
GeeksForGeeks GeeksForGeeks34
=
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