Hướng dẫn default argument in python example - đối số mặc định trong ví dụ python

Python cho phép các đối số chức năng có các giá trị mặc định. Nếu hàm được gọi mà không có đối số, đối số sẽ nhận được giá trị mặc định của nó.

Đối số mặc định: & nbsp; 

Python có một cách khác nhau để biểu diễn cú pháp và giá trị mặc định cho các đối số chức năng. Các giá trị mặc định chỉ ra rằng đối số hàm sẽ lấy giá trị đó nếu không có giá trị đối số nào được truyền trong cuộc gọi hàm. Giá trị mặc định được gán bằng cách sử dụng toán tử gán (=) của mẫu từ khóa Hàm sinh viên chứa 3 phần trong đó trong đó 2 đối số được gán với các giá trị mặc định. Vì vậy, chức năng sinh viên chấp nhận một đối số bắt buộc (FirstName) và phần còn lại hai đối số là tùy chọn. & Nbsp; & nbsp;
Let’s understand this through a function student. The function student contains 3-arguments out of which 2 arguments are assigned with default values. So, the function student accepts one required argument (firstname), and rest two arguments are optional. 
 

Python3

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
8__

& nbsp; & nbsp; chúng ta cần ghi nhớ các điểm sau trong khi gọi các chức năng: & nbsp;
We need to keep the following points in mind while calling functions: 

  1. Trong trường hợp truyền các đối số từ khóa, thứ tự của các đối số là quan trọng.
  2. Chỉ nên có một giá trị cho một tham số.
  3. Tên từ khóa được truyền phải khớp với tên từ khóa thực tế.
  4. Trong trường hợp gọi một hàm có chứa các đối số không phải là Keyword, thứ tự là quan trọng.

Ví dụ #1: gọi các chức năng không có đối số từ khóa & nbsp; & nbsp; Calling functions without keyword arguments 
 

