immutability

Is it possible to make wrapper object for numbers, e.g. float, to make it mutable?

Is it possible to make wrapper object for numbers, e.g. float, to make it mutable? Question: In Python 3 everything is supposed to be an object, even numbers, but they’re immutable. Is it possible to create wrapper object for numbers, e.g. float, such that it would behave exactly as ordinary numbers except it must be …

Total answers: 2

About the changing id of an immutable string

About the changing id of an immutable string Question: Something about the id of objects of type str (in python 2.7) puzzles me. The str type is immutable, so I would expect that once it is created, it will always have the same id. I believe I don’t phrase myself so well, so instead I’ll …

Total answers: 5

python tuple is immutable – so why can I add elements to it

python tuple is immutable – so why can I add elements to it Question: I’ve been using Python for some time already and today while reading the following code snippet: >>> a = (1,2) >>> a += (3,4) >>> a (1, 2, 3, 4) I asked myself a question: how come python tuples are immutable …

Total answers: 4

are user defined classes mutable

are user defined classes mutable Question: Say I want to create a class for car, tractor and boat. All these classes have an instance of engine and I want to keep track of all the engines in a single list. If I understand correctly if the motor object is mutable i can store it as …

Total answers: 4

Does Python have an immutable list?

Does Python have an immutable list? Question: Does python have immutable lists? Suppose I wish to have the functionality of an ordered collection of elements, but which I want to guarantee will not change, how can this be implemented? Lists are ordered but they can be mutated. Asked By: cammil || Source Answers: Yes. It’s …

Total answers: 7

How to create an immutable dictionary in python?

How to create an immutable dictionary in python? Question: I want to subclass dict in python such that all the dictionaries of the sub-class are immutable. I don’t understand how does __hash__ affects the immutability, since in my understanding it just signifies the equality or non-equality of objects ! So, can __hash__ be used to …

Total answers: 7

Why can tuples contain mutable items?

Why can tuples contain mutable items? Question: 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. Asked By: qazwsx || Source Answers: That’s an excellent question. The …

Total answers: 8

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

Aren't Python strings immutable? Then why does a + " " + b work? Question: 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 + ” ” …

Total answers: 22

Why are Python strings immutable? Best practices for using them

Why are Python strings immutable? Best practices for using them Question: What are the design reasons of making Python strings immutable? How does it make programming easier? I’m used to mutable strings, like the ones in C. How am I supposed to program without mutable strings? Are there any best practices? Asked By: Sergey || …

Total answers: 7