当某些输入已命名而其他输入未命名时,如何在 python 中使用(或打开)args?

How to use (or open) args in python when some inputs are named and others are not?

我想使用 create_superuser 方法让一些用户。我目前正在这样做:

CustomUser.objects.create_superuser(
    'amin@gmail.com',
    'password',
    first_name='Amin',
    )

但我想创建多个用户。我不想给first_name,我会这样做:

a=[['user1@gmail.com', 'pass'], ['user2@gmail.com', 'pass'],..., ['user100@gmail.com', 'pass'], ]
[CustomUser.objects.create_superuser(*user) for user in a]

但在我的例子中,我有第三个输入,我需要为其输入名称 'first_name',我该如何做我在上面所做的事情?

仅使用关键字参数:

users = [
    dict(
        email='user1@gmail.com',
        password='password',
    ),
    dict(
        email='amin@gmail.com',
        password='pass',
        first_name='Amin',
    ),
]

for user in users:
    CustomUser.objects.create_superuser(**user)

如果您真的必须在循环中混合位置参数和关键字参数:

users = [
    (
        ('user1@gmail.com', 'password'),
        dict(),
    ),
    (
        ('amin@gmail.com', 'pass'),
        dict(first_name='Amin'),
    ),
]

for args, kwargs in users:
    CustomUser.objects.create_superuser(*args, **kwargs)

我个人认为这看起来很糟糕。