def first():
print("First")
def second():
print("Second")
def third():
print("Third")
everything = [first, second, third]
for func in everything:
func()
First
Second
Third
def zero():
print("zero")
def one(value):
print("one", value)
for func in [zero, one]:
func()
zero
Traceback (most recent call last):
File "/sdx/test/signature.py", line 8, in <module>
func()
TypeError: one() missing 1 required positional argument: 'value'
type to see if something is a functionprint(type(3))
<class 'int'>
def example():
pass
print(type(example))
<class 'function'>
print(type(len))
<class 'builtin_function_or_method'>
callable to check if something can be calleddef example():
pass
print(callable(example), callable(len))
True True
assert to check resultsTrue, does nothingAssertionErrordef sign(value):
if value < 0:
return -1
else:
return 1
def test_sign_negative():
assert sign(-3) == -1
def test_sign_positive():
assert sign(19) == 1
def test_sign_zero():
assert sign(0) == 0
def test_sign_error():
assert sgn(1) == 1
TESTS = [
test_sign_negative,
test_sign_positive,
test_sign_zero,
test_sign_error
]
run_tests(TESTS)
pass 2
fail 1
error 1
TESTSimport pprint
pprint.pprint(globals())
{'__annotations__': {},
'__builtins__': <module 'builtins' (built-in)>,
'__cached__': None,
'__doc__': None,
'__file__': '/sdx/test/globals.py',
'__loader__': <_frozen_importlib_external.SourceFileLoader object \
at 0x109d65290>,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'pprint': <module 'pprint' from \
'/sdx/conda/envs/sdxpy/lib/python3.11/pprint.py'>}
import pprint
my_variable = 123
pprint.pprint(globals())
{'__annotations__': {},
'__builtins__': <module 'builtins' (built-in)>,
'__cached__': None,
'__doc__': None,
'__file__': '/sdx/test/globals_plus.py',
'__loader__': <_frozen_importlib_external.SourceFileLoader object \
at 0x108039290>,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'my_variable': 123,
'pprint': <module 'pprint' from \
'/sdx/conda/envs/sdxpy/lib/python3.11/pprint.py'>}
locals gives local variablesdef find_tests(prefix):
for (name, func) in globals().items():
if name.startswith(prefix):
print(name, func)
find_tests("test_")
test_sign_negative <function test_sign_negative at 0x105bcd440>
test_sign_positive <function test_sign_positive at 0x105bcd4e0>
test_sign_zero <function test_sign_zero at 0x105bcd580>
test_sign_error <function test_sign_error at 0x105bcd620>
def run_tests():
results = {"pass": 0, "fail": 0, "error": 0}
for (name, test) in globals().items():
if not name.startswith("test_"):
continue
try:
test()
results["pass"] += 1
except AssertionError:
results["fail"] += 1
except Exception:
results["error"] += 1
print(f"pass {results['pass']}")
print(f"fail {results['fail']}")
print(f"error {results['error']}")