为什么我不能使用'+'在打印功能中添加'int'到'str'?
Why can I not add an 'int' to 'str' in print function by using '+'?
print ("hello"+"world)
或
print ("hello"
+"world")
将输出为:
hello world
但是,当我尝试使用“+”将变量 (int) 与 'str' 一起插入打印函数时,出现错误。但是,当我改用 ',' 时,它可以正常工作。即:
x=1
print("the value of 'x' is " + x + ".")
显示错误
TypeError: can only concatenate str (not "int") to str
但是当我使用时:
print("the value of 'x' is ",x,".")
我得到了想要的输出。
我想知道为什么python这样设计,在这种情况下使用'+'是什么意思。谢谢。
编辑:感谢您的回复,原意是因为 Java + 意味着简单地放在一起。
print()
只是一个函数,它的设计方式是接受可变数量的位置参数,并在内部将它们转换为 str.
如你所见here at it's documentation:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
. . .
All non-keyword arguments are converted to strings like str() does
and
written to the stream, separated by sep and followed by end. Both sep
and end must be strings; they can also be None, which means to use the
default values. If no objects are given, print() will just write end.
. . .
当您执行 print("the value of 'x' is ",x,".")
时,您将 3 个不同的参数传递给函数,函数得到的参数为 objects
。并如上所述将它们转换为字符串。
但是,在行 print("the value of 'x' is " + x + ".")
中,您试图连接值并作为单个参数传递,因为连接操作不是有效操作(python 没有像 javascript 这样的强制在操作中转换不同的类型),它在到达打印功能之前就失败了。
将此操作视为“print
函数中的+
”是错误的心智模型。这里有两个完全独立的操作:
print
它知道它需要派生一个字符串来显示它,
+
对其结果的后续用法一无所知。
Python 中的函数(以及扩展方法和运算符)不是 return 类型的多态 ,这是一种奇特的说法,函数不是知道他们期望什么输出类型。仅仅因为 +
用于派生 print 的参数并不能告诉它字符串是所需的结果。
操作:str + :int
没有明确定义的输出类型。什么是 "1" + 1
?是 2
、"11"
、11
还是其他?这两种类型都没有明确的答案,因此 Python 因此一直提出 TypeError
。 结果是否最终在print
中使用无关紧要。
相比之下,print
用于将其参数的 string-representation 写入流。每个参数最终都必须生成一个字符串并写入流中。操作 print(:str, :int)
不需要连接 str
和 int
- 它可以将每个单独转换为 str
并立即写入。
print ("hello"+"world)
或
print ("hello"
+"world")
将输出为:
hello world
但是,当我尝试使用“+”将变量 (int) 与 'str' 一起插入打印函数时,出现错误。但是,当我改用 ',' 时,它可以正常工作。即:
x=1
print("the value of 'x' is " + x + ".")
显示错误
TypeError: can only concatenate str (not "int") to str
但是当我使用时:
print("the value of 'x' is ",x,".")
我得到了想要的输出。
我想知道为什么python这样设计,在这种情况下使用'+'是什么意思。谢谢。
编辑:感谢您的回复,原意是因为 Java + 意味着简单地放在一起。
print()
只是一个函数,它的设计方式是接受可变数量的位置参数,并在内部将它们转换为 str.
如你所见here at it's documentation:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
. . .
All non-keyword arguments are converted to strings like str() does
and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
. . .
当您执行 print("the value of 'x' is ",x,".")
时,您将 3 个不同的参数传递给函数,函数得到的参数为 objects
。并如上所述将它们转换为字符串。
但是,在行 print("the value of 'x' is " + x + ".")
中,您试图连接值并作为单个参数传递,因为连接操作不是有效操作(python 没有像 javascript 这样的强制在操作中转换不同的类型),它在到达打印功能之前就失败了。
将此操作视为“print
函数中的+
”是错误的心智模型。这里有两个完全独立的操作:
print
它知道它需要派生一个字符串来显示它,+
对其结果的后续用法一无所知。
Python 中的函数(以及扩展方法和运算符)不是 return 类型的多态 ,这是一种奇特的说法,函数不是知道他们期望什么输出类型。仅仅因为 +
用于派生 print 的参数并不能告诉它字符串是所需的结果。
操作:str + :int
没有明确定义的输出类型。什么是 "1" + 1
?是 2
、"11"
、11
还是其他?这两种类型都没有明确的答案,因此 Python 因此一直提出 TypeError
。 结果是否最终在print
中使用无关紧要。
相比之下,print
用于将其参数的 string-representation 写入流。每个参数最终都必须生成一个字符串并写入流中。操作 print(:str, :int)
不需要连接 str
和 int
- 它可以将每个单独转换为 str
并立即写入。