Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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 是否可以从参数列表中删除项?_Python_Python 2.7 - Fatal编程技术网

Python 是否可以从参数列表中删除项?

Python 是否可以从参数列表中删除项?,python,python-2.7,Python,Python 2.7,如果*args的长度(len(args))大于2,我需要删除它的前两个参数。有可能吗 以下是我需要这样做的原因: 我有以下功能: def foo(name, *args, **kwargs): 我需要在不破坏向后兼容性的情况下,向这个函数的参数中添加一个可选的布尔参数。因此,我将函数更改为: def foo(name, xml='my.xml', xsd='my.xsd', *otherfiles): print('name: ' + name) print('xml: ' +

如果
*args
的长度(
len(args)
)大于2,我需要删除它的前两个参数。有可能吗

以下是我需要这样做的原因: 我有以下功能:

def foo(name, *args, **kwargs):
我需要在不破坏向后兼容性的情况下,向这个函数的参数中添加一个可选的布尔参数。因此,我将函数更改为:

def foo(name, xml='my.xml', xsd='my.xsd', *otherfiles):
    print('name: ' + name)
    print('xml: ' + xml)
    print('xsd: ' + xsd)
    print(otherfiles)
现在,我通过检查
foo
foo2
的输出是否相同来测试向后兼容性:

def foo2(name, *otherfiles, **kwargs):
    kwargs.setdefault('relativePath', True)
    if len(otherfiles)>0:
        xml = otherfiles[0]
    else:
        kwargs.setdefault('xml', 'my.xml')
        xml = kwargs['xml']

    if len(otherfiles)>1:
        xsd = otherfiles[1]
    else:
        kwargs.setdefault('xsd', 'my.xsd')
        xsd = kwargs['xsd']
    print('name: ' + name)
    print('xml: ' + xml)
    print('xsd: ' + xsd)
    print(otherfiles)
输出:

foo('my_name', '../my.xml', '../my.xsd', 'file1', 'file2')
print('===============================================================')
foo2('my_name', '../my.xml', '../my.xsd', 'file1', 'file2')

如您所见,应删除
其他文件
的前两项以维护旧功能。

我们无法从
参数
中删除项,因为它是
元组
作为
元组
是不可变的

但是我们可以根据我们的逻辑创建包含
args
项的新变量。我们可以在下一个过程中使用新变量

演示1:

name: my_name  
xml: ../my.xml  
xsd: ../my.xsd  
('file1', 'file2')  
===============================================================  
name: my_name  
xml: ../my.xml  
xsd: ../my.xsd  
('../my.xml', '../my.xsd', 'file1', 'file2')

以下内容有助于在函数调用之前更改命名的参数列表

添加一个参数:

def foo(name, *args, **kwargs):
    print "args: ", args
    print "Type of args: ", type(args)
    if len(args)>2:
        args = args[0], args[1]     #- Created Same name variable.
    print "Temp args:", args
删除元素:

# The wrapper adds a named argument 'age' to the function call
def decorator(func):
    def wrapper(*args, **kwargs):
        print("before")
        # Add an argument to the existing list
        kwargs.update(age=4)
        func(*args, **kwargs)
        print("after")
    return wrapper

# Decorator replaces myFunc by a function which adds/changes the
# age argument before calling original myFunc
@decorator 
def myFunc(name, age):
    print(f"Hi {name} ! Are you {age} ?")
    

myFunc("Roger")
myFunc(name="Marc", age=33)

它没有严格地回答问题,但它与标题相符,并且有人在对已接受答案的评论中询问了kwargs。

您真的想删除它们吗?为什么不跳过它们呢?您想如何使用
args
?@很快更新了问题以解释我的问题。和kwargs?那他们呢?
# The wrapper adds a named argument 'age' to the function call
def decorator(func):
    def wrapper(*args, **kwargs):
        print("before")
        # Add an argument to the existing list
        kwargs.update(age=4)
        func(*args, **kwargs)
        print("after")
    return wrapper

# Decorator replaces myFunc by a function which adds/changes the
# age argument before calling original myFunc
@decorator 
def myFunc(name, age):
    print(f"Hi {name} ! Are you {age} ?")
    

myFunc("Roger")
myFunc(name="Marc", age=33)
# The wrapper removes the inappropriate arguments from kwargs
def decorator(func):
    def wrapper(*args, **kwargs):
        print("before")
        # Keep only expected names
        # kwargs.pop("argname") works if we know the name to remove
        new_kwargs = {k: v for k, v in kwargs.items() if k in ["name"]}
        func(*args, **new_kwargs)
        print("after")
    return wrapper

# Decorator replaces myFunc by a function which filters
# argument(s) before calling original myFunc
@decorator def myFunc(name):
    print(f"Hi {name} !")

    
myFunc(tutu=40, name="Roger", toto="eeee")