data is not None

在读django的form源码:

def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
             initial=None, error_class=ErrorList, label_suffix=None,
             empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None):
    self.is_bound = data is not None or files is not None
    self.data = data or {}
    self.files = files or {}
    self.auto_id = auto_id
    if prefix is not None:
        self.prefix = prefix
    self.initial = initial or {}
    self.error_class = error_class
    # Translators: This is the default suffix added to form field labels
    self.label_suffix = label_suffix if label_suffix is not None else _(':')
    self.empty_permitted = empty_permitted
    self._errors = None  # Stores the errors after clean() has been called.

    # The base_fields class attribute is the *class-wide* definition of
    # fields. Because a particular *instance* of the class might want to
    # alter self.fields, we create self.fields here by copying base_fields.
    # Instances should always modify self.fields; they should not modify
    # self.base_fields.
    self.fields = copy.deepcopy(self.base_fields)
    self._bound_fields_cache = {}
    self.order_fields(self.field_order if field_order is None else field_order)

发现self.is_bound = data is not None

Microsoft Windows [版本 10.0.17134.285]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Users\Roc Sun>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> lis = []
>>> dic = {}
>>> print(lis is not None)
True
>>> print(lis is not None)
True
>>> dic['a'] = 111
>>> print(dic)
{'a': 111}
>>> print(dic is not None)
True
>>> lis = None
>>> print(lis)
None
>>> print(lis is not None)
False
>>>
  • 空列表,空字典都是True
  • None是Ture

这里的None是指None,而不是空字符串,空列表,空元组,空字典等。

猜你喜欢

转载自www.cnblogs.com/linga/p/9698120.html