用python的方式传递有条件的关键字参数。

时间:2022-06-21 22:32:08

Is there a more pythonic way to do this?

有没有更符合python语言的方法呢?

if authenticate:
    connect(username="foo")
else:
    connect(username="foo", password="bar", otherarg="zed")

4 个解决方案

#1


16  

  1. You could add them to a list of kwargs like this:

    你可以把它们加到kwargs的列表中:

    connect_kwargs = dict(username="foo")
    if authenticate:
       connect_kwargs['password'] = "bar"
       connect_kwargs['otherarg'] = "zed"
    connect(**connect_kwargs)
    

    This can sometimes be helpful when you have a complicated set of options that can be passed to a function. In this simple case, I think what you have is better, but this could be considered more pythonic because it doesn't repeat username="foo" twice like the OP.

    当您有一组复杂的选项可以传递给函数时,这有时是很有用的。在这个简单的例子中,我认为你有更好的东西,但这可以被认为是更python化的,因为它不会重复用户名="foo"两次像OP。

  2. This alternative approach can also be used, although it only works if you know what the default arguments are. I also wouldn't consider it to be very "pythonic" because of the duplicated if clauses.

    这种替代方法也可以使用,尽管它只在您知道默认参数是什么时才有效。我也不会认为它是非常“python化”的,因为if子句是重复的。

    password = "bar" if authenticate else None
    otherarg = "zed" if authenticate else None
    connect(username="foo", password=password, otherarg=otherarg)
    

#2


2  

The OP's version is actually ok in this case where the number of unconditional arguments is low compared to the number of conditional ones. This is because only the unconditional arguments have to be repeated in both branches of the if-else construct. However, I often run into the opposite case, i.e. the number of unconditional arguments is high compared to that of the conditional ones.

在这种情况下,与条件参数的数量相比,无条件参数的数量较低,OP的版本实际上是可以的。这是因为只有无条件的参数必须在if-else结构的两个分支中重复。然而,我经常遇到相反的情况,即无条件参数的数量比条件参数的数量多。

This is what I use:

这就是我使用的:

connect( username="foo", 
         **( dict( password="bar", otherarg="zed") if authenticate else {} ) )

#3


0  

Just thought I'd throw my hat in the ring:

我只是想把我的帽子扔进戒指:

authenticate_kwargs = {'password': "bar", 'otherarg': "zed"} if authenticate else {}
connect(username="foo", **authenticate_kwargs)

#4


-2  

Or, more concisely:

或者,更简洁:

connect(**(
    {'username': 'foo', 'password': 'bar', 'otherarg': 'zed'}
    if authenticate else {'username': 'foo'}
))

#1


16  

  1. You could add them to a list of kwargs like this:

    你可以把它们加到kwargs的列表中:

    connect_kwargs = dict(username="foo")
    if authenticate:
       connect_kwargs['password'] = "bar"
       connect_kwargs['otherarg'] = "zed"
    connect(**connect_kwargs)
    

    This can sometimes be helpful when you have a complicated set of options that can be passed to a function. In this simple case, I think what you have is better, but this could be considered more pythonic because it doesn't repeat username="foo" twice like the OP.

    当您有一组复杂的选项可以传递给函数时,这有时是很有用的。在这个简单的例子中,我认为你有更好的东西,但这可以被认为是更python化的,因为它不会重复用户名="foo"两次像OP。

  2. This alternative approach can also be used, although it only works if you know what the default arguments are. I also wouldn't consider it to be very "pythonic" because of the duplicated if clauses.

    这种替代方法也可以使用,尽管它只在您知道默认参数是什么时才有效。我也不会认为它是非常“python化”的,因为if子句是重复的。

    password = "bar" if authenticate else None
    otherarg = "zed" if authenticate else None
    connect(username="foo", password=password, otherarg=otherarg)
    

#2


2  

The OP's version is actually ok in this case where the number of unconditional arguments is low compared to the number of conditional ones. This is because only the unconditional arguments have to be repeated in both branches of the if-else construct. However, I often run into the opposite case, i.e. the number of unconditional arguments is high compared to that of the conditional ones.

在这种情况下,与条件参数的数量相比,无条件参数的数量较低,OP的版本实际上是可以的。这是因为只有无条件的参数必须在if-else结构的两个分支中重复。然而,我经常遇到相反的情况,即无条件参数的数量比条件参数的数量多。

This is what I use:

这就是我使用的:

connect( username="foo", 
         **( dict( password="bar", otherarg="zed") if authenticate else {} ) )

#3


0  

Just thought I'd throw my hat in the ring:

我只是想把我的帽子扔进戒指:

authenticate_kwargs = {'password': "bar", 'otherarg': "zed"} if authenticate else {}
connect(username="foo", **authenticate_kwargs)

#4


-2  

Or, more concisely:

或者,更简洁:

connect(**(
    {'username': 'foo', 'password': 'bar', 'otherarg': 'zed'}
    if authenticate else {'username': 'foo'}
))