Bài viết này giải thích các đối số chức năng khác nhau của Python với các ví dụ rõ ràng về cách sử dụng chúng. Nhưng trước khi tìm hiểu chi tiết tất cả các đối số của hàm, trước tiên, hãy hiểu cách sử dụng đối số hoặc tham số trong hàm
Cũng thấy
- Bài tập hàm Python
- Trắc nghiệm hàm Python
Mục lục
Đối số chức năng là gì?
Khi chúng ta định nghĩa và gọi hàm Python, thuật ngữ tham số và đối số được sử dụng để truyền thông tin cho hàm
- tham số. Nó là biến được liệt kê bên trong dấu ngoặc đơn trong định nghĩa hàm
- lý lẽ. Nó là một giá trị được gửi đến hàm khi nó được gọi. Đó là dữ liệu mà hàm thực hiện một số hành động và trả về kết quả
Ví dụ
Trong ví dụ này, hàm
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
2 được xác định với ba tham số, # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
3, # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
4, # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
5 và in tổng của cả ba giá trị của các đối số được truyền trong khi gọi hàm# a, b, c are arguments of the function
def my_sum[a, b, c]:
s = a + b + c
return s
print['Total is:', my_sum[30, 40, 50]]
đầu ra
Total is: 120
Ghi chú. Hàm phải được gọi với số lượng đối số chính xác theo mặc định. Ví dụ, hàm trên cần 3 đối số, vì vậy bạn phải gọi hàm
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
6 với 3 đối số; Chức năng có cần đối số không?
Không bắt buộc phải sử dụng đối số trong định nghĩa hàm. Nhưng nếu bạn cần xử lý dữ liệu người dùng, bạn cần có các đối số trong định nghĩa hàm để chấp nhận dữ liệu đó
Ngoài ra, chúng tôi sử dụng đối số trong định nghĩa hàm khi chúng tôi cần thực hiện cùng một tác vụ nhiều lần với các dữ liệu khác nhau
Có thể gọi hàm mà không có đối số không?
Nếu hàm được xác định bằng các tham số, các đối số được truyền vào phải khớp với một trong các đối số mà hàm chấp nhận khi gọi
Các loại đối số chức năng
Có nhiều cách khác nhau để sử dụng các đối số trong một hàm. 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
7 và# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # without passing grade and school # Passing only the mandatory arguments student['Jon', 12] # Output: Student Details: Jon 12 Five ABC School
8]# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # without passing grade and school # Passing only the mandatory arguments student['Jon', 12] # Output: Student Details: Jon 12 Five ABC School
Đối số mặc định
Trong một hàm, các đối số có thể có các giá trị mặc định. Chúng tôi gán các giá trị mặc định cho đối số bằng cách sử dụng toán tử '=' [gán] tại thời điểm định nghĩa hàm. Bạn có thể định nghĩa một hàm với bất kỳ số lượng đối số mặc định nào
Giá trị mặc định của một đối số sẽ được sử dụng bên trong một hàm nếu chúng ta không truyền giá trị cho đối số đó tại thời điểm gọi hàm. Do đó, các đối số mặc định trở thành tùy chọn trong khi gọi hàm
Nó ghi đè giá trị mặc định nếu chúng ta cung cấp một giá trị cho các đối số mặc định trong khi gọi hàm
Hãy để chúng tôi hiểu điều này với một ví dụ
Ví dụ
Hãy xác định một hàm
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
9 với bốn đối số # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
0, # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
1, # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
2 và # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
3. Trong hàm này, # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
2 và # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
3 là các đối số mặc định có giá trị mặc định- Nếu bạn gọi một hàm không có đối số
3 và# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # not passing a school value [default value is used] # six is assigned to grade student['Kelly', 12, 'Six'] # passign all arguments student['Jessa', 12, 'Seven', 'XYZ School']
2, thì các giá trị mặc định của# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # not passing a school value [default value is used] # six is assigned to grade student['Kelly', 12, 'Six'] # passign all arguments student['Jessa', 12, 'Seven', 'XYZ School']
2 và# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # not passing a school value [default value is used] # six is assigned to grade student['Kelly', 12, 'Six'] # passign all arguments student['Jessa', 12, 'Seven', 'XYZ School']
3 sẽ được sử dụng# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # not passing a school value [default value is used] # six is assigned to grade student['Kelly', 12, 'Six'] # passign all arguments student['Jessa', 12, 'Seven', 'XYZ School']
- Các tham số
1 và# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # not passing a school value [default value is used] # six is assigned to grade student['Kelly', 12, 'Six'] # passign all arguments student['Jessa', 12, 'Seven', 'XYZ School']
0 không có giá trị mặc định và được yêu cầu [bắt buộc] trong khi gọi hàm# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # not passing a school value [default value is used] # six is assigned to grade student['Kelly', 12, 'Six'] # passign all arguments student['Jessa', 12, 'Seven', 'XYZ School']
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
Truyền một trong các đối số mặc định
Nếu bạn chuyển các giá trị của đối số
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
2 và # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
3 trong khi gọi một hàm, thì các giá trị đó sẽ được sử dụng thay vì giá trị mặc địnhVí dụ
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
đầu ra
Student Details: Kelly 12 Six ABC School Student Details: Jessa 12 Seven XYZ School
Đối số từ khóa
Thông thường, tại thời điểm gọi hàm, các giá trị được gán cho các đối số theo vị trí của chúng. Vì vậy, chúng ta phải chuyển các giá trị theo cùng một trình tự được xác định trong định nghĩa hàm
Ví dụ: khi chúng ta gọi
Student Details: Kelly 12 Six ABC School Student Details: Jessa 12 Seven XYZ School4, giá trị “Jon” được gán cho tên đối số và tương tự, 12 đến
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# not passing a school value [default value is used]
# six is assigned to grade
student['Kelly', 12, 'Six']
# passign all arguments
student['Jessa', 12, 'Seven', 'XYZ School']
1, v.v.Chúng ta có thể thay đổi hành vi này bằng cách sử dụng đối số từ khóa
Đối số từ khóa là những đối số trong đó giá trị được gán cho đối số theo từ khóa [tên] của chúng khi hàm được gọi. Nó được đặt trước bởi tên biến và một toán tử gán [
Student Details: Kelly 12 Six ABC School Student Details: Jessa 12 Seven XYZ School6]. Keyword Argument còn được gọi là đối số được đặt tên
Ví dụ
# function with 2 keyword arguments
def student[name, age]:
print['Student Details:', name, age]
# default function call
student['Jessa', 14]
# both keyword arguments
student[name='Jon', age=12]
# 1 positional and 1 keyword
student['Donald', age=13]
đầu ra
Student Details: Jessa 14 Student Details: Jon 12 Student Details: Donald 13
Thay đổi trình tự của các đối số từ khóa
Ngoài ra, bạn có thể thay đổi trình tự của các đối số từ khóa bằng cách sử dụng tên của chúng trong lệnh gọi hàm
Python cho phép các chức năng được gọi bằng cách sử dụng đối số từ khóa. Nhưng tất cả các đối số từ khóa phải khớp với các tham số trong định nghĩa hàm. Khi chúng ta gọi hàm theo cách này, thứ tự [vị trí] của các đối số có thể bị thay đổi
Ví dụ
# both keyword arguments by changing their order
student[age=13, name='Kelly']
# Output: Student Details: Kelly 13
Đối số vị trí
Đối số vị trí là những đối số mà các giá trị được gán cho các đối số theo vị trí của chúng khi hàm được gọi. Ví dụ: đối số vị trí đầu tiên phải là số 1 khi hàm được gọi. Đối số vị trí thứ 2 cần phải là thứ 2 khi hàm được gọi, v.v.
Theo mặc định, các hàm Python được gọi bằng các đối số vị trí
Ví dụ. Chương trình trừ 2 số bằng đối số vị trí
def add[a, b]:
print[a - b]
add[50, 10]
# Output 40
add[10, 50]
# Output -40
Ghi chú. Nếu bạn cố gắng chuyển nhiều đối số hơn, bạn sẽ gặp lỗi
def add[a, b]:
print[a - b]
add[105, 561, 4]
đầu ra
Total is: 1200
Ghi chú. Trong số đối số vị trí và vị trí của đối số phải được khớp. Nếu chúng ta thay đổi thứ tự, thì kết quả có thể thay đổi. Ngoài ra, nếu chúng tôi thay đổi số lượng đối số, chúng tôi sẽ gặp lỗi
Những điểm quan trọng cần nhớ về đối số chức năng
Điểm 1. Các đối số mặc định phải tuân theo các đối số không mặc định
Ví dụ
Total is: 1201
Điểm. Đối số mặc định phải tuân theo đối số mặc định trong định nghĩa hàm
Đối số mặc định phải tuân theo đối số mặc định. Ví dụ: Khi bạn sử dụng đối số mặc định trong một định nghĩa, tất cả các đối số ở bên phải của chúng cũng phải có giá trị mặc định. Nếu không, bạn sẽ gặp lỗi
Ví dụ
Total is: 1202
điểm 2. đối số từ khóa chỉ nên theo đối số vị trí
chúng ta có thể trộn các đối số vị trí với các đối số từ khóa trong khi gọi hàm. Tuy nhiên, đối số từ khóa phải luôn ở sau đối số không phải từ khóa [đối số vị trí]. Nếu không, bạn sẽ gặp lỗi
I. e. , tránh sử dụng đối số từ khóa trước đối số vị trí
Ví dụ
Total is: 1203
Điểm 3. Thứ tự của các đối số từ khóa không quan trọng, nhưng tất cả các đối số từ khóa được truyền phải khớp với một trong các đối số được hàm chấp nhận
Ví dụ
Total is: 1204
Điểm 4. Không có đối số sẽ nhận được một giá trị nhiều hơn một lần
Total is: 1205
Đối số có độ dài thay đổi
Trong Python đôi khi có trường hợp chúng ta cần truyền nhiều đối số cho hàm. Các loại đối số như vậy được gọi là đối số tùy ý hoặc đối số có độ dài thay đổi
Chúng tôi sử dụng các đối số có độ dài thay đổi nếu chúng tôi không biết trước số lượng đối số cần thiết cho hàm
Các loại đối số tùy ý
- đối số vị trí tùy ý [
7]# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # without passing grade and school # Passing only the mandatory arguments student['Jon', 12] # Output: Student Details: Jon 12 Five ABC School
- đối số từ khóa tùy ý [
8]# function with 2 keyword arguments grade and school def student[name, age, grade="Five", school="ABC School"]: print['Student Details:', name, age, grade, school] # without passing grade and school # Passing only the mandatory arguments student['Jon', 12] # Output: Student Details: Jon 12 Five ABC School
*args và **kwargs cho phép bạn chuyển nhiều đối số vị trí hoặc đối số từ khóa cho một hàm
Đối số vị trí tùy ý [# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
7]
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
Chúng ta có thể khai báo một đối số có độ dài thay đổi bằng ký hiệu
# function with 2 keyword arguments
def student[name, age]:
print['Student Details:', name, age]
# default function call
student['Jessa', 14]
# both keyword arguments
student[name='Jon', age=12]
# 1 positional and 1 keyword
student['Donald', age=13]
0 [dấu hoa thị]. Đặt dấu hoa thị [# function with 2 keyword arguments
def student[name, age]:
print['Student Details:', name, age]
# default function call
student['Jessa', 14]
# both keyword arguments
student[name='Jon', age=12]
# 1 positional and 1 keyword
student['Donald', age=13]
0] trước một tham số trong định nghĩa hàm để xác định một đối số vị trí tùy ýchúng ta có thể truyền nhiều đối số cho hàm. Bên trong, tất cả các giá trị này được biểu diễn dưới dạng một bộ. Hãy hiểu việc sử dụng các đối số có độ dài thay đổi bằng một ví dụ
Đây là một hàm đơn giản nhận ba đối số và trả về giá trị trung bình của chúng
Total is: 1206
Chức năng này hoạt động, nhưng nó chỉ giới hạn ở ba đối số. Điều gì sẽ xảy ra nếu bạn cần tính điểm trung bình của hơn ba môn học hoặc số lượng môn học chỉ được xác định trong thời gian chạy?
Ví dụ
Total is: 1207
đầu ra
Total is: 1208
Ghi chú. args chỉ là một cái tên. Bạn có thể chọn bất kỳ tên nào bạn thích, chẳng hạn như *chủ đề
Total is: 1209
Đối số từ khóa tùy ý [**kwargs]
Chúng tôi đã thấy cách sử dụng
# function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
7. Bây giờ hãy xem cách sử dụng đối số # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
8. **kwargs cho phép bạn truyền nhiều đối số từ khóa cho một hàm. Sử dụng # function with 2 keyword arguments grade and school
def student[name, age, grade="Five", school="ABC School"]:
print['Student Details:', name, age, grade, school]
# without passing grade and school
# Passing only the mandatory arguments
student['Jon', 12]
# Output: Student Details: Jon 12 Five ABC School
8 nếu bạn muốn xử lý các đối số được đặt tên trong một hàmSử dụng toán tử giải nén[
# function with 2 keyword arguments
def student[name, age]:
print['Student Details:', name, age]
# default function call
student['Jessa', 14]
# both keyword arguments
student[name='Jon', age=12]
# 1 positional and 1 keyword
student['Donald', age=13]
5] để xác định các đối số từ khóa có độ dài thay đổi. Các đối số từ khóa được truyền tới một kwargs được truy cập bằng cặp khóa-giá trị [giống như truy cập từ điển trong Python]