小编N C*_*han的帖子

input()调用在字符串中的自定义位置键入文本的位置

这实际上是我一年前开始学习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 input python-3.x

6
推荐指数
1
解决办法
72
查看次数

用于字典转换的特殊方法名称的内置函数

我一直在深入研究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)

python oop class function python-3.x

3
推荐指数
1
解决办法
55
查看次数

标签 统计

python ×2

python-3.x ×2

class ×1

function ×1

input ×1

oop ×1