You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Colab中用Ice Cream输出格式化消息?

解决Ice Cream库在Colab中输出纯值并保留全局禁用功能的方法
  • 自定义输出格式化逻辑
    Ice Cream支持通过ic.configureOutput()定制输出行为,我们可以指定outputFunction实现纯值输出,完全替代默认的带前缀格式:

    from icecream import ic
    
    # 自定义输出函数,仅打印传入的内容
    def plain_print(value):
        print(value)
    
    # 应用自定义配置
    ic.configureOutput(outputFunction=plain_print)
    
    # 测试示例
    func_name = "init"
    ic(f"function {func_name} started")  # 输出:function init started
    
  • 保留全局禁用/启用功能
    原有的ic.disable()和ic.enable()依然生效,不会因为自定义输出而受影响:

    # 禁用所有ic输出
    ic.disable()
    ic("这条内容不会被打印")
    
    # 重新启用输出
    ic.enable()
    ic("这条内容会以纯值格式输出")
    
  • 临时恢复默认格式
    如果需要切换回Ice Cream的默认输出样式,只需重置outputFunction为None:

    ic.configureOutput(outputFunction=None)
    ic(f"function {func_name} started")  # 回到默认格式:ic| "function {} started".format(func_name): 'function init started'
    

内容的提问来源于stack exchange,提问作者Eduardo Passeto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 22:32:34