Python3

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
8
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
9

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
5
{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
9

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
5
{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
6
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
1
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
3
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
4

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
5
{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
6
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
1
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
9

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
5
{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
6
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
9

Output:  
 

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
8__

& nbsp; & nbsp; chúng ta cần ghi nhớ các điểm sau trong khi gọi các chức năng: & nbsp;
  
Example #2: Calling functions with keyword arguments 
 

Python3

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
8
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
9

=0=

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
6
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
4

=0=

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
6, standard =
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
3'Mark'0

'Mark'1=

#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
1'Mark'4=
{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
6
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
4

Output:   
 

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
8__
  
Example #3: Some Invalid function calls 
 

Python3

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
8
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
9

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
03

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
6
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
8__

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
10=
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
12
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
13

& nbsp; & nbsp; chúng ta cần ghi nhớ các điểm sau trong khi gọi các chức năng: & nbsp;
 

  • Trong trường hợp truyền các đối số từ khóa, thứ tự của các đối số là quan trọng.
  • Chỉ nên có một giá trị cho một tham số.
  • Tên từ khóa được truyền phải khớp với tên từ khóa thực tế.

Trong trường hợp gọi một hàm có chứa các đối số không phải là Keyword, thứ tự là quan trọng.

Ví dụ #1: gọi các chức năng không có đối số từ khóa & nbsp; & nbsp;

John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard

Things will be much more clear with the example

Python3

Trong cuộc gọi đầu tiên, chỉ có một đối số bắt buộc và các đối số còn lại sử dụng các giá trị mặc định. Trong cuộc gọi thứ hai, giá trị LastName và tiêu chuẩn đối số được thay thế từ giá trị mặc định sang giá trị chuyển mới. Chúng ta có thể thấy thứ tự của các đối số rất quan trọng từ các cuộc gọi thứ 2, thứ 3 và thứ 4 của hàm.

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
19

Trong cuộc gọi đầu tiên, chỉ có một đối số từ khóa bắt buộc. Trong cuộc gọi thứ hai, một là một đối số bắt buộc và một là tùy chọn (tiêu chuẩn), có giá trị được thay thế từ mặc định sang giá trị chuyển mới. Trong cuộc gọi thứ ba, chúng ta có thể thấy thứ tự đó trong đối số từ khóa không quan trọng.

Các

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
24
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
29
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
24
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
33
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

Mã trên sẽ ném lỗi vì: & nbsp;

['notebook']
['notebook', 'pencil']
['notebook', 'pencil', 'eraser']

Trong cuộc gọi đầu tiên, giá trị không được truyền cho tham số FirstName là tham số cần thiết.

Trong cuộc gọi thứ hai, có một đối số không thay đổi sau một đối số từ khóa.

Trong cuộc gọi thứ ba, đối số từ khóa truyền không được khớp với các đối số từ khóa thực tế.

Sử dụng các đối tượng có thể thay đổi làm giá trị đối số mặc định trong Python

Điều này phải được thực hiện rất cẩn thận. Lý do là các giá trị mặc định của các đối số chỉ được đánh giá một lần khi điều khiển đạt đến hàm

Định nghĩa cho lần đầu tiên. Sau đó, các giá trị tương tự (hoặc các đối tượng có thể thay đổi) được tham chiếu trong các cuộc gọi hàm tiếp theo. & Nbsp; mọi thứ sẽ rõ ràng hơn nhiều với ví dụ

Python3

def

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
15=
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
17

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
21
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
22

Trong cuộc gọi đầu tiên, chỉ có một đối số từ khóa bắt buộc. Trong cuộc gọi thứ hai, một là một đối số bắt buộc và một là tùy chọn (tiêu chuẩn), có giá trị được thay thế từ mặc định sang giá trị chuyển mới. Trong cuộc gọi thứ ba, chúng ta có thể thấy thứ tự đó trong đối số từ khóa không quan trọng.

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
47
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
25
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
50
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
47
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
29
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
56
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
47
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
33
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
56
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

Đầu ra

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}

Những gì bạn đã mong đợi nếu bạn cho rằng một từ điển mới được tạo trong mỗi cuộc gọi chức năng

{‘Notebook, 4}

{‘Bút chì: 1}

{‘Eraser, 1}

Nhưng bạn có thể thấy rõ đầu ra thực tế của chương trình là khác nhau và nó cho thấy việc sử dụng cùng một từ điển trong mỗi cuộc gọi tiếp theo.

Điểm quan trọng ở đây là chúng ta nên tránh các kịch bản như vậy.

Thực hành tốt nhất

Gán giá trị mặc định là không có và sau đó kiểm tra chức năng nếu danh sách dự kiến ​​hoặc đối số từ điển không có hoặc không.

Nếu không ai chỉ định nó với danh sách hoặc từ điển tùy thuộc vào yêu cầu của bạn.

Python3

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
65
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
66
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
9

def

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
69=
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
71
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
2

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
74
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
75=__

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
80
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
75=
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
83

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
19

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
21
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
22

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
24
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
25
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
24
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
29
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
24
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
33
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
65
['notebook']
['notebook', 'pencil']
['notebook', 'pencil', 'eraser']
03
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
9

def

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
36=
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
71
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
2

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
74
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
75=__

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
80
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
75=
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
83

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
21
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
22

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
18
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
21
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
22

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
47
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
25
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
50
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
47
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
29
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
56
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
47
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
33
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
0
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
56
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26

Đầu ra

#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}

John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
24
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
29
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
26


Đối số mặc định là gì giải thích với ví dụ là gì?

Các đối số mặc định được ghi đè khi hàm gọi cung cấp các giá trị cho chúng. Ví dụ: gọi tổng hàm (10, 15, 25, 30) ghi đè lên các giá trị của Z và W đến 25 và 30 tương ứng.overwritten when the calling function provides values for them. For example, calling the function sum(10, 15, 25, 30) overwrites the values of z and w to 25 and 30 respectively.

Đối số mặc định trong Python là gì?

Python có một cách khác nhau để biểu diễn cú pháp và giá trị mặc định cho các đối số chức năng. Các giá trị mặc định chỉ ra rằng đối số hàm sẽ lấy giá trị đó nếu không có giá trị đối số nào được truyền trong cuộc gọi hàm. Giá trị mặc định được gán bằng cách sử dụng toán tử gán (=) của mẫu từ khóa = value.Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.

Làm thế nào để các đối số mặc định hoạt động trong Python?

Các đối số mặc định của Python được đánh giá một lần khi hàm được xác định, không phải mỗi lần chức năng được gọi (giống như trong nói, Ruby).Điều này có nghĩa là nếu bạn sử dụng một đối số mặc định có thể thay đổi và biến đổi nó, bạn sẽ và đã biến đổi đối tượng đó cho tất cả các cuộc gọi trong tương lai đến chức năng. (like it is in say, Ruby). This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well.

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

Do đó, chúng tôi kết luận rằng các đối số chức năng Python và ba loại đối số của nó cho các chức năng.Đây là những đối số mặc định, từ khóa và tùy ý.default, keyword, and arbitrary arguments.