def find_local_py_scripts():import os # does not cost if already importedfor entry in os.scandir('.'):# find files ending with .pyif entry.is_file() and entry.name.endswith(".py") :print("- ", entry.name)find_local_py_scripts()
- googlenet_custom_layers.py- GoogLeNet_Inception_v1.py
>>> def print_and_return(value):... print(value)... return value
>>> res = print_and_return(False) and print_and_return(True)False
正如你所看到的,只有一个print语句被执行,所以Python实际上甚至没有看正确的操作数。
二元运算符并非如此。它们总是评估两个操作数:
>>> res = print_and_return(False) & print_and_return(True);FalseTrue
但是,如果第一个操作数不够,那么当然会评估第二个运算符:
>>> res = print_and_return(True) and print_and_return(False);TrueFalse
总结一下,这里是另一个表格:
+-----------------+-------------------------+| Expression | Right side evaluated? |+=================+=========================+| `True` and ... | Yes |+-----------------+-------------------------+| `False` and ... | No |+-----------------+-------------------------+| `True` or ... | No |+-----------------+-------------------------+| `False` or ... | Yes |+-----------------+-------------------------+
class Test(object):def __init__(self, value):self.value = value
def __bool__(self):print('__bool__ called on {!r}'.format(self))return bool(self.value)
__nonzero__ = __bool__ # Python 2 compatibility
def __repr__(self):return "{self.__class__.__name__}({self.value})".format(self=self)
因此,让我们看看该类与这些运算符组合会发生什么:
>>> if Test(True) and Test(False):... pass__bool__ called on Test(True)__bool__ called on Test(False)
>>> if Test(False) or Test(False):... pass__bool__ called on Test(False)__bool__ called on Test(False)
>>> if not Test(True):... pass__bool__ called on Test(True)
>>> import numpy as np>>> arr = np.array([1,2,3])>>> bool(arr)ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()>>> arr and arrValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> import pandas as pd>>> s = pd.Series([1,2,3])>>> bool(s)ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().>>> s and sValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().