Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

python如何将一个函数接收到的不定长函数传给另一个函数?

已知Python的rich https://github.com/willmcgugan/rich库可以在终端输出彩色的字体,所以想将现有代码里print的地方,替换为rich库的print进行打印,故封装了一个打印函数如下:

from rich import print as rprint
def color_print(msg):
    style = 'bold green'
    rprint('[' + style + ']' + msg + '[/ + style + ']'

然后觉得只要将自己代码中原来的print替换为color_print即可。

但是发现不行,因为原来代码中调用print的地方,有两种方式:

第一种:
print(msg)

第二种,作用是输出后不换行
print(msg, end='')

对于第二种方式的调用,替换为color_print会报,因为参数个数不一致。
请问Python是否有什么办法可以把不固定个数的参数,从一个函数转到另一个函数?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

def abc(a, *b){} b就是不定长的参数,其效果是一个tuple
def abc(a, **b){} b就是关键字的参数,其效果是一个map


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...