immutability

Aren't Python strings immutable? Then why does a + “ ” + b work?

£可爱£侵袭症+ 提交于 2019-11-26 03:24:33
问题 My understanding was that Python strings are immutable. I tried the following code: a = \"Dog\" b = \"eats\" c = \"treats\" print a, b, c # Dog eats treats print a + \" \" + b + \" \" + c # Dog eats treats print a # Dog a = a + \" \" + b + \" \" + c print a # Dog eats treats # !!! Shouldn\'t Python have prevented the assignment? I am probably missing something. Any idea? 回答1: First a pointed to the string "Dog". Then you changed the variable a to point at a new string "Dog eats treats". You

How to make an immutable object in Python?

冷暖自知 提交于 2019-11-26 03:24:24
问题 Although I have never needed this, it just struck me that making an immutable object in Python could be slightly tricky. You can\'t just override __setattr__, because then you can\'t even set attributes in the __init__. Subclassing a tuple is a trick that works: class Immutable(tuple): def __new__(cls, a, b): return tuple.__new__(cls, (a, b)) @property def a(self): return self[0] @property def b(self): return self[1] def __str__(self): return \"<Immutable {0}, {1}>\".format(self.a, self.b)

Is a Java string really immutable?

余生长醉 提交于 2019-11-26 03:24:15
问题 We all know that String is immutable in Java, but check the following code: String s1 = \"Hello World\"; String s2 = \"Hello World\"; String s3 = s1.substring(6); System.out.println(s1); // Hello World System.out.println(s2); // Hello World System.out.println(s3); // World Field field = String.class.getDeclaredField(\"value\"); field.setAccessible(true); char[] value = (char[])field.get(s1); value[6] = \'J\'; value[7] = \'a\'; value[8] = \'v\'; value[9] = \'a\'; value[10] = \'!\'; System.out

Error: “Cannot modify the return value” c#

做~自己de王妃 提交于 2019-11-26 03:19:30
问题 I\'m using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error Message: Cannot modify the return value of \'expression\' because it is not a variable An attempt was made to modify a value type that was the result of an intermediate expression. Because the value is not persisted, the value will be unchanged. To resolve this error, store the result of the

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

What would a “frozen dict” be?

让人想犯罪 __ 提交于 2019-11-26 02:51:01
问题 A frozen set is a frozenset. A frozen list could be a tuple. What would a frozen dict be? An immutable, hashable dict. I guess it could be something like collections.namedtuple , but that is more like a frozen-keys dict (a half-frozen dict). Isn\'t it? A \"frozendict\" should be a frozen dictionary, it should have keys , values , get , etc., and support in , for , etc. update : * there it is : https://www.python.org/dev/peps/pep-0603 回答1: Python doesn't have a builtin frozendict type. It

Const in javascript? When to use it and is it necessary

孤人 提交于 2019-11-26 02:35:40
I've recently come across the const keyword in javascript. From what i can tell, it is used to create immutable variables , and I've tested to ensure that it cannot be redefined (in node.js): const x = 'const'; const x = 'not-const'; // Will give an error: 'constant 'x' has already been defined' I realise that it is not yet standardised across all browsers - but I'm only interested in the context of node.js/v8, and I've noticed that certain developers / projects seem to favour it heavily when the var keyword could be used to the same effect. Questions ? When is it appropriate to use const in

Why do we need immutable class?

放肆的年华 提交于 2019-11-26 02:28:57
问题 I am unable to get what are the scenarios where we need an immutable class. Have you ever faced any such requirement? or can you please give us any real example where we should use this pattern. 回答1: The other answers seem to focused on explaining why immutability is good. It is very good and I use it whenever possible. However, that is not your question . I'll take your question point by point to try to make sure you're getting the answers and examples you need. I am unable to get what are

How to create immutable objects in Java?

懵懂的女人 提交于 2019-11-26 02:28:25
问题 How to create immutable objects in Java? Which objects should be called immutable? If I have class with all static members is it immutable? 回答1: Below are the hard requirements of an immutable object. Make the class final make all members final, set them explicitly, in a static block, or in the constructor Make all members private No Methods that modify state Be extremely careful to limit access to mutable members(remember the field may be final but the object can still be mutable. ie private

Why can tuples contain mutable items?

只谈情不闲聊 提交于 2019-11-26 02:04:56
问题 If a tuple is immutable then why can it contain mutable items? It is seemingly a contradiction that when a mutable item such as a list does get modified, the tuple it belongs to maintains being immutable. 回答1: That's an excellent question. The key insight is that tuples have no way of knowing whether the objects inside them are mutable. The only thing that makes an object mutable is to have a method that alters its data. In general, there is no way to detect this. Another insight is that