mutable

Why does using `arg=None` fix Python's mutable default argument issue?

南楼画角 提交于 2019-11-26 04:27:13
问题 I\'m at the point in learning Python where I\'m dealing with the Mutable Default Argument problem. # BAD: if `a_list` is not passed in, the default will wrongly retain its contents between successive function calls def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list # GOOD: if `a_list` is not passed in, the default will always correctly be [] def good_append(new_item, a_list=None): if a_list is None: a_list = [] a_list.append(new_item) return a_list I understand that a

Mutable vs immutable objects

只愿长相守 提交于 2019-11-26 03:16:01
问题 I\'m trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I\'m having trouble understanding what the negative impacts are of this. What are the best practices around using mutable objects? Should you avoid them whenever possible? 回答1: Well, there are a couple aspects to this. Number one, mutable objects without reference-identity can cause bugs at odd times. For example, consider a

Are mutable hashmap keys a dangerous practice?

こ雲淡風輕ζ 提交于 2019-11-26 03:14:39
问题 Is it bad practice to use mutable objects as Hashmap keys? What happens when you try to retrieve a value from a Hashmap using a key that has been modified enough to change its hashcode? For example, given class Key { int a; //mutable field int b; //mutable field public int hashcode() return foo(a, b); // setters setA and setB omitted for brevity } with code HashMap<Key, Value> map = new HashMap<Key, Value>(); Key key1 = new Key(0, 0); map.put(key1, value1); // value1 is an instance of Value

Does the &#39;mutable&#39; keyword have any purpose other than allowing the variable to be modified by a const function?

那年仲夏 提交于 2019-11-26 01:36:43
问题 A while ago I came across some code that marked a member variable of a class with the mutable keyword. As far as I can see it simply allows you to modify a variable in a const method: class Foo { private: mutable bool done_; public: void doSomething() const { ...; done_ = true; } }; Is this the only use of this keyword or is there more to it than meets the eye? I have since used this technique in a class, marking a boost::mutex as mutable allowing const functions to lock it for thread-safety

What&#39;s the difference between placing “mut” before a variable name and after the “:”?

笑着哭i 提交于 2019-11-26 00:25:34
问题 Here are two function signatures I saw in the Rust documentation: fn modify_foo(mut foo: Box<i32>) { *foo += 1; *foo } fn modify_foo(foo: &mut i32) { *foo += 1; *foo } Why the different placement of mut ? It seems that the first function could also be declared as fn modify_foo(foo: mut Box<i32>) { /* ... */ } 回答1: mut foo: T means you have a variable called foo that is a T . You are allowed to change what the variable refers to : let mut val1 = 2; val1 = 3; // OK let val2 = 2; val2 = 3; //

Why can&#39;t strings be mutable in Java and .NET?

不羁的心 提交于 2019-11-26 00:17:40
问题 Why is it that they decided to make String immutable in Java and .NET (and some other languages)? Why didn\'t they make it mutable? 回答1: According to Effective Java, chapter 4, page 73, 2nd edition: "There are many good reasons for this: Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure. [...] " Immutable objects are simple. An immutable object can be in exactly one state, the state in which it was created. If you

Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time

删除回忆录丶 提交于 2019-11-26 00:07:21
问题 I\'m trying to navigate a recursive data structure iteratively in order to insert elements at a certain position. To my limited understanding, this means taking a mutable reference to the root of the structure and successively replacing it by a reference to its follower: type Link = Option<Box<Node>>; struct Node { next: Link } struct Recursive { root: Link } impl Recursive { fn back(&mut self) -> &mut Link { let mut anchor = &mut self.root; while let Some(ref mut node) = *anchor { anchor =

Why are mutable structs “evil”?

淺唱寂寞╮ 提交于 2019-11-25 23:55:41
问题 Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question). What\'s the actual problem with mutability and structs in C#? 回答1: Structs are value types which means they are copied when they are passed around. So if you change a copy you are changing only that copy, not the original and not any other copies which might be around. If your struct is immutable then all automatic copies resulting from being

List of lists changes reflected across sublists unexpectedly

耗尽温柔 提交于 2019-11-25 22:09:27
问题 I needed to create a list of lists in Python, so I typed the following: myList = [[1] * 4] * 3 The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then I changed one of the innermost values: myList[0][0] = 5 Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]] which is not what I wanted or expected. Can someone please explain what\'s going on, and how to get around it? 回答1: When you write [x]*3 you get, essentially, the list [x, x, x] . That is, a list

Immutable vs Mutable types

放肆的年华 提交于 2019-11-25 21:43:03
问题 I\'m confused on what an immutable type is. I know the float object is considered to be immutable, with this type of example from my book: class RoundFloat(float): def __new__(cls, val): return float.__new__(cls, round(val, 2)) Is this considered to be immutable because of the class structure / hierarchy?, meaning float is at the top of the class and is its own method call. Similar to this type of example (even though my book says dict is mutable): class SortedKeyDict(dict): def __new__(cls,