How can I omit temporary variable at loop in Python? [duplicate] - python

If I have a list comprehension (for example) like this:
['' for x in myList]
Effectively making a new list that has an empty string for every element in a list, I never use the x. Is there a cleaner way of writing this so I don't have to declare the unused x variable?

_ is a standard placeholder name for ignored members in a for-loop and tuple assignment, e.g.
['' for _ in myList]
[a+d for a, _, _, d, _ in fiveTuples]
BTW your list could be written without list comprehension (assuming you want to make a list of immutable members like strings, integers etc.).
[''] * len(myList)

No. As the Zen puts it: Special cases aren't special enough to break the rules. The special case being loops not using the items of the thing being iterated and the rule being that there's a "target" to unpack to.
You can, however, use _ as variable name, which is usually understood as "intentionally unused" (even PyLint etc. knows and respect this).

It turns out that using dummy* (starting word is dummy) as the variable name does the same trick as _. _ is a known standard and it would be better to use meaningful variable names. So you can use dummy, dummy1, dummy_anything. By using these variable names PyLint won't complain.

Add the following comment after the for loop on the same line:
#pylint: disable=unused-variable
for i in range(100): #pylint: disable=unused-variable

If you need to name your arguments (in case, for example, when writing mocks that don't use certain arguments that are referenced by name), you can add this shortcut method:
def UnusedArgument(_):
pass
and then use it like this
def SomeMethod(name_should_remain):
UnusedArgument(name_should_remain)

The generator objects don't actually use the variables. So something like
list(('' for x in myList))
should do the trick. Note that x is not defined as a variable outside of the generator comprehension.

You can also prepend a variable name with _ if you prefer giving the variable a human readable name. For example you can use _foo, _foo1, _anything and PyLint won't complain. In a for loop, it would be like:
for _something in range(10):
do_something_else()
edit: Add example

A verbose way is:
newList = []
while len(newList) < len(mylist):
newList.append('')
You avoid declaring an used variable this way.
Also you can append both mutable and immutable objects (like dictionaries) into newList.
Another thing for python newbies like me, '_', 'dummy' are a bit disconcerting.

Try it, it's simple:
# Use '_' instead of the variable
for _ in range(any_number):
do_somthing()

Comment to How can I get around declaring an unused variable in a for loop? (Ran out of comment size)
Python maintains the same reference for the object created. (irrespective of mutability),for example,
In [1]: i = 1
In [2]: j = 1
In [3]: id(i)
Out[3]: 142671248
In [4]: id(j)
Out[4]: 142671248
You, can see both i and j, refer to the same object in memory.What happens, when we change the value of one immutable variable.
In [5]: j = j+1
In [6]: id(i)
Out[6]: 142671248
In [7]: id(j)
Out[7]: 142671236
you can see j now starts to point a new location, (where 2 is stored), and i still points to location where 1 is stored.
While evaluating,
j = j+1
The value is picked from 142671248, calculated(if not already cached), and put at a new location 142671236. j is made to point to
the new location. In simpler terms a new copy made everytime an immutable variable is modified.
Mutability
Mutable objects act little different in this regard. When the value pointed by
In [16]: a = []
In [17]: b = a
In [18]: id(a)
Out[18]: 3071546412L
In [19]: id(b)
Out[19]: 3071546412L
Both a and b point to the same memory location.
In [20]: a.append(5)
Memory location pointed by a is modified.
In [21]: a
Out[21]: [5]
In [22]: b
Out[22]: [5]
In [23]: id(a)
Out[23]: 3071546412L
In [24]: id(b)
Out[24]: 3071546412L
Both a and b, still point to the same memory location. In other word, mutable variables act of the same memory location pointed by the variable, instead of making a copy of the value pointed by the variable, like in immutable variable case.

Related

In which ways are Python strings lists? [duplicate]

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):
return float.__new__(cls, round(val, 2))
Is this considered to be immutable because of the class structure / hierarchy?, meaning float is at the top of the class and is its own method call. Similar to this type of example (even though my book says dict is mutable):
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
Whereas something mutable has methods inside the class, with this type of example:
class SortedKeyDict_a(dict):
def example(self):
return self.keys()
Also, for the last class(SortedKeyDict_a), if I pass this type of set to it:
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
without calling the example method, it returns a dictionary. The SortedKeyDict with __new__ flags it as an error. I tried passing integers to the RoundFloat class with __new__ and it flagged no errors.
What? Floats are immutable? But can't I do
x = 5.0
x += 7.0
print x # 12.0
Doesn't that "mut" x?
Well you agree strings are immutable right? But you can do the same thing.
s = 'foo'
s += 'bar'
print s # foobar
The value of the variable changes, but it changes by changing what the variable refers to. A mutable type can change that way, and it can also change "in place".
Here is the difference.
x = something # immutable type
print x
func(x)
print x # prints the same thing
x = something # mutable type
print x
func(x)
print x # might print something different
x = something # immutable type
y = x
print x
# some statement that operates on y
print x # prints the same thing
x = something # mutable type
y = x
print x
# some statement that operates on y
print x # might print something different
Concrete examples
x = 'foo'
y = x
print x # foo
y += 'bar'
print x # foo
x = [1, 2, 3]
y = x
print x # [1, 2, 3]
y += [3, 2, 1]
print x # [1, 2, 3, 3, 2, 1]
def func(val):
val += 'bar'
x = 'foo'
print x # foo
func(x)
print x # foo
def func(val):
val += [3, 2, 1]
x = [1, 2, 3]
print x # [1, 2, 3]
func(x)
print x # [1, 2, 3, 3, 2, 1]
You have to understand that Python represents all its data as objects. Some of these objects like lists and dictionaries are mutable, meaning you can change their content without changing their identity. Other objects like integers, floats, strings and tuples are objects that can not be changed.
An easy way to understand that is if you have a look at an objects ID.
Below you see a string that is immutable. You can not change its content. It will raise a TypeError if you try to change it. Also, if we assign new content, a new object is created instead of the contents being modified.
>>> s = "abc"
>>> id(s)
4702124
>>> s[0]
'a'
>>> s[0] = "o"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = "xyz"
>>> id(s)
4800100
>>> s += "uvw"
>>> id(s)
4800500
You can do that with a list and it will not change the objects identity
>>> i = [1,2,3]
>>> id(i)
2146718700
>>> i[0]
1
>>> i[0] = 7
>>> id(i)
2146718700
To read more about Python's data model you could have a look at the Python language reference:
Python 2 datamodel
Python 3 datamodel
Common immutable type:
numbers: int(), float(), complex()
immutable sequences: str(), tuple(), frozenset(), bytes()
Common mutable type (almost everything else):
mutable sequences: list(), bytearray()
set type: set()
mapping type: dict()
classes, class instances
etc.
One trick to quickly test if a type is mutable or not, is to use id() built-in function.
Examples, using on integer,
>>> i = 1
>>> id(i)
***704
>>> i += 1
>>> i
2
>>> id(i)
***736 (different from ***704)
using on list,
>>> a = [1]
>>> id(a)
***416
>>> a.append(2)
>>> a
[1, 2]
>>> id(a)
***416 (same with the above id)
First of all, whether a class has methods or what it's class structure is has nothing to do with mutability.
ints and floats are immutable. If I do
a = 1
a += 5
It points the name a at a 1 somewhere in memory on the first line. On the second line, it looks up that 1, adds 5, gets 6, then points a at that 6 in memory -- it didn't change the 1 to a 6 in any way. The same logic applies to the following examples, using other immutable types:
b = 'some string'
b += 'some other string'
c = ('some', 'tuple')
c += ('some', 'other', 'tuple')
For mutable types, I can do thing that actallly change the value where it's stored in memory. With:
d = [1, 2, 3]
I've created a list of the locations of 1, 2, and 3 in memory. If I then do
e = d
I just point e to the same list d points at. I can then do:
e += [4, 5]
And the list that both e and d points at will be updated to also have the locations of 4 and 5 in memory.
If I go back to an immutable type and do that with a tuple:
f = (1, 2, 3)
g = f
g += (4, 5)
Then f still only points to the original tuple -- you've pointed g at an entirely new tuple.
Now, with your example of
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
Where you pass
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
(which is a tuple of tuples) as val, you're getting an error because tuples don't have a .clear() method -- you'd have to pass dict(d) as val for it to work, in which case you'll get an empty SortedKeyDict as a result.
Difference between Mutable and Immutable objects
Definitions
Mutable object: Object that can be changed after creating it.
Immutable object: Object that cannot be changed after creating it.
In Python if you change the value of the immutable object it will create a new object.
Mutable Objects
Here are the objects in Python that are of mutable type:
list
Dictionary
Set
bytearray
user defined classes
Immutable Objects
Here are the objects in Python that are of immutable type:
int
float
decimal
complex
bool
string
tuple
range
frozenset
bytes
Some Unanswered Questions
Question: Is string an immutable type?
Answer: yes it is, but can you explain this:
Proof 1:
a = "Hello"
a +=" World"
print a
Output
"Hello World"
In the above example the string got once created as "Hello" then changed to "Hello World". This implies that the string is of the mutable type. But it is not when we check its identity to see whether it is of a mutable type or not.
a = "Hello"
identity_a = id(a)
a += " World"
new_identity_a = id(a)
if identity_a != new_identity_a:
print "String is Immutable"
Output
String is Immutable
Proof 2:
a = "Hello World"
a[0] = "M"
Output
TypeError 'str' object does not support item assignment
Question: Is Tuple an immutable type?
Answer: yes, it is.
Proof 1:
tuple_a = (1,)
tuple_a[0] = (2,)
print a
Output
'tuple' object does not support item assignment
If you're coming to Python from another language (except one that's a lot like Python, like Ruby), and insist on understanding it in terms of that other language, here's where people usually get confused:
>>> a = 1
>>> a = 2 # I thought int was immutable, but I just changed it?!
In Python, assignment is not mutation in Python.
In C++, if you write a = 2, you're calling a.operator=(2), which will mutate the object stored in a. (And if there was no object stored in a, that's an error.)
In Python, a = 2 does nothing to whatever was stored in a; it just means that 2 is now stored in a instead. (And if there was no object stored in a, that's fine.)
Ultimately, this is part of an even deeper distinction.
A variable in a language like C++ is a typed location in memory. If a is an int, that means it's 4 bytes somewhere that the compiler knows is supposed to be interpreted as an int. So, when you do a = 2, it changes what's stored in those 4 bytes of memory from 0, 0, 0, 1 to 0, 0, 0, 2. If there's another int variable somewhere else, it has its own 4 bytes.
A variable in a language like Python is a name for an object that has a life of its own. There's an object for the number 1, and another object for the number 2. And a isn't 4 bytes of memory that are represented as an int, it's just a name that points at the 1 object. It doesn't make sense for a = 2 to turn the number 1 into the number 2 (that would give any Python programmer way too much power to change the fundamental workings of the universe); what it does instead is just make a forget the 1 object and point at the 2 object instead.
So, if assignment isn't a mutation, what is a mutation?
Calling a method that's documented to mutate, like a.append(b). (Note that these methods almost always return None). Immutable types do not have any such methods, mutable types usually do.
Assigning to a part of the object, like a.spam = b or a[0] = b. Immutable types do not allow assignment to attributes or elements, mutable types usually allow one or the other.
Sometimes using augmented assignment, like a += b, sometimes not. Mutable types usually mutate the value; immutable types never do, and give you a copy instead (they calculate a + b, then assign the result to a).
But if assignment isn't mutation, how is assigning to part of the object mutation? That's where it gets tricky. a[0] = b does not mutate a[0] (again, unlike C++), but it does mutate a (unlike C++, except indirectly).
All of this is why it's probably better not to try to put Python's semantics in terms of a language you're used to, and instead learn Python's semantics on their own terms.
Whether an object is mutable or not depends on its type. This doesn't depend on whether or not it has certain methods, nor on the structure of the class hierarchy.
User-defined types (i.e. classes) are generally mutable. There are some exceptions, such as simple sub-classes of an immutable type. Other immutable types include some built-in types such as int, float, tuple and str, as well as some Python classes implemented in C.
A general explanation from the "Data Model" chapter in the Python Language Reference":
The value of some objects can change. Objects whose value can change
are said to be mutable; objects whose value is unchangeable once they
are created are called immutable.
(The value of an immutable container
object that contains a reference to a mutable object can change when
the latter’s value is changed; however the container is still
considered immutable, because the collection of objects it contains
cannot be changed. So, immutability is not strictly the same as having
an unchangeable value, it is more subtle.)
An object’s mutability is
determined by its type; for instance, numbers, strings and tuples are
immutable, while dictionaries and lists are mutable.
A mutable object has to have at least a method able to mutate the object. For example, the list object has the append method, which will actually mutate the object:
>>> a = [1,2,3]
>>> a.append('hello') # `a` has mutated but is still the same object
>>> a
[1, 2, 3, 'hello']
but the class float has no method to mutate a float object. You can do:
>>> b = 5.0
>>> b = b + 0.1
>>> b
5.1
but the = operand is not a method. It just make a bind between the variable and whatever is to the right of it, nothing else. It never changes or creates objects. It is a declaration of what the variable will point to, since now on.
When you do b = b + 0.1 the = operand binds the variable to a new float, wich is created with te result of 5 + 0.1.
When you assign a variable to an existent object, mutable or not, the = operand binds the variable to that object. And nothing more happens
In either case, the = just make the bind. It doesn't change or create objects.
When you do a = 1.0, the = operand is not wich create the float, but the 1.0 part of the line. Actually when you write 1.0 it is a shorthand for float(1.0) a constructor call returning a float object. (That is the reason why if you type 1.0 and press enter you get the "echo" 1.0 printed below; that is the return value of the constructor function you called)
Now, if b is a float and you assign a = b, both variables are pointing to the same object, but actually the variables can't comunicate betweem themselves, because the object is inmutable, and if you do b += 1, now b point to a new object, and a is still pointing to the oldone and cannot know what b is pointing to.
but if c is, let's say, a list, and you assign a = c, now a and c can "comunicate", because list is mutable, and if you do c.append('msg'), then just checking a you get the message.
(By the way, every object has an unique id number asociated to, wich you can get with id(x). So you can check if an object is the same or not checking if its unique id has changed.)
A class is immutable if each object of that class has a fixed value upon instantiation that cannot SUBSEQUENTLY be changed
In another word change the entire value of that variable (name) or leave it alone.
Example:
my_string = "Hello world"
my_string[0] = "h"
print my_string
you expected this to work and print hello world but this will throw the following error:
Traceback (most recent call last):
File "test.py", line 4, in <module>
my_string[0] = "h"
TypeError: 'str' object does not support item assignment
The interpreter is saying : i can't change the first character of this string
you will have to change the whole string in order to make it works:
my_string = "Hello World"
my_string = "hello world"
print my_string #hello world
check this table:
source
It would seem to me that you are fighting with the question what mutable/immutable actually means. So here is a simple explenation:
First we need a foundation to base the explenation on.
So think of anything that you program as a virtual object, something that is saved in a computers memory as a sequence of binary numbers. (Don't try to imagine this too hard, though.^^) Now in most computer languages you will not work with these binary numbers directly, but rather more you use an interpretation of binary numbers.
E.g. you do not think about numbers like 0x110, 0xaf0278297319 or similar, but instead you think about numbers like 6 or Strings like "Hello, world". Never the less theses numbers or Strings are an interpretation of a binary number in the computers memory. The same is true for any value of a variable.
In short: We do not program with actual values but with interpretations of actual binary values.
Now we do have interpretations that must not be changed for the sake of logic and other "neat stuff" while there are interpretations that may well be changed. For example think of the simulation of a city, in other words a program where there are many virtual objects and some of these are houses. Now may these virtual objects (the houses) be changed and can they still be considered to be the same houses? Well of course they can. Thus they are mutable: They can be changed without becoming a "completely" different object.
Now think of integers: These also are virtual objects (sequences of binary numbers in a computers memory). So if we change one of them, like incrementing the value six by one, is it still a six? Well of course not. Thus any integer is immutable.
So: If any change in a virtual object means that it actually becomes another virtual object, then it is called immutable.
Final remarks:
(1) Never mix up your real-world experience of mutable and immutable with programming in a certain language:
Every programming language has a definition of its own on which objects may be muted and which ones may not.
So while you may now understand the difference in meaning, you still have to learn the actual implementation for each programming language. ... Indeed there might be a purpose of a language where a 6 may be muted to become a 7. Then again this would be quite some crazy or interesting stuff, like simulations of parallel universes.^^
(2) This explenation is certainly not scientific, it is meant to help you to grasp the difference between mutable and immutable.
The goal of this answer is to create a single place to find all the good ideas about how to tell if you are dealing with mutating/nonmutating (immutable/mutable), and where possible, what to do about it? There are times when mutation is undesirable and python's behavior in this regard can feel counter-intuitive to coders coming into it from other languages.
As per a useful post by #mina-gabriel:
Books to read that might help: "Data Structures and Algorithms in Python"
Excerpt from that book that lists mutable/immutable types:
mutable/imutable types image
Analyzing the above and combining w/ a post by #arrakëën:
What cannot change unexpectedly?
scalars (variable types storing a single value) do not change unexpectedly
numeric examples: int(), float(), complex()
there are some "mutable sequences":
str(), tuple(), frozenset(), bytes()
What can?
list like objects (lists, dictionaries, sets, bytearray())
a post on here also says classes and class instances but this may depend on what the class inherits from and/or how its built.
by "unexpectedly" I mean that programmers from other languages might not expect this behavior (with the exception or Ruby, and maybe a few other "Python like" languages).
Adding to this discussion:
This behavior is an advantage when it prevents you from accidentally populating your code with mutliple copies of memory-eating large data structures. But when this is undesirable, how do we get around it?
With lists, the simple solution is to build a new one like so:
list2 = list(list1)
with other structures ... the solution can be trickier. One way is to loop through the elements and add them to a new empty data structure (of the same type).
functions can mutate the original when you pass in mutable structures. How to tell?
There are some tests given on other comments on this thread but then there are comments indicating these tests are not full proof
object.function() is a method of the original object but only some of these mutate. If they return nothing, they probably do. One would expect .append() to mutate without testing it given its name. .union() returns the union of set1.union(set2) and does not mutate. When in doubt, the function can be checked for a return value. If return = None, it does not mutate.
sorted() might be a workaround in some cases. Since it returns a sorted version of the original, it can allow you to store a non-mutated copy before you start working on the original in other ways. However, this option assumes you don't care about the order of the original elements (if you do, you need to find another way). In contrast .sort() mutates the original (as one might expect).
Non-standard Approaches (in case helpful):
Found this on github published under an MIT license:
github repository under: tobgu named: pyrsistent
What it is: Python persistent data structure code written to be used in place of core data structures when mutation is undesirable
For custom classes, #semicolon suggests checking if there is a __hash__ function because mutable objects should generally not have a __hash__() function.
This is all I have amassed on this topic for now. Other ideas, corrections, etc. are welcome. Thanks.
One way of thinking of the difference:
Assignments to immutable objects in python can be thought of as deep copies,
whereas assignments to mutable objects are shallow
The simplest answer:
A mutable variable is one whose value may change in place, whereas in an immutable variable change of value will not happen in place. Modifying an immutable variable will rebuild the same variable.
Example:
>>>x = 5
Will create a value 5 referenced by x
x -> 5
>>>y = x
This statement will make y refer to 5 of x
x -------------> 5 <-----------y
>>>x = x + y
As x being an integer (immutable type) has been rebuild.
In the statement, the expression on RHS will result into value 10 and when this is assigned to LHS (x), x will rebuild to 10. So now
x--------->10
y--------->5
Mutable means that it can change/mutate. Immutable the opposite.
Some Python data types are mutable, others not.
Let's find what are the types that fit in each category and see some examples.
Mutable
In Python there are various mutable types:
lists
dict
set
Let's see the following example for lists.
list = [1, 2, 3, 4, 5]
If I do the following to change the first element
list[0] = '!'
#['!', '2', '3', '4', '5']
It works just fine, as lists are mutable.
If we consider that list, that was changed, and assign a variable to it
y = list
And if we change an element from the list such as
list[0] = 'Hello'
#['Hello', '2', '3', '4', '5']
And if one prints y it will give
['Hello', '2', '3', '4', '5']
As both list and y are referring to the same list, and we have changed the list.
Immutable
In some programming languages one can define a constant such as the following
const a = 10
And if one calls, it would give an error
a = 20
However, that doesn't exist in Python.
In Python, however, there are various immutable types:
None
bool
int
float
str
tuple
Let's see the following example for strings.
Taking the string a
a = 'abcd'
We can get the first element with
a[0]
#'a'
If one tries to assign a new value to the element in the first position
a[0] = '!'
It will give an error
'str' object does not support item assignment
When one says += to a string, such as
a += 'e'
#'abcde'
It doesn't give an error, because it is pointing a to a different string.
It would be the same as the following
a = a + 'f'
And not changing the string.
Some Pros and Cons of being immutable
• The space in memory is known from the start. It would not require extra space.
• Usually, it makes things more efficiently. Finding, for example, the len() of a string is much faster, as it is part of the string object.
Every time we change value of a immutable variable it basically destroy the previous instance and create a new instance of variable class
var = 2 #Immutable data
print(id(var))
var += 4
print(id(var))
list_a = [1,2,3] #Mutable data
print(id(list_a))
list_a[0]= 4
print(id(list_a))
Output:
9789024
9789088
140010877705856
140010877705856
Note:Mutable variable memory_location is change when we change the value
I haven't read all the answers, but the selected answer is not correct and I think the author has an idea that being able to reassign a variable means that whatever datatype is mutable. That is not the case. Mutability has to do with passing by reference rather than passing by value.
Lets say you created a List
a = [1,2]
If you were to say:
b = a
b[1] = 3
Even though you reassigned a value on B, it will also reassign the value on a. Its because when you assign "b = a". You are passing the "Reference" to the object rather than a copy of the value. This is not the case with strings, floats etc. This makes list, dictionaries and the likes mutable, but booleans, floats etc immutable.
In Python, there's a easy way to know:
Immutable:
>>> s='asd'
>>> s is 'asd'
True
>>> s=None
>>> s is None
True
>>> s=123
>>> s is 123
True
Mutable:
>>> s={}
>>> s is {}
False
>>> {} is {}
Flase
>>> s=[1,2]
>>> s is [1,2]
False
>>> s=(1,2)
>>> s is (1,2)
False
And:
>>> s=abs
>>> s is abs
True
So I think built-in function is also immutable in Python.
But I really don't understand how float works:
>>> s=12.3
>>> s is 12.3
False
>>> 12.3 is 12.3
True
>>> s == 12.3
True
>>> id(12.3)
140241478380112
>>> id(s)
140241478380256
>>> s=12.3
>>> id(s)
140241478380112
>>> id(12.3)
140241478380256
>>> id(12.3)
140241478380256
It's so weird.
For immutable objects, assignment creates a new copy of values, for example.
x=7
y=x
print(x,y)
x=10 # so for immutable objects this creates a new copy so that it doesnot
#effect the value of y
print(x,y)
For mutable objects, the assignment doesn't create another copy of values. For example,
x=[1,2,3,4]
print(x)
y=x #for immutable objects assignment doesn't create new copy
x[2]=5
print(x,y) # both x&y holds the same list

Strange behaviour when passing objects into classes [duplicate]

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):
return float.__new__(cls, round(val, 2))
Is this considered to be immutable because of the class structure / hierarchy?, meaning float is at the top of the class and is its own method call. Similar to this type of example (even though my book says dict is mutable):
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
Whereas something mutable has methods inside the class, with this type of example:
class SortedKeyDict_a(dict):
def example(self):
return self.keys()
Also, for the last class(SortedKeyDict_a), if I pass this type of set to it:
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
without calling the example method, it returns a dictionary. The SortedKeyDict with __new__ flags it as an error. I tried passing integers to the RoundFloat class with __new__ and it flagged no errors.
What? Floats are immutable? But can't I do
x = 5.0
x += 7.0
print x # 12.0
Doesn't that "mut" x?
Well you agree strings are immutable right? But you can do the same thing.
s = 'foo'
s += 'bar'
print s # foobar
The value of the variable changes, but it changes by changing what the variable refers to. A mutable type can change that way, and it can also change "in place".
Here is the difference.
x = something # immutable type
print x
func(x)
print x # prints the same thing
x = something # mutable type
print x
func(x)
print x # might print something different
x = something # immutable type
y = x
print x
# some statement that operates on y
print x # prints the same thing
x = something # mutable type
y = x
print x
# some statement that operates on y
print x # might print something different
Concrete examples
x = 'foo'
y = x
print x # foo
y += 'bar'
print x # foo
x = [1, 2, 3]
y = x
print x # [1, 2, 3]
y += [3, 2, 1]
print x # [1, 2, 3, 3, 2, 1]
def func(val):
val += 'bar'
x = 'foo'
print x # foo
func(x)
print x # foo
def func(val):
val += [3, 2, 1]
x = [1, 2, 3]
print x # [1, 2, 3]
func(x)
print x # [1, 2, 3, 3, 2, 1]
You have to understand that Python represents all its data as objects. Some of these objects like lists and dictionaries are mutable, meaning you can change their content without changing their identity. Other objects like integers, floats, strings and tuples are objects that can not be changed.
An easy way to understand that is if you have a look at an objects ID.
Below you see a string that is immutable. You can not change its content. It will raise a TypeError if you try to change it. Also, if we assign new content, a new object is created instead of the contents being modified.
>>> s = "abc"
>>> id(s)
4702124
>>> s[0]
'a'
>>> s[0] = "o"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = "xyz"
>>> id(s)
4800100
>>> s += "uvw"
>>> id(s)
4800500
You can do that with a list and it will not change the objects identity
>>> i = [1,2,3]
>>> id(i)
2146718700
>>> i[0]
1
>>> i[0] = 7
>>> id(i)
2146718700
To read more about Python's data model you could have a look at the Python language reference:
Python 2 datamodel
Python 3 datamodel
Common immutable type:
numbers: int(), float(), complex()
immutable sequences: str(), tuple(), frozenset(), bytes()
Common mutable type (almost everything else):
mutable sequences: list(), bytearray()
set type: set()
mapping type: dict()
classes, class instances
etc.
One trick to quickly test if a type is mutable or not, is to use id() built-in function.
Examples, using on integer,
>>> i = 1
>>> id(i)
***704
>>> i += 1
>>> i
2
>>> id(i)
***736 (different from ***704)
using on list,
>>> a = [1]
>>> id(a)
***416
>>> a.append(2)
>>> a
[1, 2]
>>> id(a)
***416 (same with the above id)
First of all, whether a class has methods or what it's class structure is has nothing to do with mutability.
ints and floats are immutable. If I do
a = 1
a += 5
It points the name a at a 1 somewhere in memory on the first line. On the second line, it looks up that 1, adds 5, gets 6, then points a at that 6 in memory -- it didn't change the 1 to a 6 in any way. The same logic applies to the following examples, using other immutable types:
b = 'some string'
b += 'some other string'
c = ('some', 'tuple')
c += ('some', 'other', 'tuple')
For mutable types, I can do thing that actallly change the value where it's stored in memory. With:
d = [1, 2, 3]
I've created a list of the locations of 1, 2, and 3 in memory. If I then do
e = d
I just point e to the same list d points at. I can then do:
e += [4, 5]
And the list that both e and d points at will be updated to also have the locations of 4 and 5 in memory.
If I go back to an immutable type and do that with a tuple:
f = (1, 2, 3)
g = f
g += (4, 5)
Then f still only points to the original tuple -- you've pointed g at an entirely new tuple.
Now, with your example of
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
Where you pass
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
(which is a tuple of tuples) as val, you're getting an error because tuples don't have a .clear() method -- you'd have to pass dict(d) as val for it to work, in which case you'll get an empty SortedKeyDict as a result.
Difference between Mutable and Immutable objects
Definitions
Mutable object: Object that can be changed after creating it.
Immutable object: Object that cannot be changed after creating it.
In Python if you change the value of the immutable object it will create a new object.
Mutable Objects
Here are the objects in Python that are of mutable type:
list
Dictionary
Set
bytearray
user defined classes
Immutable Objects
Here are the objects in Python that are of immutable type:
int
float
decimal
complex
bool
string
tuple
range
frozenset
bytes
Some Unanswered Questions
Question: Is string an immutable type?
Answer: yes it is, but can you explain this:
Proof 1:
a = "Hello"
a +=" World"
print a
Output
"Hello World"
In the above example the string got once created as "Hello" then changed to "Hello World". This implies that the string is of the mutable type. But it is not when we check its identity to see whether it is of a mutable type or not.
a = "Hello"
identity_a = id(a)
a += " World"
new_identity_a = id(a)
if identity_a != new_identity_a:
print "String is Immutable"
Output
String is Immutable
Proof 2:
a = "Hello World"
a[0] = "M"
Output
TypeError 'str' object does not support item assignment
Question: Is Tuple an immutable type?
Answer: yes, it is.
Proof 1:
tuple_a = (1,)
tuple_a[0] = (2,)
print a
Output
'tuple' object does not support item assignment
If you're coming to Python from another language (except one that's a lot like Python, like Ruby), and insist on understanding it in terms of that other language, here's where people usually get confused:
>>> a = 1
>>> a = 2 # I thought int was immutable, but I just changed it?!
In Python, assignment is not mutation in Python.
In C++, if you write a = 2, you're calling a.operator=(2), which will mutate the object stored in a. (And if there was no object stored in a, that's an error.)
In Python, a = 2 does nothing to whatever was stored in a; it just means that 2 is now stored in a instead. (And if there was no object stored in a, that's fine.)
Ultimately, this is part of an even deeper distinction.
A variable in a language like C++ is a typed location in memory. If a is an int, that means it's 4 bytes somewhere that the compiler knows is supposed to be interpreted as an int. So, when you do a = 2, it changes what's stored in those 4 bytes of memory from 0, 0, 0, 1 to 0, 0, 0, 2. If there's another int variable somewhere else, it has its own 4 bytes.
A variable in a language like Python is a name for an object that has a life of its own. There's an object for the number 1, and another object for the number 2. And a isn't 4 bytes of memory that are represented as an int, it's just a name that points at the 1 object. It doesn't make sense for a = 2 to turn the number 1 into the number 2 (that would give any Python programmer way too much power to change the fundamental workings of the universe); what it does instead is just make a forget the 1 object and point at the 2 object instead.
So, if assignment isn't a mutation, what is a mutation?
Calling a method that's documented to mutate, like a.append(b). (Note that these methods almost always return None). Immutable types do not have any such methods, mutable types usually do.
Assigning to a part of the object, like a.spam = b or a[0] = b. Immutable types do not allow assignment to attributes or elements, mutable types usually allow one or the other.
Sometimes using augmented assignment, like a += b, sometimes not. Mutable types usually mutate the value; immutable types never do, and give you a copy instead (they calculate a + b, then assign the result to a).
But if assignment isn't mutation, how is assigning to part of the object mutation? That's where it gets tricky. a[0] = b does not mutate a[0] (again, unlike C++), but it does mutate a (unlike C++, except indirectly).
All of this is why it's probably better not to try to put Python's semantics in terms of a language you're used to, and instead learn Python's semantics on their own terms.
Whether an object is mutable or not depends on its type. This doesn't depend on whether or not it has certain methods, nor on the structure of the class hierarchy.
User-defined types (i.e. classes) are generally mutable. There are some exceptions, such as simple sub-classes of an immutable type. Other immutable types include some built-in types such as int, float, tuple and str, as well as some Python classes implemented in C.
A general explanation from the "Data Model" chapter in the Python Language Reference":
The value of some objects can change. Objects whose value can change
are said to be mutable; objects whose value is unchangeable once they
are created are called immutable.
(The value of an immutable container
object that contains a reference to a mutable object can change when
the latter’s value is changed; however the container is still
considered immutable, because the collection of objects it contains
cannot be changed. So, immutability is not strictly the same as having
an unchangeable value, it is more subtle.)
An object’s mutability is
determined by its type; for instance, numbers, strings and tuples are
immutable, while dictionaries and lists are mutable.
A mutable object has to have at least a method able to mutate the object. For example, the list object has the append method, which will actually mutate the object:
>>> a = [1,2,3]
>>> a.append('hello') # `a` has mutated but is still the same object
>>> a
[1, 2, 3, 'hello']
but the class float has no method to mutate a float object. You can do:
>>> b = 5.0
>>> b = b + 0.1
>>> b
5.1
but the = operand is not a method. It just make a bind between the variable and whatever is to the right of it, nothing else. It never changes or creates objects. It is a declaration of what the variable will point to, since now on.
When you do b = b + 0.1 the = operand binds the variable to a new float, wich is created with te result of 5 + 0.1.
When you assign a variable to an existent object, mutable or not, the = operand binds the variable to that object. And nothing more happens
In either case, the = just make the bind. It doesn't change or create objects.
When you do a = 1.0, the = operand is not wich create the float, but the 1.0 part of the line. Actually when you write 1.0 it is a shorthand for float(1.0) a constructor call returning a float object. (That is the reason why if you type 1.0 and press enter you get the "echo" 1.0 printed below; that is the return value of the constructor function you called)
Now, if b is a float and you assign a = b, both variables are pointing to the same object, but actually the variables can't comunicate betweem themselves, because the object is inmutable, and if you do b += 1, now b point to a new object, and a is still pointing to the oldone and cannot know what b is pointing to.
but if c is, let's say, a list, and you assign a = c, now a and c can "comunicate", because list is mutable, and if you do c.append('msg'), then just checking a you get the message.
(By the way, every object has an unique id number asociated to, wich you can get with id(x). So you can check if an object is the same or not checking if its unique id has changed.)
A class is immutable if each object of that class has a fixed value upon instantiation that cannot SUBSEQUENTLY be changed
In another word change the entire value of that variable (name) or leave it alone.
Example:
my_string = "Hello world"
my_string[0] = "h"
print my_string
you expected this to work and print hello world but this will throw the following error:
Traceback (most recent call last):
File "test.py", line 4, in <module>
my_string[0] = "h"
TypeError: 'str' object does not support item assignment
The interpreter is saying : i can't change the first character of this string
you will have to change the whole string in order to make it works:
my_string = "Hello World"
my_string = "hello world"
print my_string #hello world
check this table:
source
It would seem to me that you are fighting with the question what mutable/immutable actually means. So here is a simple explenation:
First we need a foundation to base the explenation on.
So think of anything that you program as a virtual object, something that is saved in a computers memory as a sequence of binary numbers. (Don't try to imagine this too hard, though.^^) Now in most computer languages you will not work with these binary numbers directly, but rather more you use an interpretation of binary numbers.
E.g. you do not think about numbers like 0x110, 0xaf0278297319 or similar, but instead you think about numbers like 6 or Strings like "Hello, world". Never the less theses numbers or Strings are an interpretation of a binary number in the computers memory. The same is true for any value of a variable.
In short: We do not program with actual values but with interpretations of actual binary values.
Now we do have interpretations that must not be changed for the sake of logic and other "neat stuff" while there are interpretations that may well be changed. For example think of the simulation of a city, in other words a program where there are many virtual objects and some of these are houses. Now may these virtual objects (the houses) be changed and can they still be considered to be the same houses? Well of course they can. Thus they are mutable: They can be changed without becoming a "completely" different object.
Now think of integers: These also are virtual objects (sequences of binary numbers in a computers memory). So if we change one of them, like incrementing the value six by one, is it still a six? Well of course not. Thus any integer is immutable.
So: If any change in a virtual object means that it actually becomes another virtual object, then it is called immutable.
Final remarks:
(1) Never mix up your real-world experience of mutable and immutable with programming in a certain language:
Every programming language has a definition of its own on which objects may be muted and which ones may not.
So while you may now understand the difference in meaning, you still have to learn the actual implementation for each programming language. ... Indeed there might be a purpose of a language where a 6 may be muted to become a 7. Then again this would be quite some crazy or interesting stuff, like simulations of parallel universes.^^
(2) This explenation is certainly not scientific, it is meant to help you to grasp the difference between mutable and immutable.
The goal of this answer is to create a single place to find all the good ideas about how to tell if you are dealing with mutating/nonmutating (immutable/mutable), and where possible, what to do about it? There are times when mutation is undesirable and python's behavior in this regard can feel counter-intuitive to coders coming into it from other languages.
As per a useful post by #mina-gabriel:
Books to read that might help: "Data Structures and Algorithms in Python"
Excerpt from that book that lists mutable/immutable types:
mutable/imutable types image
Analyzing the above and combining w/ a post by #arrakëën:
What cannot change unexpectedly?
scalars (variable types storing a single value) do not change unexpectedly
numeric examples: int(), float(), complex()
there are some "mutable sequences":
str(), tuple(), frozenset(), bytes()
What can?
list like objects (lists, dictionaries, sets, bytearray())
a post on here also says classes and class instances but this may depend on what the class inherits from and/or how its built.
by "unexpectedly" I mean that programmers from other languages might not expect this behavior (with the exception or Ruby, and maybe a few other "Python like" languages).
Adding to this discussion:
This behavior is an advantage when it prevents you from accidentally populating your code with mutliple copies of memory-eating large data structures. But when this is undesirable, how do we get around it?
With lists, the simple solution is to build a new one like so:
list2 = list(list1)
with other structures ... the solution can be trickier. One way is to loop through the elements and add them to a new empty data structure (of the same type).
functions can mutate the original when you pass in mutable structures. How to tell?
There are some tests given on other comments on this thread but then there are comments indicating these tests are not full proof
object.function() is a method of the original object but only some of these mutate. If they return nothing, they probably do. One would expect .append() to mutate without testing it given its name. .union() returns the union of set1.union(set2) and does not mutate. When in doubt, the function can be checked for a return value. If return = None, it does not mutate.
sorted() might be a workaround in some cases. Since it returns a sorted version of the original, it can allow you to store a non-mutated copy before you start working on the original in other ways. However, this option assumes you don't care about the order of the original elements (if you do, you need to find another way). In contrast .sort() mutates the original (as one might expect).
Non-standard Approaches (in case helpful):
Found this on github published under an MIT license:
github repository under: tobgu named: pyrsistent
What it is: Python persistent data structure code written to be used in place of core data structures when mutation is undesirable
For custom classes, #semicolon suggests checking if there is a __hash__ function because mutable objects should generally not have a __hash__() function.
This is all I have amassed on this topic for now. Other ideas, corrections, etc. are welcome. Thanks.
One way of thinking of the difference:
Assignments to immutable objects in python can be thought of as deep copies,
whereas assignments to mutable objects are shallow
The simplest answer:
A mutable variable is one whose value may change in place, whereas in an immutable variable change of value will not happen in place. Modifying an immutable variable will rebuild the same variable.
Example:
>>>x = 5
Will create a value 5 referenced by x
x -> 5
>>>y = x
This statement will make y refer to 5 of x
x -------------> 5 <-----------y
>>>x = x + y
As x being an integer (immutable type) has been rebuild.
In the statement, the expression on RHS will result into value 10 and when this is assigned to LHS (x), x will rebuild to 10. So now
x--------->10
y--------->5
Mutable means that it can change/mutate. Immutable the opposite.
Some Python data types are mutable, others not.
Let's find what are the types that fit in each category and see some examples.
Mutable
In Python there are various mutable types:
lists
dict
set
Let's see the following example for lists.
list = [1, 2, 3, 4, 5]
If I do the following to change the first element
list[0] = '!'
#['!', '2', '3', '4', '5']
It works just fine, as lists are mutable.
If we consider that list, that was changed, and assign a variable to it
y = list
And if we change an element from the list such as
list[0] = 'Hello'
#['Hello', '2', '3', '4', '5']
And if one prints y it will give
['Hello', '2', '3', '4', '5']
As both list and y are referring to the same list, and we have changed the list.
Immutable
In some programming languages one can define a constant such as the following
const a = 10
And if one calls, it would give an error
a = 20
However, that doesn't exist in Python.
In Python, however, there are various immutable types:
None
bool
int
float
str
tuple
Let's see the following example for strings.
Taking the string a
a = 'abcd'
We can get the first element with
a[0]
#'a'
If one tries to assign a new value to the element in the first position
a[0] = '!'
It will give an error
'str' object does not support item assignment
When one says += to a string, such as
a += 'e'
#'abcde'
It doesn't give an error, because it is pointing a to a different string.
It would be the same as the following
a = a + 'f'
And not changing the string.
Some Pros and Cons of being immutable
• The space in memory is known from the start. It would not require extra space.
• Usually, it makes things more efficiently. Finding, for example, the len() of a string is much faster, as it is part of the string object.
Every time we change value of a immutable variable it basically destroy the previous instance and create a new instance of variable class
var = 2 #Immutable data
print(id(var))
var += 4
print(id(var))
list_a = [1,2,3] #Mutable data
print(id(list_a))
list_a[0]= 4
print(id(list_a))
Output:
9789024
9789088
140010877705856
140010877705856
Note:Mutable variable memory_location is change when we change the value
I haven't read all the answers, but the selected answer is not correct and I think the author has an idea that being able to reassign a variable means that whatever datatype is mutable. That is not the case. Mutability has to do with passing by reference rather than passing by value.
Lets say you created a List
a = [1,2]
If you were to say:
b = a
b[1] = 3
Even though you reassigned a value on B, it will also reassign the value on a. Its because when you assign "b = a". You are passing the "Reference" to the object rather than a copy of the value. This is not the case with strings, floats etc. This makes list, dictionaries and the likes mutable, but booleans, floats etc immutable.
In Python, there's a easy way to know:
Immutable:
>>> s='asd'
>>> s is 'asd'
True
>>> s=None
>>> s is None
True
>>> s=123
>>> s is 123
True
Mutable:
>>> s={}
>>> s is {}
False
>>> {} is {}
Flase
>>> s=[1,2]
>>> s is [1,2]
False
>>> s=(1,2)
>>> s is (1,2)
False
And:
>>> s=abs
>>> s is abs
True
So I think built-in function is also immutable in Python.
But I really don't understand how float works:
>>> s=12.3
>>> s is 12.3
False
>>> 12.3 is 12.3
True
>>> s == 12.3
True
>>> id(12.3)
140241478380112
>>> id(s)
140241478380256
>>> s=12.3
>>> id(s)
140241478380112
>>> id(12.3)
140241478380256
>>> id(12.3)
140241478380256
It's so weird.
For immutable objects, assignment creates a new copy of values, for example.
x=7
y=x
print(x,y)
x=10 # so for immutable objects this creates a new copy so that it doesnot
#effect the value of y
print(x,y)
For mutable objects, the assignment doesn't create another copy of values. For example,
x=[1,2,3,4]
print(x)
y=x #for immutable objects assignment doesn't create new copy
x[2]=5
print(x,y) # both x&y holds the same list

python- why the list doesn't modified?

>>> c=[1,100,3]
>>>def nullity (lst):
lst=[]
>>>nullity (c)
>>>c
[1,100,3]
Why c doesn't return []? Isn't nullity(c) mean c=lst so thy both point now at []?
Python has pass-by-value semantics, meaning that when you pass a parameter to a function, it receives a copy to the object's reference, but not the object itself. So, if you reassign a function parameter to something else (as you did), the assignment is local to the function, the original object "outside" the function remains unchanged. A completely different situation happens if you change something inside the object:
def test(a):
a[0] = 0
lst = [1, 2, 3]
test(lst)
lst
=> [0, 2, 3]
In the above snippet the list's elements were changed, that's because both lst and a are pointing to the exact same object. Now back to the original question - notice that in Python 3.2 and below there isn't a clear() method (it was added in Python 3.3), here is a related discussion. But you can do this for the same effect, and it'll work in Python 2.x and 3.x:
def nullity(lst):
del lst[:]
You're reassigning local variable lst to a new empty list. To empty an existing list, you need to delete all its members:
del lst[:]
Lets use the id() function
In [14]: c=[1,2,3]
In [15]: def nullity (lst):
...: print id(lst)
...: lst=[]
...: print id(lst)
...:
In [16]: id(c)
Out[16]: 33590520
In [17]: nullity(c)
33590520
33591024
When doing the reassignment a new local object is created which is not the same as the one used when calling the function.
Python does not allow you to replace parameters. You are only changing what "lst" refers to within the function, but it does not affect the calling code.
Instead change the list itself by clearing it:
def nullity (lst):
lst.clear() # With Python >= 3.3

Behavior of colon operator in x = myList[:] vs myList[:] = x [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between slice assignment that slices the whole list and direct assignment?
I don't have money for school so I am teaching myself some Python whilst working shifts at a tollbooth on the highway (long nights with few customers). (BTW: Coursera should be translated to all languages...)
I have read here that if I have a list l:
l = ['a', '', 'b']
and I want to filter out empty strings like so:
l = [c for c in l if c]
or like so:
l = filter(lambda x: x, l)
it is advisable to do this instead:
l[:] = ... # either method 1 or method 2 above
not to "lose" the reference to the first l, especially in case other variables were pointing to it.
My question:
Why is it that l[:] denotes "the contents of l" in this case, allowing specifically reassignment to the "same" l, when elsewhere I think of it as a "same size slice", conveniently creating a copy of l for me?
Did I misunderstand how to use the l[:] for same-list-reassignments completely?
I thought that if I had an l and I asked for a l[:], the latter was an actual copy of the original l?
Reference: "Learning Python" -> There are a variety of
ways to copy a list, including using the built-in list function and the standard library
copy module. Perhaps the most common way is to slice from start to finish
Thanks!
There's a difference in Python between getting a slice of a list and setting a slice of a list. Indeed, they are actually separate operations (__getitem__ and __setitem__, respectively). So, what's true for the get case may not be true for set.
In the former case, l[:] means to get a copy of the list (it generates a brand new list which has all the same contents as the old list). In the latter case, l[:] = ... means to set the contents of the list to something else.
Because a LHS slice operator basically says enumerate through this list and assign something to each index, while a RHS operator says enumerate through this list, and create a new object with the values.
So lets see it in action:
LHS
l[:] = [c for c in l if c!=0] #ignore how stupid this list comprehension is
basically gets broken down into:
l[0] = l[0] if l[0] != 0
l[1] = l[1] if l[1] != 0
...
l[n] = l[n] if l[n] != 0
So because you're enumerating through it and assigning values, you're not creating any new objects -- just giving new values to the indices of l.
But, on the other hand, doing
l = anything
Changes the reference that the variable l holds. After that, it points to a totally different object in memory.
RHS
Now,
c = l[:]
will give you a copy of l, not a reference to l. This has to be true, because otherwise,
c = l[1:5]
would have to change the value of l if it were to try and use the same object reference as l.
When using a slice operator on the left-hand side of an assignment, you are telling python to store the sequence on the right-hand side in the selected elements of the list.
In other words, you are not replacing l, you are assigning to the elements within l.

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):
return float.__new__(cls, round(val, 2))
Is this considered to be immutable because of the class structure / hierarchy?, meaning float is at the top of the class and is its own method call. Similar to this type of example (even though my book says dict is mutable):
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
Whereas something mutable has methods inside the class, with this type of example:
class SortedKeyDict_a(dict):
def example(self):
return self.keys()
Also, for the last class(SortedKeyDict_a), if I pass this type of set to it:
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
without calling the example method, it returns a dictionary. The SortedKeyDict with __new__ flags it as an error. I tried passing integers to the RoundFloat class with __new__ and it flagged no errors.
What? Floats are immutable? But can't I do
x = 5.0
x += 7.0
print x # 12.0
Doesn't that "mut" x?
Well you agree strings are immutable right? But you can do the same thing.
s = 'foo'
s += 'bar'
print s # foobar
The value of the variable changes, but it changes by changing what the variable refers to. A mutable type can change that way, and it can also change "in place".
Here is the difference.
x = something # immutable type
print x
func(x)
print x # prints the same thing
x = something # mutable type
print x
func(x)
print x # might print something different
x = something # immutable type
y = x
print x
# some statement that operates on y
print x # prints the same thing
x = something # mutable type
y = x
print x
# some statement that operates on y
print x # might print something different
Concrete examples
x = 'foo'
y = x
print x # foo
y += 'bar'
print x # foo
x = [1, 2, 3]
y = x
print x # [1, 2, 3]
y += [3, 2, 1]
print x # [1, 2, 3, 3, 2, 1]
def func(val):
val += 'bar'
x = 'foo'
print x # foo
func(x)
print x # foo
def func(val):
val += [3, 2, 1]
x = [1, 2, 3]
print x # [1, 2, 3]
func(x)
print x # [1, 2, 3, 3, 2, 1]
You have to understand that Python represents all its data as objects. Some of these objects like lists and dictionaries are mutable, meaning you can change their content without changing their identity. Other objects like integers, floats, strings and tuples are objects that can not be changed.
An easy way to understand that is if you have a look at an objects ID.
Below you see a string that is immutable. You can not change its content. It will raise a TypeError if you try to change it. Also, if we assign new content, a new object is created instead of the contents being modified.
>>> s = "abc"
>>> id(s)
4702124
>>> s[0]
'a'
>>> s[0] = "o"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = "xyz"
>>> id(s)
4800100
>>> s += "uvw"
>>> id(s)
4800500
You can do that with a list and it will not change the objects identity
>>> i = [1,2,3]
>>> id(i)
2146718700
>>> i[0]
1
>>> i[0] = 7
>>> id(i)
2146718700
To read more about Python's data model you could have a look at the Python language reference:
Python 2 datamodel
Python 3 datamodel
Common immutable type:
numbers: int(), float(), complex()
immutable sequences: str(), tuple(), frozenset(), bytes()
Common mutable type (almost everything else):
mutable sequences: list(), bytearray()
set type: set()
mapping type: dict()
classes, class instances
etc.
One trick to quickly test if a type is mutable or not, is to use id() built-in function.
Examples, using on integer,
>>> i = 1
>>> id(i)
***704
>>> i += 1
>>> i
2
>>> id(i)
***736 (different from ***704)
using on list,
>>> a = [1]
>>> id(a)
***416
>>> a.append(2)
>>> a
[1, 2]
>>> id(a)
***416 (same with the above id)
First of all, whether a class has methods or what it's class structure is has nothing to do with mutability.
ints and floats are immutable. If I do
a = 1
a += 5
It points the name a at a 1 somewhere in memory on the first line. On the second line, it looks up that 1, adds 5, gets 6, then points a at that 6 in memory -- it didn't change the 1 to a 6 in any way. The same logic applies to the following examples, using other immutable types:
b = 'some string'
b += 'some other string'
c = ('some', 'tuple')
c += ('some', 'other', 'tuple')
For mutable types, I can do thing that actallly change the value where it's stored in memory. With:
d = [1, 2, 3]
I've created a list of the locations of 1, 2, and 3 in memory. If I then do
e = d
I just point e to the same list d points at. I can then do:
e += [4, 5]
And the list that both e and d points at will be updated to also have the locations of 4 and 5 in memory.
If I go back to an immutable type and do that with a tuple:
f = (1, 2, 3)
g = f
g += (4, 5)
Then f still only points to the original tuple -- you've pointed g at an entirely new tuple.
Now, with your example of
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
Where you pass
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
(which is a tuple of tuples) as val, you're getting an error because tuples don't have a .clear() method -- you'd have to pass dict(d) as val for it to work, in which case you'll get an empty SortedKeyDict as a result.
Difference between Mutable and Immutable objects
Definitions
Mutable object: Object that can be changed after creating it.
Immutable object: Object that cannot be changed after creating it.
In Python if you change the value of the immutable object it will create a new object.
Mutable Objects
Here are the objects in Python that are of mutable type:
list
Dictionary
Set
bytearray
user defined classes
Immutable Objects
Here are the objects in Python that are of immutable type:
int
float
decimal
complex
bool
string
tuple
range
frozenset
bytes
Some Unanswered Questions
Question: Is string an immutable type?
Answer: yes it is, but can you explain this:
Proof 1:
a = "Hello"
a +=" World"
print a
Output
"Hello World"
In the above example the string got once created as "Hello" then changed to "Hello World". This implies that the string is of the mutable type. But it is not when we check its identity to see whether it is of a mutable type or not.
a = "Hello"
identity_a = id(a)
a += " World"
new_identity_a = id(a)
if identity_a != new_identity_a:
print "String is Immutable"
Output
String is Immutable
Proof 2:
a = "Hello World"
a[0] = "M"
Output
TypeError 'str' object does not support item assignment
Question: Is Tuple an immutable type?
Answer: yes, it is.
Proof 1:
tuple_a = (1,)
tuple_a[0] = (2,)
print a
Output
'tuple' object does not support item assignment
If you're coming to Python from another language (except one that's a lot like Python, like Ruby), and insist on understanding it in terms of that other language, here's where people usually get confused:
>>> a = 1
>>> a = 2 # I thought int was immutable, but I just changed it?!
In Python, assignment is not mutation in Python.
In C++, if you write a = 2, you're calling a.operator=(2), which will mutate the object stored in a. (And if there was no object stored in a, that's an error.)
In Python, a = 2 does nothing to whatever was stored in a; it just means that 2 is now stored in a instead. (And if there was no object stored in a, that's fine.)
Ultimately, this is part of an even deeper distinction.
A variable in a language like C++ is a typed location in memory. If a is an int, that means it's 4 bytes somewhere that the compiler knows is supposed to be interpreted as an int. So, when you do a = 2, it changes what's stored in those 4 bytes of memory from 0, 0, 0, 1 to 0, 0, 0, 2. If there's another int variable somewhere else, it has its own 4 bytes.
A variable in a language like Python is a name for an object that has a life of its own. There's an object for the number 1, and another object for the number 2. And a isn't 4 bytes of memory that are represented as an int, it's just a name that points at the 1 object. It doesn't make sense for a = 2 to turn the number 1 into the number 2 (that would give any Python programmer way too much power to change the fundamental workings of the universe); what it does instead is just make a forget the 1 object and point at the 2 object instead.
So, if assignment isn't a mutation, what is a mutation?
Calling a method that's documented to mutate, like a.append(b). (Note that these methods almost always return None). Immutable types do not have any such methods, mutable types usually do.
Assigning to a part of the object, like a.spam = b or a[0] = b. Immutable types do not allow assignment to attributes or elements, mutable types usually allow one or the other.
Sometimes using augmented assignment, like a += b, sometimes not. Mutable types usually mutate the value; immutable types never do, and give you a copy instead (they calculate a + b, then assign the result to a).
But if assignment isn't mutation, how is assigning to part of the object mutation? That's where it gets tricky. a[0] = b does not mutate a[0] (again, unlike C++), but it does mutate a (unlike C++, except indirectly).
All of this is why it's probably better not to try to put Python's semantics in terms of a language you're used to, and instead learn Python's semantics on their own terms.
Whether an object is mutable or not depends on its type. This doesn't depend on whether or not it has certain methods, nor on the structure of the class hierarchy.
User-defined types (i.e. classes) are generally mutable. There are some exceptions, such as simple sub-classes of an immutable type. Other immutable types include some built-in types such as int, float, tuple and str, as well as some Python classes implemented in C.
A general explanation from the "Data Model" chapter in the Python Language Reference":
The value of some objects can change. Objects whose value can change
are said to be mutable; objects whose value is unchangeable once they
are created are called immutable.
(The value of an immutable container
object that contains a reference to a mutable object can change when
the latter’s value is changed; however the container is still
considered immutable, because the collection of objects it contains
cannot be changed. So, immutability is not strictly the same as having
an unchangeable value, it is more subtle.)
An object’s mutability is
determined by its type; for instance, numbers, strings and tuples are
immutable, while dictionaries and lists are mutable.
A mutable object has to have at least a method able to mutate the object. For example, the list object has the append method, which will actually mutate the object:
>>> a = [1,2,3]
>>> a.append('hello') # `a` has mutated but is still the same object
>>> a
[1, 2, 3, 'hello']
but the class float has no method to mutate a float object. You can do:
>>> b = 5.0
>>> b = b + 0.1
>>> b
5.1
but the = operand is not a method. It just make a bind between the variable and whatever is to the right of it, nothing else. It never changes or creates objects. It is a declaration of what the variable will point to, since now on.
When you do b = b + 0.1 the = operand binds the variable to a new float, wich is created with te result of 5 + 0.1.
When you assign a variable to an existent object, mutable or not, the = operand binds the variable to that object. And nothing more happens
In either case, the = just make the bind. It doesn't change or create objects.
When you do a = 1.0, the = operand is not wich create the float, but the 1.0 part of the line. Actually when you write 1.0 it is a shorthand for float(1.0) a constructor call returning a float object. (That is the reason why if you type 1.0 and press enter you get the "echo" 1.0 printed below; that is the return value of the constructor function you called)
Now, if b is a float and you assign a = b, both variables are pointing to the same object, but actually the variables can't comunicate betweem themselves, because the object is inmutable, and if you do b += 1, now b point to a new object, and a is still pointing to the oldone and cannot know what b is pointing to.
but if c is, let's say, a list, and you assign a = c, now a and c can "comunicate", because list is mutable, and if you do c.append('msg'), then just checking a you get the message.
(By the way, every object has an unique id number asociated to, wich you can get with id(x). So you can check if an object is the same or not checking if its unique id has changed.)
A class is immutable if each object of that class has a fixed value upon instantiation that cannot SUBSEQUENTLY be changed
In another word change the entire value of that variable (name) or leave it alone.
Example:
my_string = "Hello world"
my_string[0] = "h"
print my_string
you expected this to work and print hello world but this will throw the following error:
Traceback (most recent call last):
File "test.py", line 4, in <module>
my_string[0] = "h"
TypeError: 'str' object does not support item assignment
The interpreter is saying : i can't change the first character of this string
you will have to change the whole string in order to make it works:
my_string = "Hello World"
my_string = "hello world"
print my_string #hello world
check this table:
source
It would seem to me that you are fighting with the question what mutable/immutable actually means. So here is a simple explenation:
First we need a foundation to base the explenation on.
So think of anything that you program as a virtual object, something that is saved in a computers memory as a sequence of binary numbers. (Don't try to imagine this too hard, though.^^) Now in most computer languages you will not work with these binary numbers directly, but rather more you use an interpretation of binary numbers.
E.g. you do not think about numbers like 0x110, 0xaf0278297319 or similar, but instead you think about numbers like 6 or Strings like "Hello, world". Never the less theses numbers or Strings are an interpretation of a binary number in the computers memory. The same is true for any value of a variable.
In short: We do not program with actual values but with interpretations of actual binary values.
Now we do have interpretations that must not be changed for the sake of logic and other "neat stuff" while there are interpretations that may well be changed. For example think of the simulation of a city, in other words a program where there are many virtual objects and some of these are houses. Now may these virtual objects (the houses) be changed and can they still be considered to be the same houses? Well of course they can. Thus they are mutable: They can be changed without becoming a "completely" different object.
Now think of integers: These also are virtual objects (sequences of binary numbers in a computers memory). So if we change one of them, like incrementing the value six by one, is it still a six? Well of course not. Thus any integer is immutable.
So: If any change in a virtual object means that it actually becomes another virtual object, then it is called immutable.
Final remarks:
(1) Never mix up your real-world experience of mutable and immutable with programming in a certain language:
Every programming language has a definition of its own on which objects may be muted and which ones may not.
So while you may now understand the difference in meaning, you still have to learn the actual implementation for each programming language. ... Indeed there might be a purpose of a language where a 6 may be muted to become a 7. Then again this would be quite some crazy or interesting stuff, like simulations of parallel universes.^^
(2) This explenation is certainly not scientific, it is meant to help you to grasp the difference between mutable and immutable.
The goal of this answer is to create a single place to find all the good ideas about how to tell if you are dealing with mutating/nonmutating (immutable/mutable), and where possible, what to do about it? There are times when mutation is undesirable and python's behavior in this regard can feel counter-intuitive to coders coming into it from other languages.
As per a useful post by #mina-gabriel:
Books to read that might help: "Data Structures and Algorithms in Python"
Excerpt from that book that lists mutable/immutable types:
mutable/imutable types image
Analyzing the above and combining w/ a post by #arrakëën:
What cannot change unexpectedly?
scalars (variable types storing a single value) do not change unexpectedly
numeric examples: int(), float(), complex()
there are some "mutable sequences":
str(), tuple(), frozenset(), bytes()
What can?
list like objects (lists, dictionaries, sets, bytearray())
a post on here also says classes and class instances but this may depend on what the class inherits from and/or how its built.
by "unexpectedly" I mean that programmers from other languages might not expect this behavior (with the exception or Ruby, and maybe a few other "Python like" languages).
Adding to this discussion:
This behavior is an advantage when it prevents you from accidentally populating your code with mutliple copies of memory-eating large data structures. But when this is undesirable, how do we get around it?
With lists, the simple solution is to build a new one like so:
list2 = list(list1)
with other structures ... the solution can be trickier. One way is to loop through the elements and add them to a new empty data structure (of the same type).
functions can mutate the original when you pass in mutable structures. How to tell?
There are some tests given on other comments on this thread but then there are comments indicating these tests are not full proof
object.function() is a method of the original object but only some of these mutate. If they return nothing, they probably do. One would expect .append() to mutate without testing it given its name. .union() returns the union of set1.union(set2) and does not mutate. When in doubt, the function can be checked for a return value. If return = None, it does not mutate.
sorted() might be a workaround in some cases. Since it returns a sorted version of the original, it can allow you to store a non-mutated copy before you start working on the original in other ways. However, this option assumes you don't care about the order of the original elements (if you do, you need to find another way). In contrast .sort() mutates the original (as one might expect).
Non-standard Approaches (in case helpful):
Found this on github published under an MIT license:
github repository under: tobgu named: pyrsistent
What it is: Python persistent data structure code written to be used in place of core data structures when mutation is undesirable
For custom classes, #semicolon suggests checking if there is a __hash__ function because mutable objects should generally not have a __hash__() function.
This is all I have amassed on this topic for now. Other ideas, corrections, etc. are welcome. Thanks.
One way of thinking of the difference:
Assignments to immutable objects in python can be thought of as deep copies,
whereas assignments to mutable objects are shallow
The simplest answer:
A mutable variable is one whose value may change in place, whereas in an immutable variable change of value will not happen in place. Modifying an immutable variable will rebuild the same variable.
Example:
>>>x = 5
Will create a value 5 referenced by x
x -> 5
>>>y = x
This statement will make y refer to 5 of x
x -------------> 5 <-----------y
>>>x = x + y
As x being an integer (immutable type) has been rebuild.
In the statement, the expression on RHS will result into value 10 and when this is assigned to LHS (x), x will rebuild to 10. So now
x--------->10
y--------->5
Mutable means that it can change/mutate. Immutable the opposite.
Some Python data types are mutable, others not.
Let's find what are the types that fit in each category and see some examples.
Mutable
In Python there are various mutable types:
lists
dict
set
Let's see the following example for lists.
list = [1, 2, 3, 4, 5]
If I do the following to change the first element
list[0] = '!'
#['!', '2', '3', '4', '5']
It works just fine, as lists are mutable.
If we consider that list, that was changed, and assign a variable to it
y = list
And if we change an element from the list such as
list[0] = 'Hello'
#['Hello', '2', '3', '4', '5']
And if one prints y it will give
['Hello', '2', '3', '4', '5']
As both list and y are referring to the same list, and we have changed the list.
Immutable
In some programming languages one can define a constant such as the following
const a = 10
And if one calls, it would give an error
a = 20
However, that doesn't exist in Python.
In Python, however, there are various immutable types:
None
bool
int
float
str
tuple
Let's see the following example for strings.
Taking the string a
a = 'abcd'
We can get the first element with
a[0]
#'a'
If one tries to assign a new value to the element in the first position
a[0] = '!'
It will give an error
'str' object does not support item assignment
When one says += to a string, such as
a += 'e'
#'abcde'
It doesn't give an error, because it is pointing a to a different string.
It would be the same as the following
a = a + 'f'
And not changing the string.
Some Pros and Cons of being immutable
• The space in memory is known from the start. It would not require extra space.
• Usually, it makes things more efficiently. Finding, for example, the len() of a string is much faster, as it is part of the string object.
Every time we change value of a immutable variable it basically destroy the previous instance and create a new instance of variable class
var = 2 #Immutable data
print(id(var))
var += 4
print(id(var))
list_a = [1,2,3] #Mutable data
print(id(list_a))
list_a[0]= 4
print(id(list_a))
Output:
9789024
9789088
140010877705856
140010877705856
Note:Mutable variable memory_location is change when we change the value
I haven't read all the answers, but the selected answer is not correct and I think the author has an idea that being able to reassign a variable means that whatever datatype is mutable. That is not the case. Mutability has to do with passing by reference rather than passing by value.
Lets say you created a List
a = [1,2]
If you were to say:
b = a
b[1] = 3
Even though you reassigned a value on B, it will also reassign the value on a. Its because when you assign "b = a". You are passing the "Reference" to the object rather than a copy of the value. This is not the case with strings, floats etc. This makes list, dictionaries and the likes mutable, but booleans, floats etc immutable.
In Python, there's a easy way to know:
Immutable:
>>> s='asd'
>>> s is 'asd'
True
>>> s=None
>>> s is None
True
>>> s=123
>>> s is 123
True
Mutable:
>>> s={}
>>> s is {}
False
>>> {} is {}
Flase
>>> s=[1,2]
>>> s is [1,2]
False
>>> s=(1,2)
>>> s is (1,2)
False
And:
>>> s=abs
>>> s is abs
True
So I think built-in function is also immutable in Python.
But I really don't understand how float works:
>>> s=12.3
>>> s is 12.3
False
>>> 12.3 is 12.3
True
>>> s == 12.3
True
>>> id(12.3)
140241478380112
>>> id(s)
140241478380256
>>> s=12.3
>>> id(s)
140241478380112
>>> id(12.3)
140241478380256
>>> id(12.3)
140241478380256
It's so weird.
For immutable objects, assignment creates a new copy of values, for example.
x=7
y=x
print(x,y)
x=10 # so for immutable objects this creates a new copy so that it doesnot
#effect the value of y
print(x,y)
For mutable objects, the assignment doesn't create another copy of values. For example,
x=[1,2,3,4]
print(x)
y=x #for immutable objects assignment doesn't create new copy
x[2]=5
print(x,y) # both x&y holds the same list

Categories

Resources