default-parameters

Python, default keyword arguments after variable length positional arguments

北城余情 提交于 2019-11-26 22:17:29
I thought I could use named parameters after variable-length positional parameters in a function call in Python 2, but I get a SyntaxError when importing a python class. I'm writing with the following "get" method, for example: class Foo(object): def __init__(self): print "You have created a Foo." def get(self, *args, raw=False, vars=None): print len(args) print raw print vars The error looks like: def get(self, *args, raw=False, vars=None): ^ SyntaxError: invalid syntax I'd like to be able to call the method several ways: f = Foo() f.get(arg1, arg2) f.get(arg1, raw=True) f.get(arg1, arg2, raw

Default template parameter partial specialization

≯℡__Kan透↙ 提交于 2019-11-26 19:48:24
问题 Please explain to me why the following piece of code complies and works perfectly. I am very confused. #include<iostream> template<class A = int, class B=double> class Base {}; template<class B> class Base <int, B> { public: Base() { std::cout<<"it works!!!!!\n"; } }; int main() { Base<> base; // it prints "it works!!!!!" return 0; } Shouldn't it fall into the generalized form of the template class Base? 回答1: The default argument applies to the specialization -- and, in fact, a specialization

Default Values for function parameters in Python [duplicate]

随声附和 提交于 2019-11-26 19:10:45
问题 Possible Duplicate: default value of parameter as result of instance method While it is possible to set default values to function parameters in python: def my_function(param_one='default') ... It seems not to be possible to access the current instance (self): class MyClass(..): def my_function(self, param_one=self.one_of_the_vars): ... My question(s): Is this true that I cannot access the current instance to set the default parameter in functions? If it is not possble: what are the reasons

Python, default keyword arguments after variable length positional arguments

梦想的初衷 提交于 2019-11-26 08:15:08
问题 I thought I could use named parameters after variable-length positional parameters in a function call in Python 2, but I get a SyntaxError when importing a python class. I\'m writing with the following \"get\" method, for example: class Foo(object): def __init__(self): print \"You have created a Foo.\" def get(self, *args, raw=False, vars=None): print len(args) print raw print vars The error looks like: def get(self, *args, raw=False, vars=None): ^ SyntaxError: invalid syntax I\'d like to be

C default arguments

拟墨画扇 提交于 2019-11-26 05:57:00
问题 Is there a way to specify default arguments to a function in C? 回答1: Not really. The only way would be to write a varargs function and manually fill in default values for arguments which the caller doesn't pass. 回答2: Wow, everybody is such a pessimist around here. The answer is yes. It ain't trivial: by the end, we'll have the core function, a supporting struct, a wrapper function, and a macro around the wrapper function. In my work I have a set of macros to automate all this; once you

Does Java support default parameter values?

这一生的挚爱 提交于 2019-11-26 05:41:11
问题 I came across some Java code that had the following structure: public MyParameterizedFunction(String param1, int param2) { this(param1, param2, false); } public MyParameterizedFunction(String param1, int param2, boolean param3) { //use all three parameters here } I know that in C++ I can assign a parameter a default value. For example: void MyParameterizedFunction(String param1, int param2, bool param3=false); Does Java support this kind of syntax? Are there any reasons why this two step

“Least Astonishment” and the Mutable Default Argument

岁酱吖の 提交于 2019-11-25 22:09:15
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it \"a dramatic design flaw\"

Set a default parameter value for a JavaScript function

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-25 21:53:56
问题 I would like a JavaScript function to have optional arguments which I set a default on, which get used if the value isn\'t defined (and ignored if the value is passed). In Ruby you can do it like this: def read_file(file, delete_after = false) # code end Does this work in JavaScript? function read_file(file, delete_after = false) { // Code } 回答1: From ES6/ES2015, default parameters are in the language specification. function read_file(file, delete_after = false) { // Code } just works.