Python 如何迭代函数参数

Python 如何迭代函数参数,python,arguments,Python,Arguments,我有一个Python函数,它接受几个字符串参数def foo(a,b,c):,并将它们连接到一个字符串中。 我想迭代所有函数参数,检查它们是否为无。如何做到这一点? 有没有快速的方法将None转换为“” 谢谢 def func(*args): ' '.join(i if i is not None else '' for i in args) 如果您是在空字符串上加入,您可以只执行''。join(如果i不是None,则在args中i代表i)我会使用sed s/None//g,但在pyt

我有一个Python函数,它接受几个字符串参数
def foo(a,b,c):
,并将它们连接到一个字符串中。 我想迭代所有函数参数,检查它们是否为无。如何做到这一点? 有没有快速的方法将None转换为“”

谢谢

def func(*args):
    ' '.join(i if i is not None else '' for i in args)

如果您是在空字符串上加入,您可以只执行
''。join(如果i不是None,则在args中i代表i)

我会使用sed s/None//g,但在python中不是这样,但您可能可以使用os.popen()来完成此操作。

如果您在函数中首先调用它,它可能是您的朋友

示例1

>>> def fun(a, b, c):
...     d = locals()
...     e = d
...     print e
...     print locals()
... 
>>> fun(1, 2, 3)
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'e': {...}, 'd': {...}}
>>> def nones(a, b, c, d):
...     arguments = locals()
...     print 'The following arguments are not None: ', ', '.join(k for k, v in arguments.items() if v is not None)
... 
>>> nones("Something", None, 'N', False)
The following arguments are not None:  a, c, d
>>> def foo(a, b, c):
...     return ''.join(v for v in locals().values() if v is not None)
... 
>>> foo('Cleese', 'Palin', None)
'CleesePalin'
示例2

>>> def fun(a, b, c):
...     d = locals()
...     e = d
...     print e
...     print locals()
... 
>>> fun(1, 2, 3)
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'e': {...}, 'd': {...}}
>>> def nones(a, b, c, d):
...     arguments = locals()
...     print 'The following arguments are not None: ', ', '.join(k for k, v in arguments.items() if v is not None)
... 
>>> nones("Something", None, 'N', False)
The following arguments are not None:  a, c, d
>>> def foo(a, b, c):
...     return ''.join(v for v in locals().values() if v is not None)
... 
>>> foo('Cleese', 'Palin', None)
'CleesePalin'
回答

>>> def fun(a, b, c):
...     d = locals()
...     e = d
...     print e
...     print locals()
... 
>>> fun(1, 2, 3)
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'e': {...}, 'd': {...}}
>>> def nones(a, b, c, d):
...     arguments = locals()
...     print 'The following arguments are not None: ', ', '.join(k for k, v in arguments.items() if v is not None)
... 
>>> nones("Something", None, 'N', False)
The following arguments are not None:  a, c, d
>>> def foo(a, b, c):
...     return ''.join(v for v in locals().values() if v is not None)
... 
>>> foo('Cleese', 'Palin', None)
'CleesePalin'
更新

>>> def fun(a, b, c):
...     d = locals()
...     e = d
...     print e
...     print locals()
... 
>>> fun(1, 2, 3)
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'e': {...}, 'd': {...}}
>>> def nones(a, b, c, d):
...     arguments = locals()
...     print 'The following arguments are not None: ', ', '.join(k for k, v in arguments.items() if v is not None)
... 
>>> nones("Something", None, 'N', False)
The following arguments are not None:  a, c, d
>>> def foo(a, b, c):
...     return ''.join(v for v in locals().values() if v is not None)
... 
>>> foo('Cleese', 'Palin', None)
'CleesePalin'
“示例1”强调,如果参数的顺序很重要,因为
locals()
(或
vars()
)返回的
dict
是无序的,那么我们可能需要做一些额外的工作。上面的函数也不能很好地处理数字。以下是一些改进:

>>> def foo(a, b, c):
...     arguments = locals()
...     return ''.join(str(arguments[k]) for k in sorted(arguments.keys()) if arguments[k] is not None)
... 
>>> foo(None, 'Antioch', 3)
'Antioch3'

您可以使用inspect模块并定义如下函数:

import inspect
def f(a,b,c):
    argspec=inspect.getargvalues(inspect.currentframe())
    return argspec
f(1,2,3)
ArgInfo(args=['a', 'b', 'c'], varargs=None, keywords=None, locals={'a': 1, 'c': 3, 'b': 2})
在argspec中,有执行任何传递参数的操作所需的所有信息

连接字符串足以使用接收到的参数信息:

def f(a,b,c):
    argspec=inspect.getargvalues(inspect.currentframe())
    return ''.join(argspec.locals[arg] for arg in argspec.args)
供参考:
这可能是你想要的吗

def foo(a, b, c):
    "SilentGhost suggested the join"
    ' '.join(i if i is not None else '' for i in vars().values())

def bar(a,b,c): 
    "A very usefull method!"
    print vars()
    print vars().values()

注意使用,它返回一个dict.

如果所有参数都显式声明了怎么办?就像def foo(a,b,c)中一样:@Jack:重新定义你的函数。这将是最简单的选择。如果你只有很少的参数,你可以把它们放进一个元组中。@Jack:你为什么这么认为?这是惯用的Python。你会单独使用你的论点吗?如果您可以轻松地将
args
解包为具有适当名称的变量。@杰克,我同意您的看法。您可以显式地命名参数,并在加入之前构建这些参数的列表(args=[a,b,c])。也许关键字构造也是一种解决方案;然后您可以从kwargs dict中获取值。关键字默认值可能会有意外行为。@extraneon我两天前开始使用Python,我不确定我是否理解您的意思。我认为
None
表示Python对象
None
而不是文本字符串
“None”
FWIW,我认为,
*args
是解决这个问题的方法。。。如果要连接16个字符串,该怎么办?我只是想说明,在Python中使用提供的函数签名是可能的:-)您也可以使用vars()。@extranon谢谢您的提示
locals()
在这种情况下的行为似乎与
vars()
相同,没有任何参数。发现这与将下划线分隔的字符串转换为camelcase字符串的函数一起非常有用。。。从init参数实例化对象属性时,可以节省大量繁琐的样板代码。我仍然在等待这种编程风格给自己带来麻烦:为了简化示例,将语句拆分成单独的行可能会有所帮助。(即:
对于排序中的k(arguments.keys()):
如果参数[k]不是无:
打印(str(arguments[k]))