【问题标题】:Concat the results of type() to a string in python将 type() 的结果连接到 python 中的字符串
【发布时间】:2012-03-20 20:30:44
【问题描述】:

如何将 type() 的结果连接到 Python 中的字符串?

我尝试这样做的原因是出于调试目的(在没有逐行调试的谷歌应用引擎服务器上)。这是我的代码:

logging.debug('Type is: ' + type(some_var))

我得到的错误是:TypeError: cannot concatenate 'str' and 'PropertiedClass' objects

【问题讨论】:

    标签: python debugging logging types tostring


    【解决方案1】:

    这就是我们改用 repr 的原因。

    'Type is: %r' % (type(...),)
    

    【讨论】:

      【解决方案2】:

      与您可能习惯的其他语言(如 C)不同,Python 是强类型的。 type 返回一个 type 对象。您需要将其转换为字符串:

      logging.debug('Type is: ' + str(type(some_var)))
      

      甚至更好:

      logging.debug('Type is: %s' % type(some_var))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-18
        • 1970-01-01
        • 1970-01-01
        • 2016-05-28
        • 2012-05-26
        • 1970-01-01
        • 2012-03-27
        相关资源
        最近更新 更多