我正在尝试将一个冗长的空洞"数据"类转换为一个命名元组.我的班级目前看起来像这样:
class Node(object):
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
Run Code Online (Sandbox Code Playgroud)
转换为namedtuple它后看起来像:
from collections import namedtuple
Node = namedtuple('Node', 'val left right')
Run Code Online (Sandbox Code Playgroud)
但这里有一个问题.我的原始类允许我传入一个值,并使用命名/关键字参数的默认值来处理默认值.就像是:
class BinaryTree(object):
def __init__(self, val):
self.root = Node(val)
Run Code Online (Sandbox Code Playgroud)
但是这对我重构的名为元组的情况不起作用,因为它希望我传递所有字段.当然,我可以代替的出现Node(val)到Node(val, None, None),但它不是我的胃口.
所以确实存在着一个很好的技巧,它可以使我重新写成功无需添加大量的代码复杂度(元编程),或者我应该只吞下药丸,并与"查找和替换"继续前进?:)
C#4.0是否允许可选out或ref参数?
我有一个Python函数,它需要几个参数.在某些情况下,可以省略其中一些参数.
def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None):
#code
Run Code Online (Sandbox Code Playgroud)
这些参数d通过h是字符串,每个都有不同的含义.重要的是我可以选择以任意组合传递哪些可选参数.例如,(a, b, C, d, e)或(a, b, C, g, h),或(a, b, C, d, e, f,或全部(这些是我的选择).
如果我可以重载函数会很棒 - 但我读到Python不支持重载.我试图在列表中插入一些必需的int参数 - 并得到一个参数不匹配错误.
现在我发送空字符串代替前几个缺少的参数作为占位符.我希望能够使用实际值调用函数.
有没有办法做到这一点?我可以传递一个列表而不是参数列表吗?
现在使用ctypes的原型看起来像:
_fdll.some_function.argtypes = [c_void_p, c_char_p, c_int, c_char_p, c_char_p, c_char_p, c_char_p, c_char_p]
Run Code Online (Sandbox Code Playgroud) 如何在LaTeX中使用可选参数创建命令?就像是:
\newcommand{\sec}[2][]{
\section*{#1
\ifsecondargument
and #2
\fi}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以称之为
\sec{Hello}
%Output: Hello
\sec{Hello}{Hi}
%Output: Hello and Hi
Run Code Online (Sandbox Code Playgroud) 我把空括号作为Python函数的可选参数的默认值,而pylint(使用Sublime包)告诉我这很危险.有人可以解释为什么会这样吗?而使用None是更好的选择吗?
如何测试是否提供了可选参数? - 在VB6/VBA中
Function func (Optional ByRef arg As Variant = Nothing)
If arg Is Nothing Then <----- run-time error 424 "object required"
MsgBox "NOT SENT"
End If
End Function
Run Code Online (Sandbox Code Playgroud) 在C中,getopt_long不会解析命令行参数参数的可选参数.
当我运行程序时,无法识别可选参数,如下面的示例运行.
$ ./respond --praise John
Kudos to John
$ ./respond --blame John
You suck !
$ ./respond --blame
You suck !
Run Code Online (Sandbox Code Playgroud)
这是测试代码.
#include <stdio.h>
#include <getopt.h>
int main(int argc, char ** argv )
{
int getopt_ret, option_index;
static struct option long_options[] = {
{"praise", required_argument, 0, 'p'},
{"blame", optional_argument, 0, 'b'},
{0, 0, 0, 0} };
while (1) {
getopt_ret = getopt_long( argc, argv, "p:b::",
long_options, &option_index);
if (getopt_ret == -1) break;
switch(getopt_ret)
{
case 0: break;
case 'p': …Run Code Online (Sandbox Code Playgroud) 我想编写调用两种功能:plot()和legend(),这将是理想的,如果用户可以指定一些额外的参数,然后可通过要么通过plot()或legend().我知道我可以使用...以下两种函数之一实现此目的:
foo.plot <- function(x,y,...) {
plot(x,y,...)
legend("bottomleft", "bar", pch=1)
}
foo.plot(1,1, xaxt = "n")
Run Code Online (Sandbox Code Playgroud)
这传递xaxt = "n"给情节.但是有没有办法title = "legend"在legend()没有预先指定函数头中的参数的情况下将eg传递给调用?
从接受的答案中更新:我认为VitoshKa的方式是实现我想要的最优雅的方式.但是,我必须解决一些小问题,直到它按照我想要的方式工作.
首先,我检查了我要传递给legend哪个参数以及哪个参数plot.为此目的的第一步是查看哪些参数legend是独特的,legend而不是绘图和/或参数的一部分:
legend.args <- names(formals(legend))
plot.args <- c(names(formals(plot.default)), names(par()))
dput(legend.args[!(legend.args %in% plot.args)])
Run Code Online (Sandbox Code Playgroud)
我dput()在这里使用,因为该行plot.args <- c(names(formals(plot.default)), names(par()))总是调用一个我不想要的新空图.所以,我使用dput了以下函数的输出.
接下来,我不得不处理重叠的参数(通过它们获取dput(largs.all[(largs.all %in% pargs.all)])).对于一些这是微不足道的(例如x,y)等获得通过这两个功能(例如,pch).但是,在我的实际应用程序中,我甚至使用其他策略(例如,不同的变量名称adj,但在此示例中未实现).
最后, …
大多数网站都说"callee"作为Function.arguments的属性已被弃用.但有些网站更进一步,并说整个Functions.argument已被弃用,例如http://aptana.com/reference/api/Arguments.html为什么只提到被叫者,如果整个程序在水中已经死了?我只是发现了"论点",看起来非常有用,例如:http://hungred.com/how-to/secret-arguments-array-javascript/