Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在一个函数中使用*arg和可选参数_Python_Arguments - Fatal编程技术网

Python 在一个函数中使用*arg和可选参数

Python 在一个函数中使用*arg和可选参数,python,arguments,Python,Arguments,如何将*arg和带有defall值的变量一起使用 def my_fun(a, b = True, *arg): print (a) print (b) print (arg) my_fun(1,2,3) 我想要的是 1 True (2,3,) 我得到的是 1 2 (3) 如果捕获关键字参数之前的所有参数: >>> def my_fun(*arg, a=1, b=2): ... print (a) ... print (b) ...

如何将*arg和带有defall值的变量一起使用

def my_fun(a, b = True, *arg):
    print (a)
    print (b)
    print (arg)

my_fun(1,2,3)
我想要的是

1
True
(2,3,)
我得到的是

1
2
(3)

如果捕获关键字参数之前的所有参数:

>>> def my_fun(*arg, a=1, b=2):
...     print (a)
...     print (b)
...     print (arg)
...
>>> my_fun(3,4,5)
1
2
(3, 4, 5)
a
b
可以通过将它们作为命名参数传递来修改:

>>> my_fun(3,4,5, a=10, b=11)
10
11
(3, 4, 5)

你所要求的没有意义


如果我遵循您的逻辑,无论您如何调用函数,
a
b
始终是
1
2
,我犯了一个问题,我想保留更改a和b变量的能力,这对我来说是清楚的。显示一个调用函数的示例,其中
a
不是
1
。更改了问题,请检查开头的错误。我想把可选参数放在“a”和“*arg”之间。但是我不明白如何让python定义哪个值属于*arg,哪个值属于可选变量这就是我要告诉你的。你不能。