Hướng dẫn how check value is null or not in python? - làm thế nào kiểm tra giá trị là null hay không trong python?

# In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2, Python's null?

Không có

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
3 trong Python; Thay vào đó là
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2. Như đã nêu, cách chính xác nhất để kiểm tra rằng một cái gì đó đã được cung cấp
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 dưới dạng giá trị là sử dụng toán tử nhận dạng
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
6, kiểm tra rằng hai biến đề cập đến cùng một đối tượng.

Show
>>> foo is None
True
>>> foo = 'bar'
>>> foo is None
False

Những thứ cơ bản

Có và chỉ có thể có một # In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 là ví dụ duy nhất của lớp
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
9 và bất kỳ nỗ lực nào nữa trong việc khởi tạo lớp sẽ trả về cùng một đối tượng, điều này làm cho
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 trở thành người độc thân. Những người mới đến Python thường thấy các thông báo lỗi đề cập đến
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
9 và tự hỏi nó là gì. Theo ý kiến ​​cá nhân của tôi, những tin nhắn này chỉ đơn giản là chỉ đề cập đến ____32 theo tên bởi vì, như chúng ta sẽ thấy trong thời gian ngắn,
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 để lại ít chỗ cho sự mơ hồ. Vì vậy, nếu bạn thấy một số thông điệp
if value==None:
    pass
4 đề cập rằng
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
9 không thể làm điều này hoặc không thể làm điều đó, chỉ cần biết rằng đó chỉ đơn giản là một
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 đang được sử dụng theo cách mà nó không thể.

Ngoài ra,

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 là một hằng số tích hợp. Ngay khi bạn bắt đầu Python, nó có sẵn để sử dụng từ mọi nơi, cho dù trong mô -đun, lớp hoặc chức năng. Ngược lại,
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
9 không phải là bạn cần phải có một tham chiếu đến nó trước bằng cách truy vấn
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 cho lớp của nó.

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType

Bạn có thể kiểm tra tính độc đáo của ____ 32 với chức năng nhận dạng của Python

if not value:
    pass
1. Nó trả về số duy nhất được gán cho một đối tượng, mỗi đối tượng có một. Nếu ID của hai biến là giống nhau, thì chúng chỉ ra cùng một đối tượng.

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000

# In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2 không thể bị ghi đè

Trong các phiên bản cũ hơn của Python (trước 2.4), có thể gán lại

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2, nhưng không còn nữa. Thậm chí không phải là một thuộc tính lớp hoặc trong giới hạn của một hàm.

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword

Do đó, an toàn khi cho rằng tất cả các tài liệu tham khảo

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 đều giống nhau. Không có bất kỳ "tùy chỉnh"
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 nào.

Để kiểm tra # In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2 sử dụng toán tử # In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 6

Khi viết mã, bạn có thể bị cám dỗ để kiểm tra cho người khác như thế này:

if value==None:
    pass

Hoặc để kiểm tra sự giả dối như thế này

if not value:
    pass

Bạn cần hiểu những hàm ý và tại sao nó thường là một ý tưởng tốt để rõ ràng.

Trường hợp 1: Kiểm tra xem giá trị là # In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2

Tại sao

value is None

còn hơn là

value==None

?

Đầu tiên là tương đương với:

id(value)==id(None)

Trong khi biểu thức

if not value:
    pass
9 trên thực tế được áp dụng như thế này

value.__eq__(None)

Nếu giá trị thực sự là

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 thì bạn sẽ nhận được những gì bạn mong đợi.

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
0

Trong hầu hết các trường hợp phổ biến, kết quả sẽ giống nhau, nhưng phương pháp

value is None
1 mở ra một cánh cửa không có bất kỳ sự đảm bảo nào về độ chính xác, vì nó có thể được ghi đè trong một lớp để cung cấp hành vi đặc biệt.

Hãy xem xét lớp học này.

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
1

Vì vậy, bạn đã thử nó trên

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 và nó hoạt động

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
2

Nhưng sau đó nó cũng hoạt động trên chuỗi trống

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
3

Và chưa

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
4

Trường hợp 2: Sử dụng # In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2 làm boolean

Hai bài kiểm tra sau

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
5

