Hướng dẫn default arguments in python - đối số mặc định trong python

Hướng dẫn default arguments in python - đối số mặc định trong python

Đã đăng vào thg 6 19, 2020 1:18 SA 3 phút đọc 3 phút đọc

Nguồn: https://qiita.com/yukinoi/items/57f6150c5d805d4b25e4

Những điều cần chú ý với giá trị đối số mặc định của Python.

Gần đây tôi gặp rắc rối với việc những giá trị đối số mặc định của Python tự ý hoạt động. Nay tôi viết bài này để chia sẽ thông tin giúp sự việc này không xảy ra với các bạn. Môi trường là Python 3.7.7 và 3.8.3.

Giá trị đối số default là gì? Ví dụ như trong đoạn code sau, thì giá trị đối số default là dt=datetime.now()

from datetime import datetime


def show_second(dt=datetime.now()):
    print(dt.second)

Cái bẫy của giá trị đối số mặc định của Python Trong đoạn code ở trên, thì đã gọi hàm show_second một lần. Tôi đã chỉnh để ba giây sau lại gọi hàm show_second một lần nữa.

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23

Khi đó thì chuyện gì xảy ra? Dù đã sleep 3 giây nhưng mà khi gọi hàm show_second lần hai thì giá trị vẫn y hệt 3 giây trước. Thời gian dừng lại ư? Hay là Za Warudo? Hay là dùng Stand của Arate? ( reference bộ truyện tranh JoJo Bizzare Adventure)

Hoạt động của giá trị đối số mặc định Python

Tôi đã tìm hiểu trong tài liệu của Python thì thấy đoạn sau: Giá trị đối số mặc định thì khi thực hiện định nghĩa hàm số, sẽ đánh giá từ trái san phải.

Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

Nói cách khác, giá trị đối số mặc định được ước tính một lần tại thời điểm xác định hàm, kết quả được lưu trong bộ nhớ và nếu giá trị đối số mặc định được sử dụng cho dù hàm đó được gọi bao nhiêu lần, kết quả đánh giá tại thời điểm xác định hàm được sử dụng. Nó dường như làm việc.

Vì vậy, thật nguy hiểm khi sử dụng cái gì đó như datetime.now () mang lại kết quả khác nhau mỗi lần bạn gọi và / hoặc thứ gì đó yêu cầu tính thời gian thực làm giá trị đối số mặc định.

Tương tự, bạn cần cẩn thận khi chỉ định danh sách hoặc từ điển làm giá trị đối số mặc định. (Cách sử dụng đối số mặc định trong các hàm và ghi chú Python | note.nkmk.me)

Các biện pháp chống lại các giá trị đối số mặc định trong Python

Vậy nên điều cần làm là đặt Không làm giá trị đối số mặc định và nếu không có giá trị, có vẻ tốt hơn là thay thế giá trị ban đầu muốn được đặt làm giá trị đối số mặc định.

Dưới đây là một ví dụ.

import time
from datetime import datetime

def show_second(dt=None):
    if dt is None:
        dt = datetime.now()
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 26

Trên đây là nội dung những điều cần chú ý về đối số mặc định Python.

All rights reserved

['sổ tay']

['bút chì'] 

['cục gôm']
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

Nhưng như bạn có thể thấy trong đầu ra thực tế của chương trình mỗi khi chức năng được gọi, cùng một danh sách được sử dụng, không có danh sách mới nào được thực hiện trên một cuộc gọi mới. & NBSP;

Ví dụ sử dụng từ điển

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
40
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
42
We need to keep the following points in mind while calling functions: 

  1. import time
    from datetime import datetime
    def show_second(dt=datetime.now()):
        print(dt.second)
    
    show_second()  #=> 23
    time.sleep(3)
    show_second()  #=> 23
    
    222
    import time
    from datetime import datetime
    def show_second(dt=datetime.now()):
        print(dt.second)
    
    show_second()  #=> 23
    time.sleep(3)
    show_second()  #=> 23
    
    44
    import time
    from datetime import datetime
    def show_second(dt=datetime.now()):
        print(dt.second)
    
    show_second()  #=> 23
    time.sleep(3)
    show_second()  #=> 23
    
    1
    import time
    from datetime import datetime
    def show_second(dt=datetime.now()):
        print(dt.second)
    
    show_second()  #=> 23
    time.sleep(3)
    show_second()  #=> 23
    
    46
  2. 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
  3. {‘Notebook, 4}
  4. {‘Bút chì: 1}

{‘Eraser, 1} Calling functions without keyword arguments 
 

Python3

