tha*_*nce -21 python variable-assignment
在Python中,似乎我可以执行以下3个变量赋值中的任何一个:
g = (3, 4, 5)
g = "(3, 4, 5)"
g = 3, 4, 5
Run Code Online (Sandbox Code Playgroud)
其次是
print(g)
Run Code Online (Sandbox Code Playgroud)
并且输出总是如此
(3, 4, 5)
Run Code Online (Sandbox Code Playgroud)
那么,这三种变量赋值之间的区别是什么?
Łuk*_*ski 12
小代码片段足以证明它:
g1 = (3, 4, 5)
g2 = "(3, 4, 5)"
g3 = 3, 4, 5
type(g1) # <type 'tuple'>
type(g2) # <type 'str'>
type(g3) # <type 'tuple'>
g1 == g3 # True
g1 == g2 # False
g2 == g3 # False
g1[0] # 3, first element of tuple, type: int
g2[0] # "(", first char of string, type: str
Run Code Online (Sandbox Code Playgroud)
总而言之,对象和对象属性的字符串表示是两个不同的概念.可能有多个对象具有相同的字符串表示但行为不同.