Hướng dẫn if x is none python - nếu x không phải là python

Python if x is not None hay if not x is None?

TLDR: Trình biên dịch Bytecode phân tích cả hai đến x is not None - vì vậy để có thể đọc được, hãy sử dụng if x is not None.

Khả năng đọc

Chúng tôi sử dụng Python vì chúng tôi coi trọng những thứ như khả năng đọc của con người, khả năng sử dụng và tính đúng đắn của các mô hình khác nhau của lập trình về hiệu suất.

Python tối ưu hóa cho khả năng đọc, đặc biệt là trong bối cảnh này.

Phân tích cú pháp và biên dịch mã byte

>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
1 liên kết yếu hơn
>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
2, do đó không có sự khác biệt logic ở đây. Xem tài liệu:

Các toán tử

>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
2 và
>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
4 Kiểm tra cho nhận dạng đối tượng:
>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
5 là đúng khi và chỉ khi X và Y là cùng một đối tượng.
>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
6 mang lại giá trị sự thật nghịch đảo.

>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
4 được quy định cụ thể trong ngữ pháp Python như là một cải tiến dễ đọc cho ngôn ngữ:

comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'

Và vì vậy nó cũng là một yếu tố đơn nhất của ngữ pháp.

Tất nhiên, nó không được phân tích cú pháp giống nhau:

>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"

Nhưng sau đó, trình biên dịch byte thực sự sẽ dịch

>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
8 sang
>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
4:

>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE

Vì vậy, vì mục đích đọc và sử dụng ngôn ngữ như dự định, vui lòng sử dụng

>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
4.

Để không sử dụng nó là không khôn ngoan.is not wise.

Bạn không thể xử lý sự thật!

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!
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à:

>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
1 và if x is not None không giống nhau! Hệ quả:
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
3 và
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
4 cũng khá khác nhau, rõ ràng.
corollary:
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
3 and
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
4 are also quite different, obviously.

Điều này thường xảy ra khi ai đó gán

>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
5 cho một biến (giả sử,
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
6) làm giá trị sentinel, và sau đó
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
6 có thể hoặc không thể được gán. Thử nghiệm được thiết kế để kiểm tra xem
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
6 có được gán hay không.

Khi bạn thực hiện

>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
4, bạn gọi cho người vận hành
>>> import ast
>>> ast.dump(ast.parse('x is not None').body[0].value)
"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])"
>>> ast.dump(ast.parse('not x is None').body[0].value)
"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
2, kiểm tra danh tính của
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
6.
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
5 là một singleton trong Python và tất cả các giá trị
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
5 cũng là cùng một trường hợp chính xác. Khi bạn nói
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
1, một cái gì đó khác nhau xảy ra. Nếu mong đợi một boolean và giả sử
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
6 không phải là boolean, Python sẽ tự động gọi ________ 26 Phương pháp
>>> x = None 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None'
7. tức là,
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
1 thực sự được thực hiện là
>>> x = None 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None'
9 (hoặc
>>> x = [] 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None' 
if x is not None 
0).
>>> x = None 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None'
7 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à,
>>> x = [] 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None' 
if x is not None 
2 trả về
>>> x = [] 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None' 
if x is not None 
3, vì vậy nếu x không có,
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
1 hoạt động như bạn mong đợi. Tuy nhiên, có những giá trị khác được đánh giá là
>>> x = [] 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None' 
if x is not None 
3. Ví dụ nổi bật nhất là 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 
6 trả về
>>> x = [] 
... if x: 
...     print 'if x' 
... if x is not None: 
...     print 'if x is not None' 
if x is not None 
3 là tốt. Thông thường, một danh sách trống có một ý nghĩa khác với
>>> import dis
>>> dis.dis(lambda x, y: x is not y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x, y: not x is y)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                1 (y)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
5; 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 >>> import dis >>> dis.dis(lambda x, y: x is not y) 1 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 COMPARE_OP 9 (is not) 9 RETURN_VALUE >>> dis.dis(lambda x, y: not x is y) 1 0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 COMPARE_OP 9 (is not) 9 RETURN_VALUE 5

>>> 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

Bài viết tương tự

    X không có nghĩa là gì trong Python?

    Gán giá trị Không có biến: x = none.: x = None.

    Làm thế nào để bạn biết nếu x không phải là không có trong Python?

    Sử dụng toán tử IS không để kiểm tra xem một biến không phải là không có trong Python, ví dụ:Nếu my_var không phải là không:.Toán tử không trả về đúng nếu các giá trị ở bên trái và bên phải không trỏ đến cùng một đối tượng (cùng một vị trí trong bộ nhớ). to check if a variable is not None in Python, e.g. if my_var is not None: . The is not operator returns True if the values on the left-hand and right-hand sides don't point to the same object (same location in memory).

    Làm thế nào để bạn kiểm tra xem một đối tượng không có trong Python?

    Để kiểm tra xem một biến có hay không, hãy sử dụng toán tử IS trong Python.Với toán tử IS, sử dụng đối tượng cú pháp là không có để trả về true nếu đối tượng có loại không định dạng và sai.use the is operator in Python. With the is operator, use the syntax object is None to return True if the object has the type NoneType and False otherwise.

    Không có nhà điều hành trong Python?

    Từ khóa không được sử dụng để xác định biến null hoặc một đối tượng.Trong Python, không có từ khóa nào là một đối tượng và nó là một loại dữ liệu của lớp không.Chúng tôi có thể gán không cho bất kỳ biến nào, nhưng bạn không thể tạo các đối tượng phi kiểu khác.Lưu ý: Tất cả các biến được gán không có điểm cho cùng một đối tượng.. In Python, None keyword is an object, and it is a data type of the class NoneType . We can assign None to any variable, but you can not create other NoneType objects. Note: All variables that are assigned None point to the same object.