Nhưng như bạn có thể thấy trong đầu ra thực tế của chương trình mỗi khi chức năng được gọi, cùng một danh sách được sử dụng, không có danh sách mới nào được thực hiện trên một cuộc gọi mới. & NBSP;

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
7
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
9
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

0
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

1
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

2
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

3

import time
from datetime import datetime

def show_second(dt=None):
    if dt is None:
        dt = datetime.now()
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 26
9
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
0
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

3

import time
from datetime import datetime

def show_second(dt=None):
    if dt is None:
        dt = datetime.now()
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 26
9
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
0
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
7
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
8

import time
from datetime import datetime

def show_second(dt=None):
    if dt is None:
        dt = datetime.now()
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 26
9
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
0
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
5
John Mark studies in Fifth Standard
John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
3

import time
from datetime import datetime

def show_second(dt=None):
    if dt is None:
        dt = datetime.now()
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 26
9
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
0
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
7
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

3

Output:  
 

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

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.
  
Example #2: Calling functions with keyword arguments 
 

Python3

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
0
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
2
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
3
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
5
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
6

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
7
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
9
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

0
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

1
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

2
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

3

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
0
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
8

{'notebook': 4}
{'notebook': 4, 'pencil': 1}
{'notebook': 4, 'pencil': 1, 'eraser': 1}
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
0
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
3
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
7
#list
['notebook']
['pencil']
['eraser']


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

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


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
5
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
5
#list
['notebook']
['pencil']
['eraser']


#dictionary
{'notebook': 4}
{'pencil': 1}
{'eraser': 1}
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
0
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
8

Output:   
 

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

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.
  
Example #3: Some Invalid function calls 
 

Python3

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
0
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
2
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
3
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
5
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
6

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
7
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
9
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

0
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

1
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

2
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

3

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
07

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.

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
14
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
16
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
17


 

  • Mã trên sẽ ném lỗi vì: & nbsp;
  • 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
Things will be much more clear with the example

Python3

Đị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ụ

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
22
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
23

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
19
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
21

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
22
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
25
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
26

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
28
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
33
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
28
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
37
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
28
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
29
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

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

Đầu ra

Những gì bạn đã mong đợi nếu bạn cho rằng một danh sách mới được tạo trong mỗi cuộc gọi chức năng khi chúng tôi không truyền một danh sách cho nó

['sổ tay']

['bút chì']

['cục gôm']

Nhưng như bạn có thể thấy trong đầu ra thực tế của chương trình mỗi khi chức năng được gọi, cùng một danh sách được sử dụng, không có danh sách mới nào được thực hiện trên một cuộc gọi mới. & NBSP;

Python3

Ví dụ sử dụng từ điển

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
40
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
42

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
19
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
21

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
51
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
29
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
54
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
51
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
33
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
60
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
51
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
37
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
60
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
28
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
29
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

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

Đầu ra

Những gì bạn đã mong đợi nếu bạn cho rằng một danh sách mới được tạo trong mỗi cuộc gọi chức năng khi chúng tôi không truyền một danh sách cho nó

['sổ tay']

['bút chì']

['cục gôm']

Nhưng như bạn có thể thấy trong đầu ra thực tế của chương trình mỗi khi chức năng được gọi, cùng một danh sách được sử dụng, không có danh sách mới nào được thực hiện trên một cuộc gọi mới. & NBSP;

Ví dụ sử dụng từ điển

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
40
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
42

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
222
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
44
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
46

Python3

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
69
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
70
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

3

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}

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
22
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
23

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
19
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
21

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
28
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
29
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
22
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
25
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
26

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
28
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
37
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
69
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

07
Các giá trị đối số mặc định được ước tính từ trái sang phải khi định nghĩa hàm được thực thi. Điều này có nghĩa là biểu thức đối số mặc định chỉ được ước tính một lần khi hàm được xác định và cùng một giá trị "được tính" được sử dụng cho mỗi cuộc gọi.
from: https://docs.python.org/ja/3/reference/compound_stmts.html#function-definitions

3

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
28
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
29
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

{‘Notebook, 4}

{‘Bút chì: 1}

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
40
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
42

def

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
19
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
1
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
21

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
51
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
29
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
54
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
51
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
33
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
60
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
8
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
51
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
37
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard
4
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
60
import time
from datetime import datetime
def show_second(dt=datetime.now()):
    print(dt.second)

show_second()  #=> 23
time.sleep(3)
show_second()  #=> 23
30

Đầu ra

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


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

Ở đây bạn có thể thấy rõ rằng mỗi khi một hàm được gọi và một danh sách hoặc từ điển không được truyền như một đối số cho hàm thì nó tạo ra một danh sách hoặc từ điển mới.