All Questions

344
votes
13answers
15068 views

List of lists changes reflected across sublists unexpectedly

I needed to create a list of lists in Python, so I typed the following: myList = [[1] * 4] * 3 The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then I changed one of t...
412
votes
17answers
56986 views

Why are mutable structs “evil”?

Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question). What's the actual problem with mutability and s...
134
votes
15answers
203795 views

Immutable vs Mutable types

I'm confused on what an immutable type is. I know the float object is considered to be immutable, with this type of example from my book: class RoundFloat(float): def __new__(cls, val): r...
148
votes
10answers
70706 views

Mutable vs immutable objects

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 wha...
177
votes
18answers
36940 views

Why can't strings be mutable in Java and .NET?

Why is it that they decided to make string immutable in Java and .NET (and some other languages)? Why didn't they make it mutable?
38
votes
7answers
14410 views

Are mutable hashmap keys a dangerous practice?

Is it bad practice to use mutable objects as Hashmap keys? What happens when you try to retrieve a value from a Hashmap using a key that has been modified enough to change its hashcode? For example,...
445
votes
18answers
176194 views

Does the 'mutable' keyword have any purpose other than allowing the variable to be modified by a const function?

A while ago I came across some code that marked a member variable of a class with the mutable keyword. As far as I can see it simply allows you to modify a variable in a const method: class Foo { ...
11
votes
5answers
1197 views

Generating sublists using multiplication ( * ) unexpected behavior

I'm sure this has been answered somewhere but I wasn't sure how to describe it. Let's say I want to create a list containing 3 empty lists, like so: lst = [[], [], []] I thought I was being all cl...
8
votes
3answers
463 views

Obtaining a mutable reference by iterating a recursive structure

I'm trying to navigate a recursive data structure iteratively in order to insert elements at a certain position. To my limited understanding, this means taking a mutable reference to the root of the s...
74
votes
10answers
49516 views

Is Integer Immutable

I know this is probably very stupid, but a lot of places claim that the Integer class in Java is immutable, yet the following code: Integer a=3; Integer b=3; a+=b; System.out.println(a); Executes w...

Previous Next