trên thực tế được đánh giá là

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
6

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 là một "giả", có nghĩa là nếu được chuyển đến boolean, nó sẽ trả lại
value is None
5 và nếu áp dụng toán tử
value is None
6, nó sẽ trả về
value is None
7. Tuy nhiên, lưu ý rằng nó không phải là một tài sản duy nhất cho
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2. Ngoài chính
value is None
5, tài sản được chia sẻ bởi các danh sách trống, bộ dữ liệu, bộ, dicts, chuỗi, cũng như 0 và tất cả các đối tượng từ các lớp thực hiện phương pháp ma thuật
value==None
0 để trả về
value is None
5.

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
7

Vì vậy, khi kiểm tra các biến theo cách sau, hãy nhận thức thêm về những gì bạn bao gồm hoặc loại trừ khỏi bài kiểm tra:

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
8

Ở trên, bạn có nghĩa là gọi

value==None
2 khi giá trị được đặt cụ thể thành
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 hoặc bạn có nghĩa là giá trị được đặt thành
value==None
4 hoặc chuỗi trống hoặc danh sách trống cũng sẽ kích hoạt khởi tạo? Như tôi đã nói, hãy chú ý. Vì nó thường như vậy, trong Python rõ ràng là tốt hơn tiềm ẩn.

# In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2 trong thực tế

# In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2 được sử dụng làm giá trị tín hiệu

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 có trạng thái đặc biệt trong Python. Đó là một giá trị cơ bản yêu thích vì nhiều thuật toán coi nó là một giá trị đặc biệt. Trong các kịch bản như vậy, nó có thể được sử dụng như một cờ để báo hiệu rằng một điều kiện yêu cầu một số xử lý đặc biệt (chẳng hạn như cài đặt của giá trị mặc định).

Bạn có thể gán

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 cho các đối số từ khóa của một hàm và sau đó kiểm tra rõ ràng cho nó.

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
9

Bạn có thể trả lại nó dưới dạng mặc định khi cố gắng truy cập thuộc tính của một đối tượng và sau đó kiểm tra rõ ràng cho nó trước khi làm điều gì đó đặc biệt.

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
0

Theo mặc định, phương thức

value==None
9 của từ điển trả về
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 khi cố gắng truy cập khóa không tồn tại:

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
1

Nếu bạn cố gắng truy cập nó bằng cách sử dụng ký hiệu chỉ số,

id(value)==id(None)
1 sẽ được nêu ra

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
2

Tương tự như vậy nếu bạn cố gắng bật một mặt hàng không tồn tại

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
3

mà bạn có thể triệt tiêu với giá trị mặc định thường được đặt thành

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
4

# In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2 được sử dụng làm cờ và giá trị hợp lệ

Các ứng dụng được mô tả ở trên của

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 áp dụng khi nó không được coi là giá trị hợp lệ, nhưng giống như một tín hiệu để làm điều gì đó đặc biệt. Tuy nhiên, có những tình huống đôi khi có vấn đề cần biết
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 đến từ đâu vì mặc dù nó được sử dụng như một tín hiệu, nó cũng có thể là một phần của dữ liệu.

Khi bạn truy vấn một đối tượng cho thuộc tính của nó với

id(value)==id(None)
6, lấy lại
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 không cho bạn biết nếu thuộc tính bạn đang cố gắng truy cập đã được đặt thành
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 hoặc nếu nó hoàn toàn vắng mặt với đối tượng. Tình huống tương tự khi truy cập khóa từ từ điển, như
id(value)==id(None)
9, bạn không biết liệu
value.__eq__(None)
0 có bị thiếu hay nếu nó chỉ được đặt thành
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2. Nếu bạn cần thông tin đó, cách thông thường để xử lý điều này là trực tiếp cố gắng truy cập thuộc tính hoặc khóa từ trong cấu trúc
value.__eq__(None)
2:

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
5

Tương tự với Dict:

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
6

Hai ví dụ trên cho thấy cách xử lý các trường hợp đối tượng và từ điển. Còn chức năng thì sao? Điều tương tự, nhưng chúng tôi sử dụng đối số từ khóa Double Asterisks vào cuối đó:

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
7

# In Python 2.7 >>> class SomeClass(object): ... def my_fnc(self): ... self.None = 'foo' SyntaxError: cannot assign to None >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to None # In Python 3.5 >>> class SomeClass: ... def my_fnc(self): ... self.None = 'foo' SyntaxError: invalid syntax >>> def my_fnc(): None = 'foo' SyntaxError: cannot assign to keyword 2 chỉ được sử dụng làm giá trị hợp lệ

