"""演示 is(身份)与 ==(相等),以及 CPython 的缓存现象。""" from __future__ import annotations def _new_list() -> list[int]: return [] def identity_facts() -> dict[str, bool]: """返回一组可复现的身份比较结果。""" small_a = int("256") small_b = int("256") large_a = int("257") large_b = int("257") literal_a = "abc" literal_b = "abc" joined_a = "".join(["a", "b", "c"]) joined_b = "".join(["a", "b", "c"]) return { "small_int_is": small_a is small_b, "large_int_is": large_a is large_b, "literal_str_is": literal_a is literal_b, "joined_str_is": joined_a is joined_b, "empty_list_is": _new_list() is _new_list(), "equal_but_not_identical": large_a == large_b and large_a is not large_b, } def main() -> None: for key, value in identity_facts().items(): print(f"{key}: {value}") if __name__ == "__main__": main()