GIẢI TRÌNH
Bạn phải nhận ra rằng True
, False
và None
đều là những người độc thân trong Python, điều đó có nghĩa là tồn tại và chỉ có thể tồn tại một, một trường hợp duy nhất của một đối tượng singleton, do đó tên Singleton. Ngoài ra, bạn không thể sửa đổi một đối tượng Singleton vì trạng thái của nó được đặt trong đá, nếu tôi có thể sử dụng cụm từ đó.one, single instance of a singleton object, hence the name singleton. Also, you can't modify a singleton object because its state is set in stone, if I may use that phrase.
Bây giờ, hãy để tôi giải thích làm thế nào những người độc thân Python được sử dụng.
Chúng ta hãy có một đối tượng Python có tên foo
có giá trị None
, sau đó
>>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
0 đang nói rằng foo
có một giá trị khác với None
. Điều này hoạt động giống như nói >>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
3, về cơ bản là >>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
4.Vì vậy,
>>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
5 và True
hoạt động theo cùng một cách, cũng như None
và False
.>>> foo = not None
>>> bool[foo]
True
>>> foo = 5 # Giving an arbitrary value here
>>> bool[foo]
True
>>> foo = None
>>> bool[foo]
False
>>> foo = 5 # Giving an arbitrary value here
>>> bool[foo]
True
Điều quan trọng cần nhận ra và nhận thức được khi mã hóa là khi so sánh hai đối tượng, None
cần
>>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
0, nhưng True
và False
cần >>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
3. Tránh sử dụng >>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
4, chỉ sử dụng >>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
5. Ngoài ra, tránh sử dụng >>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
6 và chỉ sử dụng >>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
0.CẢNH BÁO
Nếu bạn đang sử dụng
>>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
3 hoặc >>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
9 khi giá trị của foo
xảy ra là None
, hãy cẩn thận với các lỗi tiềm năng trong mã của bạn. Vì vậy, đừng kiểm tra giá trị None
tiềm năng trong các câu lệnh có điều kiện theo cách này. Ở phía an toàn bằng cách kiểm tra nó như đã giải thích ở trên, tức là, >>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
5 hoặc >>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
0. Điều rất quan trọng là phải tuân theo các thực tiễn tốt nhất được chia sẻ bởi các chuyên gia Python.very important to follow best practices shared by Python experts.LỜI NHẮC NHỞ
True
là
>>> x = 42
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x if x is not None
6 và False
là >>> x = 42
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x if x is not None
8.Vào những ngày xưa của Python, chúng tôi chỉ có số nguyên
>>> x = 42
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x if x is not None
6 để thể hiện một giá trị sự thật và chúng tôi đã có số nguyên >>> x = 42
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x if x is not None
8 để thể hiện giá trị giả. Tuy nhiên, việc nói True
là dễ hiểu hơn và thân thiện với con người hơn và nói rằng False
là dễ hiểu hơn và thân thiện với con người hơn thay vì >>> x = 42
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x if x is not None
8. Ngày 25 tháng 8 năm 2012 Bài đăng này đã được vài năm tuổi, vì vậy một số chi tiết [hoặc ý kiến của tôi] có thể đã lỗi thời. Tôi vẫn rất thích nghe phản hồi của bạn trong các ý kiến dưới đây. Vui thích!Bạn không thể xử lý sự thật!
I would still love to hear your feedback in the comments below. Enjoy!
Tôi có cơ hội xem xét một số người khác Mã Python Python gần đây, và có một nhận xét mà tôi hầu như luôn phải đưa ra, đó là: đó là:
5 và>>> class Foo[object]: ... def __nonzero__[self]: ... print 'Foo is evaluated to a boolean!' ... return True ... ... x = Foo[] ... if x: ... print 'if x' ... if x is not None: ... print 'if x is not None' Foo is evaluated to a boolean! if x if x is not None
6 không giống nhau! Hệ quả:>>> class Foo[object]: ... def __nonzero__[self]: ... print 'Foo is evaluated to a boolean!' ... return True ... ... x = Foo[] ... if x: ... print 'if x' ... if x is not None: ... print 'if x is not None' Foo is evaluated to a boolean! if x if x is not None
7 và>>> class Foo[object]: ... def __nonzero__[self]: ... print 'Foo is evaluated to a boolean!' ... return True ... ... x = Foo[] ... if x: ... print 'if x' ... if x is not None: ... print 'if x is not None' Foo is evaluated to a boolean! if x if x is not None
8 cũng khá khác nhau, rõ ràng.>>> class Foo[object]: ... def __nonzero__[self]: ... print 'Foo is evaluated to a boolean!' ... return True ... ... x = Foo[] ... if x: ... print 'if x' ... if x is not None: ... print 'if x is not None' Foo is evaluated to a boolean! if x if x is not None
corollary:7 and>>> class Foo[object]: ... def __nonzero__[self]: ... print 'Foo is evaluated to a boolean!' ... return True ... ... x = Foo[] ... if x: ... print 'if x' ... if x is not None: ... print 'if x is not None' Foo is evaluated to a boolean! if x if x is not None
8 are also quite different, obviously.>>> class Foo[object]: ... def __nonzero__[self]: ... print 'Foo is evaluated to a boolean!' ... return True ... ... x = Foo[] ... if x: ... print 'if x' ... if x is not None: ... print 'if x is not None' Foo is evaluated to a boolean! if x if x is not None
Điều này thường xảy ra khi ai đó gán None
cho một biến [giả sử, True
0] làm giá trị sentinel, và sau đó True
0 có thể hoặc không được gán cho. Thử nghiệm được thiết kế để kiểm tra xem True
0 có được gán hay không.
Khi bạn thực hiện
>>> class Foo[object]:
... def __nonzero__[self]:
... print 'Foo is evaluated to a boolean!'
... return True
...
... x = Foo[]
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
Foo is evaluated to a boolean!
if x
if x is not None
8, bạn gọi cho người vận hành >>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
0, kiểm tra danh tính của True
0. None
là một singleton trong Python và tất cả các giá trị None
cũng là cùng một trường hợp chính xác. Khi bạn nói >>> class Foo[object]:
... def __nonzero__[self]:
... print 'Foo is evaluated to a boolean!'
... return True
...
... x = Foo[]
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
Foo is evaluated to a boolean!
if x
if x is not None
5, một cái gì đó khác nhau xảy ra. Nếu mong đợi một boolean và giả sử True
0 không phải là boolean, Python sẽ tự động gọi ________ 50, False
1. tức là, >>> class Foo[object]:
... def __nonzero__[self]:
... print 'Foo is evaluated to a boolean!'
... return True
...
... x = Foo[]
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
Foo is evaluated to a boolean!
if x
if x is not None
5 thực sự được thực hiện là False
3 [hoặc False
4]. False
1 có tên khá kém1, nhưng nó là một phương pháp đánh giá một lớp là giá trị boolean. Nó là một trong những phương pháp ma thuật Python. Điều khó hiểu là, False
6 trả về False
, vì vậy nếu x không có, >>> class Foo[object]:
... def __nonzero__[self]:
... print 'Foo is evaluated to a boolean!'
... return True
...
... x = Foo[]
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
Foo is evaluated to a boolean!
if x
if x is not None
5 hoạt động như bạn mong đợi. Tuy nhiên, có những giá trị khác được đánh giá là False
. Ví dụ nổi bật nhất là một danh sách trống. None
0 trả về False
là tốt. Thông thường, một danh sách trống có một ý nghĩa khác với None
; Không có nghĩa là không có giá trị trong khi một danh sách trống có nghĩa là các giá trị bằng không. Về mặt ngữ nghĩa, chúng khác nhau. Tôi đoán mọi người chỉ không biết về sự khác biệt ngữ nghĩa giữa hai cách để viết điều kiện.Dưới đây là một số đoạn trích hữu ích để chứng minh:
Thử nghiệm None
>>> x = None
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
Kiểm tra một danh sách trống
>>> x = []
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x is not None
Kiểm tra giá trị bình thường
>>> x = 42
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
if x if x is not None
Kiểm tra một lớp tùy chỉnh
>>> class Foo[object]:
... def __nonzero__[self]:
... print 'Foo is evaluated to a boolean!'
... return True
...
... x = Foo[]
... if x:
... print 'if x'
... if x is not None:
... print 'if x is not None'
Foo is evaluated to a boolean!
if x
if x is not None
Thảo luận về bài viết này tại phần bình luận dưới đây. Theo dõi tôi trên Twitter và FacebookDiscuss this post at the comment section below. Follow me on Twitter and Facebook