default-value

Good uses for mutable function argument default values?

谁说胖子不能爱 提交于 2019-11-27 18:17:29
It is a common mistake in Python to set a mutable object as the default value of an argument in a function. Here's an example taken from this excellent write-up by David Goodger : >>> def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list >>> print bad_append('one') ['one'] >>> print bad_append('two') ['one', 'two'] The explanation why this happens is here . And now for my question: Is there a good use-case for this syntax? I mean, if everybody who encounters it makes the same mistake, debugs it, understands the issue and from thereon tries to avoid it, what use is there

Why is the default value of the string type null instead of an empty string?

允我心安 提交于 2019-11-27 17:05:23
It's quite annoying to test all my strings for null before I can safely apply methods like ToUpper() , StartWith() etc... If the default value of string were the empty string, I would not have to test, and I would feel it to be more consistent with the other value types like int or double for example. Additionally Nullable<String> would make sense. So why did the designers of C# choose to use null as the default value of strings? Note: This relates to this question , but is more focused on the why instead of what to do with it. Habib Why is the default value of the string type null instead of

Python: list() as default value for dictionary

社会主义新天地 提交于 2019-11-27 15:44:20
问题 I have Python code that looks like: if key in dict: dict[key].append(some_value) else: dict[key] = [some_value] but I figure there should be some method to get around this 'if' statement. I tried dict.setdefault(key, []) dict[key].append(some_value) and dict[key] = dict.get(key, []).append(some_value) but both complain about "TypeError: unhashable type: 'list'". Any recommendations? Thanks! 回答1: The best method is to use collections.defaultdict with a list default: from collections import

Returning a default value. (C#)

孤人 提交于 2019-11-27 14:41:57
I'm creating my own dictionary and I am having trouble implementing the TryGetValue function. When the key isn't found, I don't have anything to assign to the out parameter, so I leave it as is. This results in the following error: "The out parameter 'value' must be assigned to before control leaves the current method" So, basically, I need a way to get the default value (0, false or nullptr depending on type). My code is similar to the following: class MyEmptyDictionary<K, V> : IDictionary<K, V> { bool IDictionary<K, V>.TryGetValue (K key, out V value) { return false; } .... } Jeff Yates You

C++ template function default value

爷,独闯天下 提交于 2019-11-27 14:29:45
Is it possible to define the default value for variables of a template function in C++? Something like below: template<class T> T sum(T a, T b, T c=????) { return a + b + c; } Scharron Try this: template<class T> T sum(T a, T b, T c=T()) { return a + b + c; } You can also put in T(5) if you are expecting an integral type and want the default value to be 5. David Rodríguez - dribeas It all depends on the assumptions that you can do about the type. template <typename T> T sum( T a, T b, T c = T() ) { return a+b+c; } template <typename T> T sum2( T a, T b, T c = T(5) ) { return a+b+c; } The first

How can I change the default value of an inherited dependency property?

那年仲夏 提交于 2019-11-27 13:33:17
How can I change the default value for an inherited dependency property? In our case, we've created a subclass of Control which by default has its Focusable set to 'true'. We want our subclass to have the default of 'false'. What we've been doing is simply setting it to 'false' in the constructor, but if someone uses ClearValue, it goes back to the default, not the value set in the constructor. Here's what I'm currently doing to achieve this (This is a test control with a DP of 'Foo' for an example.) I'm not a fan of the 'new' to hide the property although thanks to AddOwner , it does point to

C++ default initialization and value initialization: which is which, which is called when and how to reliably initialize a template-type member

隐身守侯 提交于 2019-11-27 11:15:42
问题 My question somewhat overlaps with this and several other similar ones. Those have some great answers, but I've read them and I'm still confused, so please don't consider this question a duplicate. So, I have the following code: class A { public: int _a; } void main() { A inst1; A* inst2 = new A; A* inst3 = new A(); } _a is left uninitialized in inst1 and inst2 and is initialized to 0 in inst3 . Which initialization is called which, and why does the code work as it does? Please take into I

Default value on JSP custom-tag attribute

丶灬走出姿态 提交于 2019-11-27 10:42:43
问题 When defining an attribute for a custom JSP tag, is it possible to specify a default value? The attribute directive doesn't have a default value attribute. Currently I'm making do with: <%@ attribute name="myAttr" required="false" type="java.lang.String" %> <c:if test="${empty myAttr}" > <c:set var="myAttr" value="defaultValue" /> </c:if> Is there a better way? 回答1: There is a better way: <c:set var="title" value="${(empty title) ? 'Default title' : title}" /> No need for custom tag in Java

Python constructor and default value [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-27 10:30:53
This question already has an answer here: “Least Astonishment” and the Mutable Default Argument 31 answers Somehow, in the Node class below, the wordList and adjacencyList variable is shared between all instances of Node. >>> class Node: ... def __init__(self, wordList = [], adjacencyList = []): ... self.wordList = wordList ... self.adjacencyList = adjacencyList ... >>> a = Node() >>> b = Node() >>> a.wordList.append("hahaha") >>> b.wordList ['hahaha'] >>> b.adjacencyList.append("hoho") >>> a.adjacencyList ['hoho'] Is there any way I can keep using the default value (empty list in this case)

Is there a better PHP way for getting default value by key from array (dictionary)?

大城市里の小女人 提交于 2019-11-27 10:26:57
问题 In Python one can do: foo = {} assert foo.get('bar', 'baz') == 'baz' In PHP one can go for a trinary operator as in: $foo = array(); assert( (isset($foo['bar'])) ? $foo['bar'] : 'baz' == 'baz'); I am looking for a golf version. Can I do it shorter/better in PHP? 回答1: I just came up with this little helper function: function get(&$var, $default=null) { return isset($var) ? $var : $default; } Not only does this work for dictionaries, but for all kind of variables: $test = array('foo'=>'bar');