You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
这在技术上并不像前面的例子那样是错误的,因为`False`是一个单子 —— 就像`None`和`True`一样 —— 这意味着,实际上`False`只有一个实例可以用来比较。但是这样做的问题在于,它带来了不必要的限制。幸好Python是[鸭子类型(duck typing)](https://en.wikipedia.org/wiki/Duck_typing)的庞大的支持者,tying down any API specifically to only `True` or `False` is frowned upon as it locks an API to a specific type. If for some reason the API changed to return values of a different type but has the same boolean interpretation then this code would suddenly break. Instead of directly checking for `False`, the code should have simply checked for false value:
And this extends to other types as well, so don’t do `spam == []` if you care if something is empty, simply do `not spam` in case the API that gave you the value for `spam` suddenly starts returning tuples instead of lists.
About the only time you might legitimately find the need to use `is` in day-to-day code is with `None`. Sometimes you might come across an API where `None` has special meaning, in which case you should use `is None` to check for that specific value. For example, modules in Python have a [`__package__`属性](https://docs.python.org/3/reference/import.html#__package__) which stores a string representing what package the module belongs to. The trick is that top-level modules — i.e., modules that are not contained in a package — have `__package__` set to the empty string which is false but is a valid value, but there is a need to have a value represent not knowing what `__package__` should be set to. In that instance, `None` is used to represent “I don’t know”. This allows the [code that calculates what package a module belongs to](https://github.com/python/cpython/blob/d0ffca8d6aa055300f7361e4bea2d4def0fca571/Lib/importlib/_bootstrap.py#L1031) to use:
package isNone# OK when you need an "I don't know" value.
45
45
```
46
46
47
-
to detect if the package name isn’t known (`not package` would incorrectly think that `''` represented that as well). Do make sure to not overuse this kind of use of `None`, though, as a false value tends to meet the need of representing “I don’t know”.
另一点建议是,在我们自己的类中定义`__bool__()`之前三思而后行。While you should definitely define the method on classes representing containers (to help with that “empty if false” concept), in all cases you should stop and think about whether it truly makes sense to define the method. While it may be tempting to use `__bool__()` to represent some sort of state of an object, the ramifications can be surprisingly far-reaching as it means suddenly people have to start explicitly checking for some special value like `None` which represents whether an API returned an actual value or not instead of simply relying on all object defaulting to being true. As an example of how defining `__bool__()` can be surprising, see the [Python issue](https://bugs.python.org/issue13936) where there was a multi-year discussion over how defining `datetime.time()` to be false at midnight but true for all other values was a mistake and how best to fix it (in the end the implementation of `__bool__()` was [removed in Python 3.5](https://docs.python.org/3/whatsnew/3.5.html#changes-in-the-python-api)).
0 commit comments