我的带有 *args 的函数仅重制第一个参数 python,但我需要将它们全部重制

My function with *args remake only first argument python, but i need them all to be remaked

我需要编写一个装饰器,删除字符串开头和结尾的空格,这些空格就像另一个函数的参数一样。起初我试着只写一个使用 strip 的函数,但是当我需要它们时,它只重新制作第一个给定的 arg。 join 需要,因为没有它函数 returns 元组。

def NewFunc(*strings):
    newstr = ' '.join([str(x) for x in strings])
    return newstr.strip()

print(NewFunc('         Anti   ', '     hype   ', '   ajou!   '))

它returns:Anti hype ajou!

当我需要时:Anti hype ajou!

要改变什么?

strip 仅删除前导和尾随空格,您只是 stripping 最终结果。在 joining 之前,您必须 strip 每个元素,这可以在列表理解中完成:

def NewFunc(*strings):
    newstr = ' '.join([str(x).strip() for x in strings])
    return newstr

这个str(x)有点没必要,不过我不知道,也许你要传入int什么的。