immutability

Create two immutable objects with the same value in Python

元气小坏坏 提交于 2019-12-05 18:41:49
Is it possible in Python to create two immutable objects with the same value? So that you understand what I mean, here are some examples: >>> a = 13 >>> b = 13 >>> a is b True >>> a = 13 >>> b = 26/2 >>> a is b True >>> a = 13 >>> b = int.__new__(int, 13) >>> a is b True >>> a = 13 >>> b = int("13") >>> a is b True Is it possible to create a and b with the same value but a is b to return False ? Just learning.... :D Sure, just choose a value that is too large to be cached: >>> a = 256 >>> b = 256 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False >>> a = "hey" >>> b = "hey" >>> a is b

Akka and its Error Kernel

给你一囗甜甜゛ 提交于 2019-12-05 18:08:44
I am reading the Akka ( Java lib ) docs and need clarification on some of their own proclaimed Akka/Actor Best Practices . Actors should not block (i.e. passively wait while occupying a Thread) on some external entity...The blocking operations should be done in some special-cased thread which sends messages to the actors which shall act on them. So what does a code example of this look like in Akka/Java? If an Actor isn't an appriote place to put code that has to block, then what does satisfy the definition of "some special-cased thread"? Do not pass mutable objects between actors. In order to

Python: can I modify a Tuple?

て烟熏妆下的殇ゞ 提交于 2019-12-05 17:37:04
I have a 2 D tuple (Actually I thought, it was a list.. but the error says its a tuple) But anyways.. The tuple is of form: (floatnumber_val, prod_id) now I have a dictionary which contains key-> prod_id and value prod_name now.. i want to change the prod_id in tuple to prod_name So this is waht I did #if prodName is the tuple # prodDict is the dictionary for i in range(len(prodName)): key = prodName[i][1] # get the prodid if prodDict.has_key(key): value = prodDict[key] prodName[i][1] = value umm pretty straightforward but i get an error that TypeError: 'tuple' object does not support item

Performant way to detect immutables?

谁说胖子不能爱 提交于 2019-12-05 17:28:52
I have a utility that is designed to pass objects around our system. Because it is a multithreaded environment, the utility makes a deep copy of each object it passes to prevent any thread safety issues. I'm working on transitioning our system to use immutable objects to eliminate the need for this copy. I'm wonder what is the best (fastest) way to detect that the object is immutable? My first thought was to just to pick up on the attribute that we put on all our immutable objects (MessageAttribute). As you can see from the performance profile below, it takes quite a hit (roughly 10x the time

Why number are immutable in Javascript?

≡放荡痞女 提交于 2019-12-05 17:01:23
I have read the question and answer here: javascript numbers- immutable But it's not enough clear for me why the number (primitive type) are immutable? Just because they create a new reference but not overwrite the value? If on each assignemt is created a new reference var x = 5; x = 1; Would we have 100 times a new reference in the following loop? while (x < 101) { x++; } Is that efficient? I think I am not seeing correctly. I'm honestly not quite sure what kind of answer you expect since I don't quite understand what you are confused about. But here we go: Would we have 100 times a new

Why is Java's BigDecimal class not declared as final?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 16:43:13
问题 While checking the source code of Java's BigDecimal class, I was surprised that it was not declared as a final class : Class BigDecimal public class BigDecimal extends Number implements Comparable<BigDecimal> Immutable , arbitrary-precision signed decimal numbers. (from the Oracle Docs) Is there a specific reason for this or did the developers just forget to add that keyword? Is it a good practice to not declare immutable classes as final? The same goes for BigInteger, but not for String

Immutability and thread-safety in Scala

时光总嘲笑我的痴心妄想 提交于 2019-12-05 14:13:08
I'm reading the book Java concurrency in practice and when I read about the relation between immutability and thread-safety I tried to get deeper. So, I discovered that there is at least a use case in which the construction of an immutable class in Java can lead to the publishing of a non properly constructed object. According to this link, if the fields of the class are not declated final , the compiler could reorder the statements that needs to be done in order to construct the object. In fact, according to this link, to build an object the JVM needs to do these non-atomic operations:

How do I find out if a class is immutable in C#?

我是研究僧i 提交于 2019-12-05 13:53:14
问题 How do I find out if a class is immutable in C#? 回答1: There is ImmutableObjectAttribute , but this is rarely used and poorly supported - and of course not enforced (you could mark a mutable object with [ImmutableObject(true)] . AFAIK, the only thing this this affects is the way the IDE handles attributes (i.e. to show / not-show the named properties options). In reality, you would have to check the FieldInfo.IsInitOnly , but this only applies to truly 100% immutable types (assuming no

Why are Scala's `Lists` implemented as linked lists

大兔子大兔子 提交于 2019-12-05 13:38:49
问题 I always thought that the benefit of linked lists was that you could add or remove items (especially not from the end) without having to copy lots of elements thanks to the beauty of pointers. However, Scala's List is immutable (at least by default). What is the benefit of having an immutable linked list (because there are definite downsides, e.g. not O(1) element access.) Thanks! 回答1: I think the main reason is that one of the most powerful uses of linked lists is head/tail splitting. There

enum vs immutable in D

梦想与她 提交于 2019-12-05 13:38:27
问题 What's the difference between enum i = 2; enum s = "Hello"; and immutable i = 2; immutable s = "Hello"; in D 2.0? 回答1: An enum is a user-defined type, not a variable. enum e = 2; is a short-hand for something like this enum : int { e = 2 } (i.e. an anonymous enum with one member e ), see the documentation. By definition, all members of an anonymous enum are placed into the current scope. So, e is a type member placed into the current scope, where it behaves like a literal. immutable i = 2; on