python - 如何解决TypeError : can only concatenate str (not “types.GenericAlias” ) to str

标签 python error-handling

我遇到以下错误:
TypeError:只能将str(而不是“types.GenericAlias”)连接到str
该错误反射(reflect)了什么以及如何解决?
据我所记得,我试图将一个字符串与一个表达式中的变量相乘,变量i是for循环中的迭代变量。但是,我无法再次找到特定的示例。我已经注意到该错误,因为我找不到任何示例

最佳答案

从错误消息中应该显而易见。您不能将字符串连接到其他对象。

'str1' + 'str2'    # OK
'str1' + MyClass() # TypeError

# But, you can also try to cast an object to str
# In this case Python will try to get a result from __str__ method
# And will fallback to __repr__ if __str__ is not defined
'str1' + str(MyClass()) # OK
因此,仅连接字符串。

关于python - 如何解决TypeError : can only concatenate str (not “types.GenericAlias” ) to str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65050162/

相关文章:

python - 我可以在 Python 中收到始终评估为 true 的警告吗?

python - tensorflow 图节点名称与操作名称不匹配

Python请求异常超时

bash - 为什么有些人出错时退出 -1 而不是退出 1?

javascript - 检查多个变量是否已设置的最佳方法是什么?

python - Pandas:groupby 列出

python - 如何在范围 session 中使用 pytest-aiohttp 装置

python - Kivy 自定义按钮文本

asp.net - ASP.NET MVC 4自定义错误 View 问题

java - 是否可以响应 http 错误,该代码在 java spring 中是可变的?