default-parameters

How to find out the default values of a particular function's argument in another function in Python?

自闭症网瘾萝莉.ら 提交于 2019-12-01 15:16:40
问题 Let's suppose we have a function like this: def myFunction(arg1='a default value'): pass We can use introspection to find out the names of the arguments that myFunction() takes using myFunction.func_code.co_varnames , but how to find out the default value of arg1 (which is 'a default value' in the above example)? 回答1: As an alternative to rooting around in the attributes of the function you can use the inspect module for a slightly friendlier interface: For Python 3.x interpreters: import

Infer template argument from default parameter

£可爱£侵袭症+ 提交于 2019-12-01 04:12:54
问题 Consider this code: #include <functional> template <typename T,typename COMP> bool foo(T a,T b,COMP c = std::less<T>()) { return c(a,b); } bool bar(int a, int b){ return a<b;} int main(){ foo(1,2,bar); // OK foo(1,2,std::less<int>()); // OK foo(1,2); // error } The first two calls are fine, but it seems to be forbidden to let the compiler infer the type of COMP from the default parameter: <source>:14:5: error: no matching function for call to 'foo' foo(1,2); ^~~ <source>:4:6: note: candidate

How to handle nested default parameters with object destructuring?

ぃ、小莉子 提交于 2019-11-30 19:08:51
I am trying to figure out if it is possible to handle multiple levels of default parameters with destructuring. Since it is not easy to explain with words, here is a step-by-step example... 1 - Flat object destructuring with default parameters Destructuring this object is easy: let obj = { foo: 'Foo', bar: 'Bar' }; With {foo = 'Foo', bar = 'Bar'} = {} in a function signature, an object will be created if there is no argument passed when the function is called. If an object is passed but some referenced properties are undefined , they will be replaced by their default values. This code works

Function pointers with default parameters in C++

二次信任 提交于 2019-11-30 16:49:03
How does C++ handle function pointers in relation to functions with defaulted parameters? If I have: void foo(int i, float f = 0.0f); void bar(int i, float f); void (*func_ptr1)(int); void (*func_ptr2)(int, float); void (*func_ptr3)(int, float = 10.0f); Which function pointers can I use in relation to which function? Both foo() and bar() can only be assigned to func_ptr2 . §8.3.6/2 : A default argument is not part of the type of a function. [Example: int f(int = 0); void h() { int j = f(1); int k = f(); // OK, means f(0) } int (*p1)(int) = &f; int (*p2)() = &f; // error: type mismatch --end

Fortran 2003 / 2008: Elegant default arguments?

懵懂的女人 提交于 2019-11-30 13:13:25
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 Personally, I often ran into the problem of accidentially typing VAL instead of AVAL , i.e. the

Is it possible to know if the parameter was defaulted

拈花ヽ惹草 提交于 2019-11-30 10:04:29
问题 Caution: This problem is limited to MSVS I have this function signature: void do_somthing(std::vector<foo>& bar={}); Is it possible to differ between those two calls for the function: First: do_something() Second: std::vector<foo> v; do_something(v); In other words, I want something like: void do_somthing(std::vector<foo>& bar={}){ if(/* bar was defaulted*/){ } else{ } } EDIT: The actual code: template<class Tinput_iterator> Tmodel perform_fitting(Tinput_iterator begin_data, Tinput_iterator

Why can't I have template and default arguments?

时光怂恿深爱的人放手 提交于 2019-11-30 05:13:36
I changed a paremeter in a function to accept any kind of object using a template but I can't use it in conjunction with other default parameters, is there something I am missing? #include <string> #include <iostream> class MyClass { public: std::wstring msg = L"hey"; MyClass(){}; }; class MyClass2{ public: template<class T> MyClass2(T* t, int i); }; template<class T> MyClass2::MyClass2(T* t,int i=0){ std::wcout << t->msg << std::endl; } int main(int argc, char **argv) { MyClass mc; MyClass2 mc2(&mc); return 0; } Output: practice.cpp:16:32: error: redeclaration of 'MyClass2::MyClass2(T*, int)'

How does `this` work in default parameters?

夙愿已清 提交于 2019-11-30 03:54:22
So... ES6¹ (which happens to be standardized a few hours ago) brings default parameters for functions similar to those in PHP, Python etc. I can do stuff like: function foo (bar = 'dum') { return bar; } foo(1); // 1 foo(); // 'dum' foo(undefined); // 'dum' MDN says that the default value for the parameter is evaluated at call time. Which means each time I call the function, the expression 'dum' is evaluated again (unless the implementation does some weird optimizations which we don't care about). My question is, how does this play into this? let x = { foo (bar = this.foo) { return bar; } } let

How to handle nested default parameters with object destructuring?

瘦欲@ 提交于 2019-11-30 03:49:24
问题 I am trying to figure out if it is possible to handle multiple levels of default parameters with destructuring. Since it is not easy to explain with words, here is a step-by-step example... 1 - Flat object destructuring with default parameters Destructuring this object is easy: let obj = { foo: 'Foo', bar: 'Bar' }; With {foo = 'Foo', bar = 'Bar'} = {} in a function signature, an object will be created if there is no argument passed when the function is called. If an object is passed but some

Is it possible to know if the parameter was defaulted

断了今生、忘了曾经 提交于 2019-11-29 18:49:17
Caution: This problem is limited to MSVS I have this function signature: void do_somthing(std::vector<foo>& bar={}); Is it possible to differ between those two calls for the function: First: do_something() Second: std::vector<foo> v; do_something(v); In other words, I want something like: void do_somthing(std::vector<foo>& bar={}){ if(/* bar was defaulted*/){ } else{ } } EDIT: The actual code: template<class Tinput_iterator> Tmodel perform_fitting(Tinput_iterator begin_data, Tinput_iterator end_data, std::vector<Tpoint>& inliers = {}); I have this function signature: void do_somthing(std: