mutability

Understanding mutability in Python [closed]

你离开我真会死。 提交于 2019-12-18 09:25:54
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I have this snippet: a = [1,2,3] b = a b = [4,5,6] # a doesnt change and this: a = [1,2,3] b = a b[0] = 5 # a also changes How is b 's

Understanding mutability in Python [closed]

梦想与她 提交于 2019-12-18 09:25:40
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I have this snippet: a = [1,2,3] b = a b = [4,5,6] # a doesnt change and this: a = [1,2,3] b = a b[0] = 5 # a also changes How is b 's

Why two individually created immutable objects have same id and mutable objects have different while both refer to same values? [duplicate]

拥有回忆 提交于 2019-12-18 08:46:34
问题 This question already has an answer here : What's with the integer cache maintained by the interpreter? (1 answer) Closed 5 years ago . Two individually created mutable list have different ids. Python SHELL: (mutable) >>> mylist = ['spam', 'eggs'] >>> yourlist = ['spam', 'eggs'] >>> id(mylist), id(yourlist) (49624456, 48910408) While two individually created immutable strings have similar ids. Python SHELL: (immutable) >>> a = 10 >>> b = 10 >>> id(a), id(b) (507099072, 507099072) Is a and b

How to iterate through a Hashmap, print the key/value and remove the value in Rust?

冷暖自知 提交于 2019-12-18 05:35:39
问题 This should be a trivial task in any language. This isn't working in Rust. use std::collections::HashMap; fn do_it(map: &mut HashMap<String, String>) { for (key, value) in map { println!("{} / {}", key, value); map.remove(key); } } fn main() {} Here's the compiler error: error[E0382]: use of moved value: `*map` --> src/main.rs:6:9 | 4 | for (key, value) in map { | --- value moved here 5 | println!("{} / {}", key, value); 6 | map.remove(key); | ^^^ value used here after move | = note: move

Where is a MutexGuard if I never assign it to a variable?

拈花ヽ惹草 提交于 2019-12-12 19:59:40
问题 I don't understand "where" the MutexGuard in the inner block of code is. The mutex is locked and unwrapped, yielding a MutexGuard . Somehow this code manages to dereference that MutexGuard and then mutably borrow that object. Where did the MutexGuard go? Also, confusingly, this dereference cannot be replaced with deref_mut . Why? use std::sync::Mutex; fn main() { let x = Mutex::new(Vec::new()); { let y: &mut Vec<_> = &mut *x.lock().unwrap(); y.push(3); println!("{:?}, {:?}", x, y); } let z =

What is the difference between mutable values and immutable value redefinition?

痴心易碎 提交于 2019-12-12 11:55:10
问题 I have read that values in F# are immutable. However, I have also come across the concept of redefining value definitions, which shadow the previous ones. How is this different from a mutable value ? I ask this not just as a theoretical construct, but also if there is any advice on when to use mutable values and when to redefine expressions instead; or if someone can point out that the latter is not idiomatic f#. Basic example of redefinition: let a = 1;; a;; //1 let a = 2;; a;; //2 Update 1:

Where is the mutability of Objects defined in ECMAScript?

断了今生、忘了曾经 提交于 2019-12-11 06:25:14
问题 In this question about the passing of arguments in JavaScript functions, we learn that everything is passed by value in JavaScript. In Mozilla documents, it is mentioned that the primitive types are immutable, and objects are. Although I came from the procedural and structured programming school, I was able to quickly pick up the concepts. In the ECMAScript standard, it is defined that "An Object is 'logically' a collection of properties". The standard also defines how objects may be compared

Change jQuery Knob Min/Max value

三世轮回 提交于 2019-12-10 20:26:15
问题 I'm working on an angular wrapper for the jquery-knob widget. The following works as long as the maximum value doesn't change. If it does, the ng-model binding is lost. If I don't destroy the knob widget at the beginning of the watch, the max value doesn't change. //Directive app.directive('knobWidget', function () { return { scope: { maxbinding: "=maxbinding", maxbindingprop: "@maxbindingprop" }, restrict: 'A', require: 'ngModel', link: function (scope, elem, attrs, ngModel) { ngModel.

How to interpret immutable references to mutable types in Rust?

五迷三道 提交于 2019-12-10 18:48:07
问题 It seems that I cannot mutate anything if there is any immutable reference in my chain of dereferencing. A sample: fn main() { let mut x = 42; let y: &mut i32 = &mut x; // first layer let z: &&mut i32 = &y; // second layer **z = 100; // Attempt to change `x`, gives compiler error. println!("Value is: {}", z); } I'm getting the compiler error: error[E0594]: cannot assign to `**z` which is behind a `&` reference --> src/main.rs:5:5 | 4 | let z: &&mut i32 = &y; // second layer | -- help:

Command Pattern vs. Visitor Pattern

て烟熏妆下的殇ゞ 提交于 2019-12-07 00:37:06
问题 Is it generally acceptable to allow a Visitor to modify state of the Receiver, or should that be a Command pattern instead? 回答1: The purpose of the visitor pattern is to allow new operations to be added to a class heirarchy without modification to that heirarchy. I've never seen anyone suggesting that only read-only operations are acceptable. The only limitation is that the added operations should only use the public interface of the class heirarchy. 回答2: I don't think you can make a blanket