คำเตือนแตกต่างจากข้อผิดพลาดในโปรแกรม หากพบข้อผิดพลาด โปรแกรม Python จะยุติการทำงานทันที คำเตือนในทางกลับกันไม่เป็นอันตรายถึงชีวิต มันแสดงข้อความบางอย่าง แต่โปรแกรมยังคงดำเนินต่อไป มีการออกคำเตือนเพื่อเตือนผู้ใช้ถึงเงื่อนไขบางประการซึ่งไม่ใช่ข้อยกเว้นอย่างแน่นอน โดยทั่วไปแล้วคำเตือนจะปรากฏขึ้นหากพบการใช้งานองค์ประกอบการเขียนโปรแกรมบางอย่างที่เลิกใช้แล้ว เช่น คีย์เวิร์ด/ฟังก์ชัน/คลาส ฯลฯ
ข้อความเตือนจะแสดงโดยฟังก์ชัน warn() ที่กำหนดไว้ในโมดูล 'คำเตือน' ของไลบรารีมาตรฐานของ Python คำเตือนเป็นคลาสย่อยของ Exception ในลำดับชั้นของคลาสในตัว มีคลาสย่อยคำเตือนในตัวจำนวนหนึ่ง นอกจากนี้ยังสามารถกำหนด subclass ที่ผู้ใช้กำหนดได้
| คำเตือน | นี่คือคลาสพื้นฐานของคลาสหมวดหมู่คำเตือนทั้งหมด |
| คำเตือนผู้ใช้ | หมวดหมู่เริ่มต้นสำหรับ warn() |
| DeprecationWarning | คำเตือนเกี่ยวกับคุณลักษณะที่เลิกใช้แล้วเมื่อคำเตือนเหล่านี้มีไว้สำหรับนักพัฒนา |
| คำเตือนไวยากรณ์ | คำเตือนเกี่ยวกับคุณลักษณะวากยสัมพันธ์ที่น่าสงสัย |
| RuntimeWarning | คำเตือนเกี่ยวกับคุณสมบัติรันไทม์ที่น่าสงสัย |
| FutureWarning | คำเตือนเกี่ยวกับคุณลักษณะที่เลิกใช้แล้ว เมื่อคำเตือนเหล่านี้มีไว้สำหรับผู้ใช้ปลายทาง |
| PendingDeprecationWarning | คำเตือนเกี่ยวกับฟีเจอร์ที่จะเลิกใช้ในอนาคต |
| ImportWarning | คำเตือนที่ทริกเกอร์ระหว่างกระบวนการนำเข้าโมดูล |
| UnicodeWarning | คำเตือนเกี่ยวกับ Unicode |
| BytesWarning | คำเตือนที่เกี่ยวข้องกับไบต์และอาร์เรย์ไบต์ |
| ResourceWarning | คำเตือนเกี่ยวกับการใช้ทรัพยากร |
ตัวอย่างคำเตือน
โค้ดต่อไปนี้กำหนดคลาสด้วยเมธอดที่เลิกใช้แล้วและเมธอดที่กำหนดเวลาเลิกใช้งานในเวอร์ชันอนาคตของคลาสดังกล่าว
# warningexample.py
import warnings
class WarnExample:
def __init__(self):
self.text = "Warning"
def method1(self):
warnings.warn(
"method1 is deprecated, use new_method instead",
DeprecationWarning
)
print ('method1', len(self.text))
def method2(self):
warnings.warn(
"method2 will be deprecated in version 2, use new_method instead",
PendingDeprecationWarning
)
print ('method2', len(self.text))
def new_method(self):
print ('new method', len(self.text))
if __name__=='__main__':
e = WarnExample()
e.method1()
e.method2()
e.new_method() หากสคริปต์ข้างต้นถูกเรียกใช้จากพรอมต์คำสั่งเป็น
E:\python37>python warningexample.py
ไม่มีข้อความเตือนปรากฏบนเทอร์มินัล เพื่อที่คุณจะต้องใช้สวิตช์ _Wd ดังต่อไปนี้
E:\python37>python -Wd warningexample.py warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead DeprecationWarning method1 7 warningexample.py:19: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead PendingDeprecationWarning method2 7 new method 7
ในทำนองเดียวกัน การติดตามเซสชันแบบโต้ตอบก็ไม่แสดงข้อความเตือนใดๆ เช่นกัน
E:\python37>python >>> from warningexample import WarnExample >>> e = WarnExample() >>> e.method1() method1 7 >>> e.method2() method2 7 >>> e.new_method() new method 7
คุณต้องเริ่มเซสชัน Python ด้วย –Wd
E:\python37>python -Wd >>> from warningexample import WarnExample >>> e=WarnExample() >>> e.method1() E:\python37\warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead DeprecationWarning method1 7 >>> e.method2() E:\python37\warningexample.py:17: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead PendingDeprecationWarning method2 7 >>> e.new_method() new method 7
คำเตือนตัวกรอง
ตัวกรองคำเตือนจะควบคุมว่าคำเตือนจะถูกละเว้น แสดง หรือกลายเป็นข้อผิดพลาด (ทำให้เกิดข้อยกเว้น)
| การกระทำ | ความหมาย |
|---|---|
| ข้อผิดพลาด | เปลี่ยนคำเตือนเป็นข้อยกเว้น |
| ละเว้น | ยกเลิกคำเตือน |
| เสมอ | ส่งคำเตือนเสมอ |
| ค่าเริ่มต้น | พิมพ์คำเตือนในครั้งแรกที่สร้างจากแต่ละตำแหน่ง |
| โมดูล | พิมพ์คำเตือนในครั้งแรกที่สร้างจากแต่ละโมดูล |
| ครั้งเดียว | พิมพ์คำเตือนในครั้งแรกที่สร้าง |
การติดตามเซสชันแบบโต้ตอบตั้งค่าตัวกรองเป็นค่าเริ่มต้นโดยฟังก์ชัน simplefilter()
E:\python37>python
>>> import warnings
>>> warnings.simplefilter('default')
>>> from warningexample import WarnExample
>>> e=WarnExample()
>>> e.method1()
E:\python37\warningexample.py:10: DeprecationWarning: method1 is deprecated, use new_method instead
DeprecationWarning
method1 7
>>> e.method2()
E:\python37\warningexample.py:17: PendingDeprecationWarning: method2 will be deprecated in version 2, use new_method instead
PendingDeprecationWarning
method2 7
>>> e.new_method()
new method 7 หากต้องการระงับคำเตือนชั่วคราว ให้ตั้งค่าตัวกรองแบบง่ายเป็น 'ละเว้น'
import warnings
def function():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
function()