这实际上是我一年前开始学习Python以来的一个问题.它是以下内容:如何调用该 input
函数并让用户在除结尾之外的某个位置键入其条目prompt
?
澄清一下,说我有字符串
'Enter file size: MB'
Run Code Online (Sandbox Code Playgroud)
当调用时,我希望文本插入如下:
Enter file size: 62 MB
Run Code Online (Sandbox Code Playgroud)
是否可以在函数调用中重新创建此行为,如下所示?
input('Enter file size: {} MB')
# where Python would interpret
# the {} as the position to input text
Run Code Online (Sandbox Code Playgroud) 我一直在深入研究Python类中的运算符重载和特殊方法,并且我注意到许多内置函数具有等效的特殊方法名称:
int(x)
电话 x.__int__()
next(x)
调用x.__next__()
或x.next()
Python 2但是,有几个功能,即tuple()
和dict()
,没有任何等价物.我知道对于这种特殊方法还没有出现这种需要,但在某些情况下,dict()
在类上调用转换的方法可能是有用的.我该如何实现呢?或者,对于试图使用这种逻辑的人,您会怎么说?
# I think this is quite interesting, so I shall post my own implementation of it as well
Run Code Online (Sandbox Code Playgroud)