mutable

How to check immutability [duplicate]

﹥>﹥吖頭↗ 提交于 2020-03-18 11:55:05
问题 This question already has answers here : Check for mutability in Python? (6 answers) Closed 6 years ago . In python, is there any way to check if an object is immutable or mutable? like isimmutable(a) would return True , if a is immutable, else returns False . 回答1: There are no general tests for immutability. An object is immutable only if none of its methods can mutate the underlying data. take a look at this question the answer says: 1) Keys must not be mutable, unless you have a user

How to check immutability [duplicate]

断了今生、忘了曾经 提交于 2020-03-18 11:53:47
问题 This question already has answers here : Check for mutability in Python? (6 answers) Closed 6 years ago . In python, is there any way to check if an object is immutable or mutable? like isimmutable(a) would return True , if a is immutable, else returns False . 回答1: There are no general tests for immutability. An object is immutable only if none of its methods can mutate the underlying data. take a look at this question the answer says: 1) Keys must not be mutable, unless you have a user

How to mutate a list with a function in python?

谁说我不能喝 提交于 2020-02-27 05:07:11
问题 Here's a pseudocode I've written describing my problem:- func(s): #returns a value of s x = a list of strings print func(x) print x #these two should give the SAME output When I print the value of x in the end, I want it to be the one returned by func(x). Can I do something like this only by editing the function (and without setting x = func(x) ) 回答1: func(s): s[:] = whatever after mutating return s x = a list of strings print func(x) print x You don't actually need to return anything: def

How can I do a mutable borrow in a for loop?

此生再无相见时 提交于 2020-02-02 05:39:39
问题 I tried: let mut vec = [1,2,3]; for mut x in &vec { *x=3; } for mut &x in &vec { x=3; } for mut *x in &vec { x=3; } for mut x in mut &vec { *x=3; } for mut x in &(mut vec) { *x=3; } None of these work; how should I do it? 回答1: You may want to re-read The Rust Programming Language section on mutability: You can also create a reference to it, using &x , but if you want to use the reference to change it, you will need a mutable reference: let mut x = 5; let y = &mut x; fn main() { let mut array

In Rust, what exactly are mutable and immutable borrows?

久未见 提交于 2020-01-24 13:53:06
问题 I'm stuck with the Rust concepts of borrowing and mutable : #[derive(Debug)] struct Rectangle { height: u32, width: u32, } fn mut_area(rect_mut: &mut Rectangle) -> u32 { rect_mut.width /= 2; rect_mut.height * rect_mut.width } fn mut_string(s: &mut String) -> &str { s.push_str("!"); let len = s.len(); &s[0..len / 2] } fn main() { let mut rect = Rectangle { height: 50, width: 40, }; println!("original rect: {:?}", rect); let a = mut_area(&mut rect); println!("area of rect: {}", a); println!(

In Rust, what exactly are mutable and immutable borrows?

烈酒焚心 提交于 2020-01-24 13:52:00
问题 I'm stuck with the Rust concepts of borrowing and mutable : #[derive(Debug)] struct Rectangle { height: u32, width: u32, } fn mut_area(rect_mut: &mut Rectangle) -> u32 { rect_mut.width /= 2; rect_mut.height * rect_mut.width } fn mut_string(s: &mut String) -> &str { s.push_str("!"); let len = s.len(); &s[0..len / 2] } fn main() { let mut rect = Rectangle { height: 50, width: 40, }; println!("original rect: {:?}", rect); let a = mut_area(&mut rect); println!("area of rect: {}", a); println!(

What is the proper way to remove elements from a scala mutable map using a predicate

蹲街弑〆低调 提交于 2020-01-22 13:27:52
问题 How to do that without creating any new collections? Is there something better than this? val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4) m.foreach(t => if (t._2 % 2 == 0) m.remove(t._1)) println(m) P.S. in Scala 2.8 回答1: retain does what you want. In 2.7: val a = collection.mutable.Map(1->"one",2->"two",3->"three") a: scala.collection.mutable.Map[Int,java.lang.String] = Map(2 -> two, 1 -> one, 3 -> three) scala> a.retain((k,v) => v.length < 4) scala

When is it okay to use “var” in Scala?

巧了我就是萌 提交于 2020-01-22 13:25:28
问题 I know that Scala has var (for mutable state) but pure functional programming discourages use of any mutable state and rather focuses on using val for everything. Coming from an imperative world it's hard to let go of mutable state. My question is when is it okay to use var in your Scala code ? Can all code really be done using just val. If yes, then why does Scala have vars? 回答1: Here are some reasons for vars in Scala: Scala is a multi-paradigm language, it encourages functional programming

Are open(file, “wt” or “rt”) different objects?

耗尽温柔 提交于 2020-01-15 05:29:07
问题 When you do: file = open("my file","wt") and file = open("my file" , "rt") These both create file objects that we use file methods on. But are they creating different file objects? And if they are creating different file objects would it be fair to say that the "wt" one is mutable, while the "rt" one is immutable? 回答1: No, that would not be fair to say. You are creating instances of the same standard file type, which proxies file manipulation calls to the operating system. The mode defines

What benefit does the ImmutableObject attribute provide?

僤鯓⒐⒋嵵緔 提交于 2020-01-10 05:09:08
问题 I was testing the ImmutableObjectAttribute attribute just for curiosity to see if I could gain some beneffits applying it, or if it was just for semantic decoration... ImmutableObjectAttribute Class Specifies that an object has no subproperties capable of being edited. So I have this class: <ImmutableObject(True)> Public Class TestClass <ImmutableObject(True)> Public sb As StringBuilder Public Sub New() sb = New StringBuilder End Sub End Class And I've tested with this code: Dim obj As New