- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在尝试编写一个接受多个参数的函数,例如 a,b,c
,其中一些可能是长度为 X 的列表,而另一些可能是单个值,但这事先是未知的。
然后该函数应创建参数集 (a[i],b[i],c[i])
,这些参数集将被传递到多处理池。如果给定一个变量的单个值,它只会对每个参数集重复。
现在,如果我想遍历每个变量,我可以为每个变量写一个 if 语句:
def foo(a, b, c):
if type(a) == int:
# make list of length needed
# Repeat for each variable
但在实践中,这很快就会变得困惑和冗长。我想知道是否有更简单的方法来遍历输入变量。
我希望有类似的东西
def foo(a, b, c):
for variable in foo.__variables__:
if type(variable) == int:
# Make list of length needed
编辑:附加信息
正如 Lauro Bravar 所指出的,可以通过将参数作为列表传递来解决上述问题。不幸的是,我试图解决的全部问题包括存在多个可选变量,所以这不起作用。所以我正在寻找更多的代码来解决这种情况:
def foo(a, b, c, d=None, e=None):
for variable in foo.__variables__:
if variable is not None:
if type(variable) == int:
# Make list of length needed
有没有办法不使用**kwarg
来做到这一点?理想情况下,我希望所有参数都在定义中可见,因为可读性很重要(这将被没有编码经验的学生使用)
最佳答案
你可以通过*args
传递你的变量
def foo(*args):
for item in args:
print(type(item))
foo([3,4,5],4,"foo")
输出:
<type 'list'>
<type 'int'>
<type 'str'>
关于您在问题中的附加信息:*args
占用了您希望的任意多个参数。当遍历 *args
的所有元素并检查它们的类型时,您可以将结果存储在 dictionary
或 list
中以使其可访问:
def foo(*args):
mylist = list()
for item in args:
if type(item) == int:
mylist.append([item])
elif type(item) == list:
mylist.append(item)
return mylist
result = foo([3,4,5],4,"foo")
输出:
[[3, 4, 5], [4]]
在评论部分你添加了两个条件:
help()
函数需要返回foo
的参数*args
,因为您要创建指定参数的参数集(不支持开放式传递方案*args
)所以我的新方法处理内置函数 locals() ,返回“当前本地符号表”。当在函数内调用 locals()
时,它将返回函数的所有参数及其作为字典的值。确保在一开始就调用它,因为在运行时可能会在函数中创建新的局部变量,并且您可能会陷入循环。
这个怎么样?
def foo(a, b, c, d=None, e=None):
foo_arguments = locals()
for variable in foo_arguments:
if foo_arguments[variable] is not None:
if type(foo_arguments[variable]) == int:
print("Detected argument named {} which is an integer of value {:d}"
.format(variable, foo_arguments[variable]))
result = foo([3,4,5], 4, "foo", d=10)
这允许您为 a
、b
、c
和可选的 d
、e
。与
print(help(foo))
返回
Detected argument named b which is an integer of value 4
Detected argument named d which is an integer of value 10
Help on function foo in module __main__:
foo(a, b, c, d=None, e=None)
关于python - 有没有办法遍历给函数的每个变量并确定其类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54177656/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
在现代 IDE 中,有一个键盘快捷键可以通过键入文件名称来打开文件,而无需将手放在鼠标上。例如: Eclipse:Cmd|Ctrl + Shift + R -> 打开资源 IntelliJ:Cmd|C
有什么东西会等待事件发生(我正在等待的是 WebBrowser.DocumentCompleted),然后执行代码吗?像这样: If (WebBrowser.DocumentCompleted) 不会
我使用 PHP Minify,它很棒。但我的问题是,是否有任何 PHP 插件或其他东西可以自动检测 javascript/css 代码并自动缩小它?谢谢。 最佳答案 Javascript 压缩器? 看
有没有一种语言,类似什么CoffeeScript是JavaScript,编译成windows batch|cmd|command line的语言? 我指的cmd版本是基于NT的,尤其是XP sp3及以
我知道我可以 ,但是,我真的宁愿有一个任务,我可以从任何可以使用所有(或至少大部分)属性的操作系统调用 copy ,但这并没有消除 unix 上的权限。 我想知道是否已经有解决方案,或者我必须自己编
我正在使用 Vuejs(不使用 jQuery)开发一个项目,该项目需要像 jvectormap 这样的 map 但正如我所说,我没有使用 jQuery,那么是否有任何其他库可以在不使用 jQuery
想要进行一个简单的民意调查,甚至不需要基于 cookie,我不在乎投了多少票。有没有类似的插件或者简单的东西? 最佳答案 这是一个有用的教程 - 让我知道它是否适合您 using jQuery to
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
var FileBuff: TBytes; Pattern: TBytes; begin FileBuff := filetobytes(filename); Result := Co
我想要一个 vqmod xml 文件来添加一次上传多个图像的功能。身边有这样的事吗? 编辑:Opencart版本:2.1.0.1 最佳答案 最后我写了一个xml来添加到opencart 2.1.0.1
所以考虑这样的函数: public void setTemperature(double newTemperatureValue, TemperatureUnit unit) 其中Temperatur
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我是 ggplot2 的新手,一直在尝试找到一个全面的美学列表。我想我理解它们的目的,但很难知道哪些可以在各种情况下使用(主要是几何图形?)。 Hadley 的网站偶尔会在各个几何图形的页面上列出可用
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
是否有任何 PHP 函数可以将整数转换为十万和千万? 900800 -> 9,00,800 500800 -> 5,00,800 最佳答案 由于您已在问题标签中添加了 Yii,因此您可以按照 Yii
使用 Clojure 一段时间后,我积累了一些关于它的惰性的知识。我知道诸如map之类的常用API是否是惰性的。然而,当我开始使用一个不熟悉的API(例如with-open)时,我仍然感到怀疑。 是否
我的项目需要一个像 AvalonDock 这样的对接系统,但它的最后一次更新似乎是在 2013 年 6 月。是否有更多...积极开发的东西可以代替它? 最佳答案 AvalonDock 实际上相当成熟并
我正在寻找一个可以逆转 clojure 打嗝的函数 所以 turns into [:html] 等等 根据@kotarak的回答,这现在对我有用: (use 'net.cgrand.enliv
我是一名优秀的程序员,十分优秀!