optional-arguments

Argparse optional arguments with multilevel parser/subparser

你说的曾经没有我的故事 提交于 2021-02-11 15:40:37
问题 I have a set of parsers and subparsers to build a production or development system. If the user picks production, he can add options and all is well. If he pics development, he can enter an architecture and then enter build options. This is where it gets sticky. I want him to be able to select build option 'comms' 'server' or 'all', but if he picks server, he has more choices. My implementation is below. I tried combinations of parsers and subpasers (it seems that arguments can only be added

Java: Making an Optional Command Line Argument

做~自己de王妃 提交于 2021-01-29 04:34:19
问题 I am working on a program that is supposed to take one required command line argument and one optional command line argument. The first argument is the name of an output file where data will be written to, and the second is a number that will be used to calculate the data to be written to the output file. If the user does not enter a number, then it should just use a default value to calculate the data. For example, if the user entered command line arguments "Foo.csv 1024" the program would

How to change default value of optional function parameter in Python 2.7?

牧云@^-^@ 提交于 2020-05-28 09:59:27
问题 I need to change global variable S at a.py from b.py , but it is used as a default value in a function at a.py . a.py S = "string" def f(s=S): print(s) print(S) b.py import a def main(): a.S = "another string" a.f() if __name__ == "__main__": main() python b.py outputs string another string instead of expected another string another string If I call a.f in b.py like this a.f(a.S) this works as expected, but is there any way to change default variable value? 回答1: The short answer is: You can't

How to change default value of optional function parameter in Python 2.7?

狂风中的少年 提交于 2020-05-28 09:59:09
问题 I need to change global variable S at a.py from b.py , but it is used as a default value in a function at a.py . a.py S = "string" def f(s=S): print(s) print(S) b.py import a def main(): a.S = "another string" a.f() if __name__ == "__main__": main() python b.py outputs string another string instead of expected another string another string If I call a.f in b.py like this a.f(a.S) this works as expected, but is there any way to change default variable value? 回答1: The short answer is: You can't

Python 3.5 TypeError: got multiple values for argument [duplicate]

徘徊边缘 提交于 2020-01-30 05:04:29
问题 This question already has answers here : TypeError: got multiple values for argument (6 answers) Closed 3 years ago . def f(a, b, *args): return (a, b, args) f(a=3, b=5) (3, 5, ()) whereas: f(a=3, b=5, *[1,2,3]) TypeError: got multiple values for argument 'b' Why it behaves like this? Any particular reason? 回答1: In the documentation for calls: If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from these iterables are treated as if they

Python 3.5 TypeError: got multiple values for argument [duplicate]

↘锁芯ラ 提交于 2020-01-30 05:03:26
问题 This question already has answers here : TypeError: got multiple values for argument (6 answers) Closed 3 years ago . def f(a, b, *args): return (a, b, args) f(a=3, b=5) (3, 5, ()) whereas: f(a=3, b=5, *[1,2,3]) TypeError: got multiple values for argument 'b' Why it behaves like this? Any particular reason? 回答1: In the documentation for calls: If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from these iterables are treated as if they

Fortran 2003 / 2008: Elegant default arguments?

空扰寡人 提交于 2020-01-10 01:35:57
问题 In fortran, we can define default arguments. However, if an optional argument is not present, it can also not be set. When using arguments as keyword arguments with default values, this leads to awkward constructs like PROGRAM PDEFAULT CALL SUB CALL SUB(3) CONTAINS SUBROUTINE SUB(VAL) INTEGER, OPTIONAL :: VAL INTEGER :: AVAL ! short for "actual val" IF(PRESENT(VAL)) THEN AVAL = VAL ELSE AVAL = -1 ! default value END IF WRITE(*,'("AVAL is ", I0)') AVAL END SUBROUTINE SUB END PROGRAM PDEFAULT

Creating a C# Nullable Int32 within Python (using Python.NET) to call a C# method with an optional int argument

£可爱£侵袭症+ 提交于 2019-12-29 07:52:43
问题 I'm using Python.NET to load a C# Assembly to call C# code from Python. This works pretty cleanly, however I am having an issue calling a method that looks like this: A method within Our.Namespace.Proj.MyRepo: OutputObject GetData(string user, int anID, int? anOptionalID= null) I can call the method for the case where the optional third argument is present but can't figure out what to pass for the third argument to match the null case. import clr clr.AddReference("Our.Namespace.Proj") import

Creating a C# Nullable Int32 within Python (using Python.NET) to call a C# method with an optional int argument

倖福魔咒の 提交于 2019-12-29 07:52:10
问题 I'm using Python.NET to load a C# Assembly to call C# code from Python. This works pretty cleanly, however I am having an issue calling a method that looks like this: A method within Our.Namespace.Proj.MyRepo: OutputObject GetData(string user, int anID, int? anOptionalID= null) I can call the method for the case where the optional third argument is present but can't figure out what to pass for the third argument to match the null case. import clr clr.AddReference("Our.Namespace.Proj") import

Naming practice for optional argument in python function

让人想犯罪 __ 提交于 2019-12-24 16:56:04
问题 Is it OK to give the optional argument in a python function the same name as an outside variable? It seems to work fine as functions f and g defined below give the same output. However, is this considered bad practice and does it fail in some cases? a = 1 def f(x,A=a): return x*A def g(x,a=a): return x*a print g(2) >> 2 print g(2,a=2) >> 4 回答1: It will work, allthough it can easily be a bit confusing, it is perfectly valid. This is due to the fact that the function and the call to the