我知道在Python列表推导中使用副作用并不是一种好的做法.但我无法理解为什么会发生以下情况:
In [66]: tmp = [1,2,3,4,5]; [tmp.remove(elem) for elem in tmp]
Out[66]: [None, None, None]
In [67]: tmp
Out[67]: [2, 4]
Run Code Online (Sandbox Code Playgroud)
无论这是否是一种好的做法,列表理解的内部是否应该做一些可预测的事情?如果以上是可预测的,有人可以解释为什么只remove发生了三次操作,为什么偶数条目仍然存在?
我正在尝试学习Haskell与学习你一个Haskell ...但我不耐烦,并希望实现我最喜欢的算法,看看我能不能.
我正在研究用于循环检测的乌龟/野兔算法(Floyd算法).
这是我到目前为止的代码:
idx :: (Eq a) => (a -> a) -> a -> a -> a
idx f tortoise hare
| (f tortoise) == (f (f hare)) = (f f hare)
| otherwise = (idx f) (f tortoise) (f f hare)
mu :: (Eq a) => (a -> a) -> a -> a -> Integer -> (Integer, a)
mu f tortoise hare cntr
| (f tortoise) == (f hare) = (cntr+1, f tortoise)
| …Run Code Online (Sandbox Code Playgroud) 我最近升级到 Postgres 9.5,只需从 Ubuntu 包 repos 安装即可。升级过程中一切顺利,我经常使用 Postgres 和我以前的所有数据。
但是,我也在研究一些 C 语言扩展,需要导入后端头文件“postgres.h”——在 Postgres 包含目录中找不到。
该文档(搜索“服务器的头文件”和阅读说明)认为,应该有一个基地在私人子目录包含目录和服务器端头将安装在那里,但是这不是我的情况。
使用find在整个机器上搜索“postgres.h”表明唯一的副本位于一个单独的目录中,我在其中下载了旧版本的 Postgres 源代码,只是为了阅读和搜索代码——根本不支持安装。
我想不惜一切代价避免重建或重新安装 Postgres,因为一切正常。我只需要使标题可用。
假设我创建了一个带有参数默认值的解析器,然后给它一个带有参数默认值的子解析器。
In [1]: parser = argparse.ArgumentParser(description='test')
In [2]: parser.add_argument("--test", dest="test", default="hello")
Out[2]: _StoreAction(option_strings=['--test'], dest='test', nargs=None, const=None, default='hello', type=None, choices=None, help=None, metavar=None)
In [3]: parser.get_default("test")
Out[3]: 'hello'
In [4]: subparsers = parser.add_subparsers(dest="command")
In [5]: parser_other = subparsers.add_parser("other")
In [6]: parser_other.add_argument("--other-test", dest="other_test", default="world")
Out[6]: _StoreAction(option_strings=['--other-test'], dest='other_test', nargs=None, const=None, default='world', type=None, choices=None, help=None, metavar=None)
In [7]: parser_other.get_default("other_test")
Out[7]: 'world'
Run Code Online (Sandbox Code Playgroud)
这一切都很好。但是假设我有一个函数parser从上面创建并返回父解析器,但不能直接访问子解析器。
我怎样才能打印出子解析器参数的默认值?或者分别获取每个子解析器的句柄?
In [8]: parser._subparsers._defaults
Out[8]: {}
In [9]: parser._subparsers.get_default("other_test") # is None
Run Code Online (Sandbox Code Playgroud)
似乎没有从任何更多的属性或方法,parser._subparsers或parser可能显示的默认值。
总体问题是:当您只有父解析器的句柄时,如何以编程方式访问子解析器默认值?
python ×2
argparse ×1
arguments ×1
haskell ×1
lambda ×1
parsing ×1
postgresql ×1
side-effects ×1
subparsers ×1
typeclass ×1