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
273 views
in Technique[技术] by (71.8m points)

类中的函数如何定义和调用任意数量的关键字实参?

image.pngimage.png


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

1 Reply

0 votes
by (71.8m points)

你原本中的写法:
info 是 tuple,
sex='man' 写法被解释成字典。

通常写法:

def fun(a, b, *args, **kwargs):
    print('a: "{}" value: {}'.format(type(a), a))
    print('b: "{}" value: {}'.format(type(b), b))
    print('args: "{}" value: {}'.format(type(args), args))
    print('kwargs: "{}" value: {}'.format(type(kwargs), kwargs))

fun(1, 'string', 'more', 9, name='hehe', sex='man')

把上面这段代码拷贝进去运行看看。


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

...