immutability

The immutable object in python

↘锁芯ラ 提交于 2019-12-29 09:23:07
问题 I see a article about the immutable object. It says when: variable = immutable As assign the immutable to a variable. for example a = b # b is a immutable It says in this case a refers to a copy of b , not reference to b . If b is mutable , the a wiil be a reference to b so: a = 10 b = a a =20 print (b) #b still is 10 but in this case: a = 10 b = 10 a is b # return True print id(10) print id(a) print id(b) # id(a) == id(b) == id(10) if a is the copy of 10 , and b is also the copy of 10 , why

Mutable or immutable class?

为君一笑 提交于 2019-12-29 06:51:46
问题 I had read in some design book that immutable class improves scalability and its good practice to write immutable class wherever possible. But I think so immutable class increase object proliferation. So is it good of going immutable class or better go for static class (A class with all the methods static) for improve scalability ? 回答1: Immutable classes do promote object proliferation, but if you want safety, mutable objects will promote more object proliferation because you have to return

The final word on NSStrings: Mutable and Immutable

随声附和 提交于 2019-12-29 06:19:02
问题 I've read in several books... and online... about immutable and mutable strings. They claim "immutable strings" can't be changed. (But they never define "change".) Which of these NSStrings could be changed without using NSMutableString? The string contains "catfish"... and I later try to change it to "cat". (Same letters, just shorter.) It contains "cat"... and I try to change it to "catfish". (Similar start... but just made longer.) I change "cat" into "CAT". (Same letters, but just the case

Immutable type and property in C#

我是研究僧i 提交于 2019-12-29 04:35:28
问题 What is meant by immutable type and immutable property in C# ? can you give simple example? 回答1: An immutable type is a type of which its properties can only be set at initialization. Once an object is created, nothing can be changed anymore. An immutable property is simply a read-only property. In the following example, ImmutableType is an immutable type with one property Test . Test is a read-only property. It can only be set at construction. class ImmutableType { private readonly string

Why might a System.String object not cache its hash code?

和自甴很熟 提交于 2019-12-29 04:27:12
问题 A glance at the source code for string.GetHashCode using Reflector reveals the following (for mscorlib.dll version 4.0): public override unsafe int GetHashCode() { fixed (char* str = ((char*) this)) { char* chPtr = str; int num = 0x15051505; int num2 = num; int* numPtr = (int*) chPtr; for (int i = this.Length; i > 0; i -= 4) { num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0]; if (i <= 2) { break; } num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1]; numPtr += 2; } return (num +

R: selecting subset without copying

允我心安 提交于 2019-12-29 04:02:31
问题 Is there a way to select a subset from objects (data frames, matrices, vectors) without making a copy of selected data? I work with quite large data sets, but never change them. However often for convenience I select subsets of the data to operate on. Making a copy of a large subset each time is very memory inefficient, but both normal indexing and subset (and thus xapply() family of functions) create copies of selected data. So I'm looking for functions or data structures that can overcome

How can we maintain Immutability of a class with a mutable reference

99封情书 提交于 2019-12-28 12:05:11
问题 I know all the basic rules to make our class immutable but I am a little confused when there is another class reference. I know if there is collection instead of Address then we can make use of Collections.unmodifiableList(new ArrayList<>(modifiable)); and then we can make our class immutable. But in below case I am still unable to get the concept. public final class Employee{ private final int id; private Address address; public Employee(int id, Address address) { this.id = id; this.address

A grasp of immutable datastructures

柔情痞子 提交于 2019-12-28 05:21:28
问题 I am learning scala and as a good student I try to obey all rules I found. One rule is: IMMUTABILITY!!! So I have tried to code everything with immutable data structures and vals, and sometimes this is really hard. But today I thought to myself: the only important thing is that the object/class should have no mutable state. I am not forced to code all methods in an immutable style, because these methods don't affect each other. My Question: Am I correct or are there any problems/disadvantages

Idiomatic Way to declare C++ Immutable Classes

独自空忆成欢 提交于 2019-12-28 03:45:18
问题 So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variables and any methods const. struct RockSolid { const float x; const float y; float MakeHarderConcrete() const { return x + y; } } Is this actually the way "we should do it" in C++? Or is there a better way? 回答1: The way you proposed is perfectly fine, except if in your code you need to make assignment

Immutable numpy array?

自古美人都是妖i 提交于 2019-12-28 03:31:25
问题 Is there a simple way to create an immutable NumPy array? If one has to derive a class from ndarray to do this, what's the minimum set of methods that one has to override to achieve immutability? 回答1: You can make a numpy array unwriteable: a = np.arange(10) a.flags.writeable = False a[0] = 1 # Gives: RuntimeError: array is not writeable Also see the discussion in this thread: http://mail.scipy.org/pipermail/numpy-discussion/2008-December/039274.html and the documentation: http://docs.scipy