python - 我的带有 *args 的函数仅重制第一个参数 python,但我需要全部重制
问题描述
我需要编写一个装饰器,它从字符串的开头和结尾删除空格,这些空格就像另一个函数的参数一样给出。起初我试图只写一个使用 的函数strip
,但它只在我需要它们时重新制作第一个给定的 arg。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 最终结果。您必须strip
在每个元素之前join
对它们进行 ing,这可以在列表理解中完成:
def NewFunc(*strings):
newstr = ' '.join([str(x).strip() for x in strings])
return newstr
thestr(x)
有点不必要,但我不知道,也许你会传入int
s 什么的。
推荐阅读
- azure - 门户中缺少 Azure 功能
- android - Libgdx Stage.setActionsRequestRendering(true) 未按预期工作
- django - Django/PostgreSQL manage.py 刷新错误。查看“django-admin sqlflush”的输出
- java - 如何为二叉搜索树中的每个节点设置位置?
- javascript - Vue.js 不会绑定到未预定义的对象属性
- c++ - “模板
“什么=是什么意思? - android - 安卓黑屏原因
- spring - Spring Boot 2 和 Tomcat 7
- scala - Scala Spark IntelliJ Idea 开发流程
- c++ - cudaMallocManaged with vector
> C++ - 英伟达 CUDA