I come across the below in some code from an ex employee.
the code is not called, from anywhere, but my question is can it actually do something useful as it is?
def xshow(x):
print("{[[[[]}".format(x))
答案 0 :(得分:4)
这是一个格式字符串,其中包含一个空参数名称和一个元素索引([
和]
之间的部分,用于键[[[
(这些索引不需要是整数)。它将打印该键的值。
通话:
xshow({'[[[': 1})
将打印1
答案 1 :(得分:0)
可以使用交互式翻译来实验性地研究这样的事情。
>>> xshow(None)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
xshow(None)
File "<pyshell#11>", line 1, in xshow
def xshow(x): print("{[[[[]}".format(x))
TypeError: 'NoneType' object is not subscriptable
# So let us try something subscriptable.
>>> xshow([])
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
xshow([])
File "<pyshell#11>", line 1, in xshow
def xshow(x): print("{[[[[]}".format(x))
TypeError: list indices must be integers or slices, not str
# That did not work, try something else.
>>> xshow({})
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
xshow({})
File "<pyshell#11>", line 1, in xshow
def xshow(x): print("{[[[[]}".format(x))
KeyError: '[[['
# Aha! Try a dict with key '[[['.
>>> xshow({'[[[':1})
1
现在可以去阅读文档。