Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用frozensets。
当我这样做时print(my_frozenset),输出类似于“frozenset({1, 2, 3})”。
print(my_frozenset)
但是我有很多嵌套的frozensets,我发现这个打印很长而且很难阅读。
我想修改它,以便print(my_frozenset)输出,例如,“fs{1,2,3}”或不同的东西。
不建议直接修改原生代码。
您可以定义新函数来打印frozenset或新类继承frozenset。
frozenset
如果你必须更换内置的frozenset,你可以试试这样
import builtins class _frozenset(frozenset): def __str__(self): return "custom..." builtins.frozenset = _frozenset print(frozenset([1, 2, 3]))
但这可能会导致意外错误