optional-arguments

Python optional function argument to default to another argument's value

假装没事ソ 提交于 2019-12-24 12:02:07
问题 I want to define a function with some optional arguments, let's say A (mandatory) and B (optional). When B is not given, I want it to take the same value as A. How could I do that? I have tried this, but it doesn't work (name 'B' is not defined): def foo(A, B=A): do_something() I understand that the values of the arguments are not assigned before the body of the function. 回答1: You shall do this inside of your function. Taking your original function: def foo(A, B=A): do_something() try

Python optional, positional and keyword arguments

旧街凉风 提交于 2019-12-24 07:38:00
问题 This is a class I have: class metadict(dict): def __init__(self, do_something=False, *args, **kwargs) if do_something: pass super(metadict,self).__init__(*args,**kwargs) The idea is to encapsulate a dictionary and add some functionality with a special keyword. The dictionary can still hold do_something though you can't add it at creation time. For all other aspects it behaves just like a normal dictionary. Anyway, the problem is that whatever I give to args it starts by assigning the first

argparse doesn't check for positional arguments

半城伤御伤魂 提交于 2019-12-22 11:33:47
问题 I'm creating a script that takes both positional and optional arguments with argparse. I have gone through Doug's tutorial and the python Docs but can't find an answer. parser = argparse.ArgumentParser(description='script to run') parser.add_argument('inputFile', nargs='?', type=argparse.FileType('rt'), parser.add_argument('inputString', action='store', nargs='?') parser.add_argument('-option1', metavar='percent', type=float, action='store') parser.add_argument('-option2', metavar='outFile1',

Multiple optional arguments python

走远了吗. 提交于 2019-12-22 05:24:16
问题 So I have a function with several optional arguments like so: def func1(arg1, arg2, optarg1=None, optarg2=None, optarg3=None): Optarg1 & optarg2 are usually used together and if these 2 args are specified then optarg3 is not used. By contrast, if optarg3 is specified then optarg1 & optarg2 are not used. If it were one optional argument it'd be easy for the function to "know" which argument to use: if optarg1 != None: do something else: do something else My question is how to I "tell" the

How to pass optional arguments to a method in C++?

烂漫一生 提交于 2019-12-18 10:14:31
问题 How to pass optional arguments to a method in C++ ? Any code snippet... 回答1: Here is an example of passing mode as optional parameter void myfunc(int blah, int mode = 0) { if (mode == 0) do_something(); else do_something_else(); } you can call myfunc in both ways and both are valid myfunc(10); // Mode will be set to default 0 myfunc(10, 1); // Mode will be set to 1 回答2: An important rule with respect to default parameter usage: Default parameters should be specified at right most end, once

Can we avoid creating a local variable if an optional argument is not PRESENT?

人走茶凉 提交于 2019-12-17 17:02:54
问题 I am having a problem with the PRESENT statement with Fortran 95. Currently I am using Silverfrost's Plato and their FTN95 compiler (in "Release Win32" mode). What I wanted to do is to create a subroutine SUB(a,b) , where b is an optional variable. So far so good, but the problem arises when I try to give a new value to b with if (.NOT. present(b)) b=0 . This is the code: module MOD contains subroutine SUB(a,b) implicit none integer :: a integer,optional :: b if (.NOT. present(b)) b=0 print*,

C# 4.0 optional out/ref arguments

孤街醉人 提交于 2019-12-17 04:47:31
问题 Does C# 4.0 allow optional out or ref arguments? 回答1: 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

Is there a way to use two '…' statements in a function in R?

巧了我就是萌 提交于 2019-12-17 03:26:36
问题 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

Is there a way to use two '…' statements in a function in R?

假如想象 提交于 2019-12-17 03:26:26
问题 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

Optional params in Go?

梦想与她 提交于 2019-12-14 03:34:28
问题 I know that there aren't any optional params in the latest version of Go . But there are quite a lot cases when they are really helpful. Consider oversimplified example: func getFullName(firstName string, lastName string, maybeMiddleName func() (bool, string)) string { if ok, middleName:= maybeMiddleName(); ok { return firstName + " " + middleName + " " + lastName } return firstName + " " + lastName } That looks fine enough, thought requires a lot verbosity on a client side: whenever