3

可以说我有这个代码:

def dosomething(thing1, thing2=hello, thing3=world):
    print thing1
    print thing2
    print thing3

我希望能够指定thing3 是什么,但不必说thing2 是什么。(下面的代码是我认为它可能工作的方式......)

dosomething("This says 'hello fail!'", , 'fail!')

它会说

This says 'hello fail!'
hello
fail!

那么有没有办法做到这一点,或者thing2每次我想说什么时我都必须指定thing3

我正在使用 python2,如果这很重要。

4

2 回答 2

7

使用关键字参数

dosomething("This says 'hello fail!'", thing3='fail!')
于 2012-08-12T16:10:22.467 回答
3

是的你可以:

dosomething("This says 'hello fail!'", thing3 = 'fail!')
于 2012-08-12T16:10:52.203 回答