Nếu bạn thấy rằng mã của bạn bị xả vào mẫu

value.__eq__(None)
2 ở trên chỉ để phân biệt giữa các cờ
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 và dữ liệu
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2, thì chỉ cần sử dụng một giá trị kiểm tra khác. Có một mẫu trong đó một giá trị nằm ngoài tập hợp các giá trị hợp lệ được chèn là một phần của dữ liệu trong cấu trúc dữ liệu và được sử dụng để kiểm soát và kiểm tra các điều kiện đặc biệt (ví dụ: ranh giới, trạng thái, v.v.). Giá trị như vậy được gọi là Sentinel và nó có thể được sử dụng theo cách
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 được sử dụng làm tín hiệu. Thật tầm thường để tạo ra một Sentinel trong Python.

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
8

Đối tượng

value.__eq__(None)
8 ở trên là duy nhất và không làm nhiều bất cứ điều gì có thể quan tâm đến một chương trình, do đó, đó là một sự thay thế tuyệt vời cho
# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2 như một cờ. Một số cảnh báo áp dụng, nhiều hơn về điều đó sau mã.

Với chức năng

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000
9

Với Dict

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
0

Với một đối tượng

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
1

Như tôi đã đề cập trước đó, Sentinels tùy chỉnh đi kèm với một số cảnh báo. Đầu tiên, chúng không phải là từ khóa như

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword
2, vì vậy Python không bảo vệ chúng. Bạn có thể ghi đè lên
value.__eq__(None)
8 của mình ở trên bất cứ lúc nào, bất cứ nơi nào trong mô -đun được xác định, vì vậy hãy cẩn thận cách bạn phơi bày và sử dụng chúng. Tiếp theo, trường hợp được trả lại bởi
>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType
02 không phải là một singleton. Nếu bạn thực hiện cuộc gọi đó gấp 10 lần, bạn sẽ nhận được 10 đối tượng khác nhau. Cuối cùng, việc sử dụng một sentinel là rất bình dị. Một sentinel dành riêng cho thư viện mà nó được sử dụng và do đó phạm vi của nó thường được giới hạn trong nội bộ của thư viện. Nó không nên "rò rỉ" ra ngoài. Mã bên ngoài chỉ nên nhận thức được nó, nếu mục đích của chúng là mở rộng hoặc bổ sung API của thư viện.

Null () trong python?

NULL thường được định nghĩa là 0 trong các ngôn ngữ đó, nhưng null trong Python là khác nhau.Python sử dụng từ khóa Không có để xác định các đối tượng và biến null.Mặc dù không ai phục vụ một số mục đích giống như null trong các ngôn ngữ khác, nhưng đó là một con thú khác hoàn toàn.Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it's another beast entirely.

Không phải null () Python?

.notnull là một hàm chung của thư viện gấu trúc trong Python phát hiện nếu các giá trị không bị thiếu đối với một giá trị duy nhất (vô hướng) hoặc các đối tượng giống như mảng.Hàm trả về booleans để phản ánh liệu các giá trị được đánh giá là null (sai) hay không null (true)..Notnull là một bí danh của gấu trúc. notnull is a general function of the pandas library in Python that detects if values are not missing for either a single value (scalar) or array-like objects. The function returns booleans to reflect whether the values evaluated are null (False) or not null (True). . notnull is an alias of the pandas .

Làm thế nào để bạn kiểm tra nếu dữ liệu là null?

Bạn có thể kiểm tra NULL với toán tử loại () trong JavaScript.Thật kỳ lạ, nếu bạn kiểm tra với typeof (), một biến null sẽ trả về đối tượng.Điều này là do một lỗi lịch sử trong JavaScript.with the typeof() operator in JavaScript. Curiously, if you check with typeof() , a null variable will return object . This is because of a historic bug in JavaScript.

Null == null python?

Python Null được gọi là không.Không có đối tượng đặc biệt đại diện cho sự vắng mặt của một giá trị.Một hàm không trả về giá trị tự động trả về không.. None is a special object that represents the absence of a value. A function that does not return a value automatically returns None.