我遇到以下错误:
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/