optional-arguments

namedtuple and default values for optional keyword arguments

喜你入骨 提交于 2019-11-27 05:47:51
I'm trying to convert a longish hollow "data" class into a named tuple. My class currently looks like this: class Node(object): def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right After conversion to namedtuple it looks like: from collections import namedtuple Node = namedtuple('Node', 'val left right') But there is a problem here. My original class allowed me to pass in just a value and took care of the default by using default values for the named/keyword arguments. Something like: class BinaryTree(object): def __init__(self, val): self.root =

How do I test if optional arguments are supplied or not?

南笙酒味 提交于 2019-11-27 01:26:21
问题 How do I test if optional arguments are supplied or not? -- in 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 回答1: Use IsMissing : If IsMissing(arg) Then MsgBox "Parameter arg not passed" End If However, if I remember correctly, this doesn’t work when giving a default for the argument, and in any case it makes using the default argument rather redundant. 回答2: You can use

How do I create a Python function with optional arguments?

删除回忆录丶 提交于 2019-11-26 21:21:21
I have a Python function which takes several arguments. Some of these arguments could be omitted in some scenarios. def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None): #code The arguments d through h are strings which each have different meanings. It is important that I can choose which optional parameters to pass in any combination. For example, (a, b, C, d, e) , or (a, b, C, g, h) , or (a, b, C, d, e, f , or all of them (these are my choices). It would be great if I could overload the function - but I read that Python does not support overloading. I tried to

C# 4.0 optional out/ref arguments

ε祈祈猫儿з 提交于 2019-11-26 19:39:16
Does C# 4.0 allow optional out or ref arguments? As already mentioned, this is simply not allowed and I think it makes a very good sense. However, to add some more details, here is a quote from the C# 4.0 Specification , section 21.1: Formal parameters of constructors, methods, indexers and delegate types can be declared optional: fixed-parameter: attributes opt parameter-modifier opt type identifier default-argument opt default-argument: = expression A fixed-parameter with a default-argument is an optional parameter , whereas a fixed-parameter without a default-argument is a required

getopt does not parse optional arguments to parameters

落爺英雄遲暮 提交于 2019-11-26 17:36:58
问题 In C, getopt_long does not parse the optional arguments to command line parameters parameters. When I run the program, the optional argument is not recognized like the example run below. $ ./respond --praise John Kudos to John $ ./respond --blame John You suck ! $ ./respond --blame You suck ! Here is the test code. #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

Is there a way to use two &#39;…&#39; statements in a function in R?

不羁的心 提交于 2019-11-26 15:52:44
I want to write a function that calls both plot() and legend() and it would be ideal if the user could specify a number of additional arguments that are then passed through to either plot() or legend() . I know I can achieve this for one of the two functions using ... : foo.plot <- function(x,y,...) { plot(x,y,...) legend("bottomleft", "bar", pch=1) } foo.plot(1,1, xaxt = "n") This passes xaxt = "n" to plot. But is there a way for example to pass e.g. title = "legend" to the legend() call without prespecifying the arguments in the function header? Update from the accepted answer: I thought

namedtuple and default values for optional keyword arguments

人盡茶涼 提交于 2019-11-26 10:05:50
问题 I\'m trying to convert a longish hollow \"data\" class into a named tuple. My class currently looks like this: class Node(object): def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right After conversion to namedtuple it looks like: from collections import namedtuple Node = namedtuple(\'Node\', \'val left right\') But there is a problem here. My original class allowed me to pass in just a value and took care of the default by using default values for

How do I create a Python function with optional arguments?

此生再无相见时 提交于 2019-11-26 04:59:22
问题 I have a Python function which takes several arguments. Some of these arguments could be omitted in some scenarios. def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None): #code The arguments d through h are strings which each have different meanings. It is important that I can choose which optional parameters to pass in any combination. For example, (a, b, C, d, e) , or (a, b, C, g, h) , or (a, b, C, d, e, f , or all of them (these are my choices). It would be