我需要编写一个decorator,它删除strig开头和结尾的空格,这些空格就像另一个函数的参数一样给出。起初,我试图只编写一个使用strip
的函数,但当我需要所有参数时,它只重新生成第一个给定的参数join
需要,因为如果没有它,函数将返回元组
def NewFunc(*strings):
newstr = ' '.join([str(x) for x in strings])
return newstr.strip()
print(NewFunc(' Anti ', ' hype ', ' ajou! '))
它返回:Anti hype ajou!
当我需要时:Anti hype ajou!
改变什么
strip
只删除前导和尾随空格,您只strip
ping最终结果。在对每个元素进行join
运算之前,必须对其进行strip
运算,这可以在列表中完成:这个
str(x)
有点不必要,但我不知道,也许你会通过int
什么的相关问题 更多 >
编程相关推荐