Why must dictionary keys be immutable?

On my computer, there’s a file /etc/dictionaries-common/words containing a large collection of English words: >>> with open(“/etc/dictionaries-common/words”) as f: … words = [line.strip() for line in f] … >>> “python” in words True >>> “BDFL” in words False Let’s create a dictionary storing the lengths of all those words: >>> word_lengths = {w: len(w) for … Read more

Why are Integers immutable in Java?

You won’t find a mandatory reason why java.lang wrappers must be immutable. Simply because it’s a design decision. They could have decided otherwise. The language designers had to choose between mutable and immutable. And they chose immutable. That’s it. There are some compelling (IMO) reasons though to make them immutable: It’s consistent with String. The … Read more

Scala – initialization order of vals

Vals are initialized in the order they are declared (well, precisely, non-lazy vals are), so properties is getting initialized before loadedProps. Or in other words, loadedProps is still null when propertiesis getting initialized. The simplest solution here is to define loadedProps before properties: class Config { private val loadedProps = { val p = new … Read more