当前位置: 首页 > 知识库问答 >
问题:

当与形式(position)、*args和**kwargs一起使用时,显式传递命名(关键字)参数

子车俊哲
2023-03-14
#!/usr/bin/python

import sys
import os

from pprint import pprint as pp

def test_var_args(farg, default=1, *args, **kwargs):
    print "type of args is", type(args)
    print "type of args is", type(kwargs)

    print "formal arg:", farg
    print "default arg:", default

    for arg in args:
        print "another arg:", arg

    for key in kwargs:
        print "another keyword arg: %s: %s" % (key, kwargs[key])

    print "last argument from args:", args[-1]


test_var_args(1, "two", 3, 4, myarg2="two", myarg3=3)
type of args is <type 'tuple'>
type of args is <type 'dict'>
formal arg: 1
default arg: two
another arg: 3
another arg: 4
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
last argument from args: 4
type of args is <type 'tuple'>
type of args is <type 'dict'>
formal arg: 1
default arg: 1
another arg: two
another arg: 3
another arg: 4
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
last argument from args: 4
type of args is <type 'tuple'>
type of args is <type 'dict'>
formal arg: 1
default arg: two
another arg: 3
another arg: 4
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
last argument from args: 4

我尝试了以下内容,但它也返回编译错误:test_var_args(1,3,4,myarg2=“two”,myarg3=3)

这可能吗?

共有1个答案

郜彬
2023-03-14

不幸的是,我认为那是不可能的。

正如萨姆指出的,你可以通过从Kwargs中取出值来实现同样的行为。如果逻辑依赖于kwargs不包含“default”参数,则可以使用pop方法将其从kwargs字典中删除(参见此处)。以下代码的行为与您所需的类似:

import sys
import os

from pprint import pprint as pp

def test_var_args(farg, *args, **kwargs):
    print "type of args is", type(args)
    print "type of args is", type(kwargs)

    print "formal arg:", farg
    print 'default', kwargs.pop('default', 1)

    for arg in args:
        print "another arg:", arg

    for key in kwargs:
        print "another keyword arg: %s: %s" % (key, kwargs[key])

    print "last argument from args:", args[-1]

# Sample call
test_var_args(1, 3, 4, default="two", myarg2="two", myarg3=3)

它的工作原理与你在问题中想要的类似

 类似资料:
  • 问题 你有一个函数或方法,它使用*args和**kwargs作为参数,这样使得它比较通用, 但有时候你想检查传递进来的参数是不是某个你想要的类型。 解决方案 对任何涉及到操作函数调用签名的问题,你都应该使用 inspect 模块中的签名特性。 我们最主要关注两个类:Signature 和 Parameter 。下面是一个创建函数前面的交互例子: >>> from inspect import Si

  • 问题内容: 我经常发现自己会覆盖父类的方法,并且永远无法决定是应该显式列出给定的参数还是仅使用通用结构。一个版本比另一个版本好吗?有最佳做法吗?我缺少什么(缺点)? 显式变体的感知好处 更明确(Python的禅宗) 更容易掌握 功能参数易于访问 毯子变体的感知优势 更干 父类很容易互换 无需更改其他代码即可传播父方法中默认值的更改 问题答案: 里斯科夫替代原则 通常,您不希望方法签名在派生类型中有

  • 本文向大家介绍Python函数中*args和**kwargs来传递变长参数的用法,包括了Python函数中*args和**kwargs来传递变长参数的用法的使用技巧和注意事项,需要的朋友参考一下 单星号形式(*args)用来传递非命名键可变参数列表。双星号形式(**kwargs)用来传递键值可变参数列表。 下面的例子,传递了一个固定位置参数和两个变长参数。 结果如下: 这个例子用来展示键值对形式的

  • 问题内容: 这是我尝试在其上使用该函数的循环: 有办法吗?如果不是参数,那将是微不足道的,但是我不确定如何处理。 问题答案: 用途: 创建一个新的可调用对象,除了传递给该新的可调用对象的内容之外,该参数还将对包装的函数应用所有参数(包括关键字参数)。

  • 问题内容: 在 Python的2.x的 (我用2.7),这是使用默认参数用正确的方式和? 我已经找到了与此主题相关的SO问题,但这是针对 Python 3的 : 使用 args,* kwargs和可选的/默认参数调用Python函数 在那里,他们说此方法有效: 在2.7中,结果为。有没有推荐的方法来定义这种功能? 我以这种方式工作,但我猜有更好的解决方案。 问题答案: 只需将默认参数放在: 现在,

  •   *args 和 **kwargs 都允许将可变数量的参数传递给函数。当不确定要在函数中传递的参数数量时,将使用它们。   *args 允许你将可变数量的参数传递给函数。 def addNumbers(*numbers):     sum = 0     for number in numbers:         sum = sum + number     print("Sum: "