Python 3: Sometimes Immutable Is Mutable and Everything Is an Object - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Computers (https://www.sickgaming.net/forum-86.html) +--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html) +--- Thread: Python 3: Sometimes Immutable Is Mutable and Everything Is an Object (/thread-85366.html) |
Python 3: Sometimes Immutable Is Mutable and Everything Is an Object - xSicKxBot - 06-25-2018 Python 3: Sometimes Immutable Is Mutable and Everything Is an Object <div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/python-3-sometimes-immutable-is-mutable-and-everything-is-an-object.png" width="1616" height="778" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/python-3-sometimes-immutable-is-mutable-and-everything-is-an-object.png" class="ff-og-image-inserted" /></div> <h3>What is Python?</h3> <p class="graf graf--p graf-after--figure">Python is an interpreted, interactive object-oriented programming language; it incorporated modules, classes, exceptions, dynamic typing and high level data types. Python is also powerful when it comes to clear syntax. It is a high-level general-purpose programming language that can be applied to many different classes of problems — with a large standard library that encapsulates string processing (regular expressions, Unicode, calculating differences between files), Internet protocols (HTTP, FTP, SMTP, XML-RPC, POP, IMAP, CGI programming), software engineering (unit testing, logging, profiling, parsing Python code), and operating system interfaces (system calls, filesystems, TCP/IP sockets). Here are some of Python’s features:</p> <ul class="postList"> <li class="graf graf--li graf-after--p">An <em class="markup--em markup--li-em">interpreted</em> (as opposed to <em class="markup--em markup--li-em">compiled</em>) language. Contrary to C, for example, Python code does not need to be compiled before executing it. In addition, Python can be used interactively: many Python interpreters are available, from which commands and scripts can be executed.</li> <li class="graf graf--li graf-after--li">A free software released under an open-source license: Python can be used and distributed free of charge, even for building commercial software.</li> <li class="graf graf--li graf-after--li">Multi-platform: Python is available for all major operating systems, Windows, Linux/Unix, MacOS X, most likely your mobile phone OS, etc.</li> <li class="graf graf--li graf-after--li">A very readable language with clear non-verbose syntax</li> <li class="graf graf--li graf-after--li">A language for which a large variety of high-quality packages are available for various applications, from web frameworks to scientific computing.</li> <li class="graf graf--li graf-after--li">A language very easy to interface with other languages, in particular C and C++.</li> <li class="graf graf--li graf-after--li">Some other features of the language are illustrated just below. For example, Python is an object-oriented language, with dynamic typing (an object’s type can change during the course of a program).</li> </ul> <h3 class="graf graf--h3 graf-after--li">What does it mean to be an object-oriented language?</h3> <p class="graf graf--p graf-after--h3">Python is a multi-paradigm programming language. Meaning, it supports different programming approach. One of the popular approach to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).</p> <p class="graf graf--p graf-after--p">An object has two characteristics:<br />1) attributes<br />2) behavior</p> <p class="graf graf--p graf-after--p">Let’s take an example:</p> <p class="graf graf--p graf-after--p">Dog is an object:<br />a) name, age, color are data<br />b) singing, dancing are behavior</p> <p class="graf graf--p graf-after--p">We call <em class="markup--em markup--p-em">data</em> as <em class="markup--em markup--p-em">attributes</em> and <em class="markup--em markup--p-em">behavior</em> as <em class="markup--em markup--p-em">methods</em> in object oriented programming. Again:</p> <p class="graf graf--p graf-after--p">Data → Attributes & Behavior → Methods</p> <p class="graf graf--p graf-after--p">The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don’t Repeat Yourself). In Python, the concept of OOP follows some basic principles:</p> <p class="graf graf--p graf-after--p">Inheritance — A process of using details from a new class without modifying existing class.<br />Encapsulation — Hiding the private details of a class from other objects.<br />Polymorphism — A concept of using common operation in different ways for different data input.</p> <h3 class="graf graf--h3 graf-after--p">Class</h3> <p class="graf graf--p graf-after--h3">A class is a blueprint for the object.</p> <p class="graf graf--p graf-after--p">We can think of class as an sketch of a dog with labels. It contains all the details about the name, colors, size etc. Based on these descriptions, we can study about the dog. Here, dog is an object.</p> <p class="graf graf--p graf-after--p">The example for class of dog can be :</p> <pre class="graf graf--pre graf-after--p"> class Dog: pass</pre> <p class="graf graf--p graf-after--pre">Here, we use <code class="markup--code markup--p-code">class</code> keyword to define an empty class Dog. From class, we construct instances. An instance is a specific object created from a particular class.</p> <p class="graf graf--p graf-after--p">A Class is the blueprint from which individual objects are created. In the real world we often find many objects with all the same type. Like cars. All the same make and model (have an engine, wheels, doors, …). Each car was built from the same set of blueprints and has the same components.</p> <h3 class="graf graf--h3 graf-after--p">Object</h3> <p class="graf graf--p graf-after--h3">Think of an object in Python as a block of memory, and a variable is just something that points/references to that block of memory. All the information relevant to your data is stored within the object itself. And the variable stores the address to that object. So it actually doesn’t matter if you reassign a variable pointing to an integer to point to a different data type.</p> <pre class="graf graf--pre graf-after--p"> >>> a = 1 >>> a = "I am a string now" >>> print(a) I am a string now</pre> <p class="graf graf--p graf-after--pre">Every object has its own identity/ID that stores its address in memory. Every object has a type. An object can also hold references to other objects. For example, an integer will not have references to other objects but if the object is a list, it will contain references to each object within this list. We will touch up on this when we look at tuples later.</p> <p class="graf graf--p graf-after--p">The built-in function <code class="markup--code markup--p-code">id()</code> will return an object’s id and <code class="markup--code markup--p-code">type()</code> will return an object’s type:</p> <pre class="graf graf--pre graf-after--p"> >>> list_1 = [1, 2, 3]</pre> <pre class="graf graf--pre graf-after--pre"> # to access this object's value >>> list_1 [1, 2, 3]</pre> <pre class="graf graf--pre graf-after--pre"> # to access this object's ID >>> id(list_1) 140705683311624</pre> <pre class="graf graf--pre graf-after--pre"> # to access object's data type >>> type(list_1) <class 'list'></pre> <p class="graf graf--p graf-after--pre">So, an object (instance) is an instantiation of a class. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated.</p> <p class="graf graf--p graf-after--p">The example for object of class Dog can be:</p> <pre class="graf graf--pre graf-after--p"> obj = Dog()</pre> <p class="graf graf--p graf-after--pre">Here, obj is object of class <code class="markup--code markup--p-code">Dog</code>.</p> <p class="graf graf--p graf-after--p">Suppose we have details of Dog. Now, we are going to show how to build the class and objects of Dog.</p> <pre class="graf graf--pre graf-after--p"> class Dog:</pre> <pre class="graf graf--pre graf-after--pre"> #class attribute species = "animal"</pre> <pre class="graf graf--pre graf-after--pre"> # instance attribute def __init__(self, name, age): self.name = name self.age = age</pre> <pre class="graf graf--pre graf-after--pre"> # instantiate the Dog class blu = Dog("Blu", 10) woo = Dog("Woo", 15)</pre> <pre class="graf graf--pre graf-after--pre"> # access the class attributes print("Blu is an {}".format(blu.__class__.species)) print("Woo is also an {}".format(woo.__class__.species))</pre> <pre class="graf graf--pre graf-after--pre"> # access the instance attributes print("{} is {} years old".format( blu.name, blu.age)) print("{} is {} years old".format( woo.name, woo.age))</pre> <p class="graf graf--p graf-after--pre">When we run the program, the output will be:</p> <pre class="graf graf--pre graf-after--p"> Blu is an animal Woo is also an animal Blu is 10 years old Woo is 15 years old</pre> <p class="graf graf--p graf-after--pre">In the above program, we create a class with name Dog. Then, we define attributes. The attributes are a characteristic of an object.</p> <p class="graf graf--p graf-after--p">Then, we create instances of the Dog class. Here, blu and woo are references (value) to our new objects.</p> <p class="graf graf--p graf-after--p">Then, we access the class attribute using <code class="markup--code markup--p-code">__class __.species</code>. Class attributes are same for all instances of a class. Similarly, we access the instance attributes using <code class="markup--code markup--p-code">blu.name</code> and <code class="markup--code markup--p-code">blu.age</code>. However, instance attributes are different for every instance of a class.</p> <p class="graf graf--p graf-after--p">Let’s try to understand how value and identity are affected if you use operators “==” and “is”</p> <p class="graf graf--p graf-after--p">The “==” operator compares values whereas “is” operator compares identities. Hence, a is b is similar to id(a) == id(y), but two different objects may share the same value, but they will never share the same identity.</p> <p class="graf graf--p graf-after--p">Example:</p> <pre class="graf graf--pre graf-after--p"> >>> a = ['blu', 'woof'] >>> id(a) 1877152401480 >>> b = a >>> id(b) 1877152401480 >>> id(a) == id(b) True >>> a is b True >>> c = ['blu', 'woof'] >>> a == c True >>> id© 1877152432200 >>> id(a) == id© False</pre> <h3 class="graf graf--h3 graf-after--pre">Hashability</h3> <h4 class="graf graf--h4 graf-after--h3">What is a hash?</h4> <blockquote class="graf graf--blockquote graf-after--h4"> <p><a class="markup--anchor markup--blockquote-anchor" href="https://docs.python.org/3/glossary.html#term-hashable">According to Python , “An object is hashable if it has a hash value which never changes during its lifetime”, if and only if the object is immutable.</a></p> </blockquote> <p class="graf graf--p graf-after--blockquote">A hash is an integer that depends on an object’s value, and objects with the same value always have the same hash. (Objects with different values will occasionally have the same hash too. This is called a <a class="markup--anchor markup--p-anchor" href="https://stackoverflow.com/a/17586126/1893164"><em class="markup--em markup--p-em">hash collision</em></a>.) While <code class="markup--code markup--p-code">id()</code> will return an integer based on an object’s identity, the <code class="markup--code markup--p-code">hash()</code> function will return an integer (the object’s hash) based on the hashable object’s value:</p> <pre class="graf graf--pre graf-after--p"> >>> a = ('cow', 'bull') >>> b = ('cow', 'bull') >>> a == b True >>> a is b False >>> hash(a) 6950940451664727300 >>> hash(b) 6950940451664727300 >>> hash(a) == hash(b) True</pre> <p class="graf graf--p graf-after--pre">Immutable objects can be hashable, mutable objects can’t be hashable.This is important to know, because (for reasons beyond the scope of this post) <a class="markup--anchor markup--p-anchor" href="https://stackoverflow.com/questions/17585730/what-does-hash-do-in-python">only hashable objects can be used as keys in a dictionary or as items in a set</a>. Since hashes are based on values and only immutable objects can be hashable, this means that hashes will never change during the object’s lifetime.</p> <p class="graf graf--p graf-after--p">Hashability will be covered more under the mutable vs immutable object section, as sometimes a tuple can be mutable and how does that change values and understanding of mutable objects and immutable objects.</p> <p class="graf graf--p graf-after--p">To summarize, EVERYTHING is an object in Python the only difference is some are mutable and some immutable. Wait but what kind of objects are possible in Python and which ones are mutable and which ones aren’t?</p> <p class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">Objects of built-in types like (bytes, int, float, bool, str, tuple, unicode, complex) are immutable. Objects of built-in types like (list, set, dict, array, bytearray) are mutable. Custom classes are mutable. To simulate immutability in a class, one should override attribute setting and deletion to raise exceptions.</em></p> <p class="graf graf--p graf-after--figure">Now how would a newbie know which variables are mutable objects and which ones are not? For this we use 2 very handy built-in functions called id() and type()</p> <h3 class="graf graf--h3 graf-after--p">What is id() and type()?</h3> <pre class="graf graf--pre graf-after--h3"> Syntax to use id() id(object)</pre> <p class="graf graf--p graf-after--pre">As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.</p> <p class="graf graf--p graf-after--p">Examples:</p> <pre class="graf graf--pre graf-after--p"> The output is the identity of the object passed. This is random but when running in the same program, it generates unique and same identity. </pre> <pre class="graf graf--pre graf-after--pre"> Input : id(2507) Output : 140365829447504 Output varies with different runs</pre> <pre class="graf graf--pre graf-after--pre"> Input : id("Holberton") Output : 139793848214784</pre> <h3 class="graf graf--h3 graf-after--pre">What is an Alias?</h3> <pre class="graf graf--pre graf-after--h3"> >>> a = 1 >>> id(a) 1904391232 >>> b = a #aliasing a >>> id(b) 1904391232 >>> b 1</pre> <p class="graf graf--p graf-after--pre"><em class="markup--em markup--p-em">An alias is a second name for a piece of data. Programmers use/ create aliases because it’s often easier and faster to refer data than to copy it. If the data that is being created and assigned is immutable then aliasing does not matter as the data won’t change, but there will be a lot of bugs if the data is mutable as it will lead to some issues like see below —</em></p> <pre class="graf graf--pre graf-after--p"> >>> a = 1 >>> id(a) 1904391232 >>> b = a #aliasing a >>> id(b) 1904391232 >>> b 1 >>> a = 2 >>> id(2) 1904391264 >>> id(b) 1904391232 >>> b 1 >>> a 2</pre> <p class="graf graf--p graf-after--pre">as it can be seen a now points to 2 and id is different as compared to b which is still pointing to 1. In Python, aliasing happens whenever one variable’s value is assigned to another variable, because variables are just names that store references to values.</p> <p class="graf graf--p graf-after--p"><code class="markup--code markup--p-code">type()</code> method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.</p> <p class="graf graf--p graf-after--p">Two different types of arguments can be passed to type() function, single and three argument. If single argument <code class="markup--code markup--p-code">type(obj)</code> is passed, it returns the type of given object.</p> <p class="graf graf--p graf-after--p">Syntax :</p> <pre class="graf graf--pre graf-after--p"> <code class="markup--code markup--pre-code">type(object)</code></pre> <p class="graf graf--p graf-after--pre">We can find out what class an object belongs to using the built-in <code class="markup--code markup--p-code">type()</code>function:</p> <pre class="graf graf--pre graf-after--p"> >>> Blue = [1, 2, 3] >>> type(Blue) <class 'list'></pre> <pre class="graf graf--pre graf-after--pre"> >>> def my_func(x) ... x = 89 >>> type(my_func) <class 'function'></pre> <p class="graf graf--p graf-after--pre">Now that we can compare variables to see their type and id’s, we can dive in deeper to understand how mutable and immutable objects work.</p> <h3 class="graf graf--h3 graf-after--p">Mutable Objects vs. Immutable Objects</h3> <p class="graf graf--p graf-after--h3">Not all Python objects handle changes the same way. Some objects are mutable, meaning they can be altered. Others are immutable; they cannot be changed but rather return new objects when attempting to update. What does this mean when writing Python code?</p> <p class="graf graf--p graf-after--p">The following are some mutable objects:</p> <ul class="postList"> <li class="graf graf--li graf-after--p">list</li> <li class="graf graf--li graf-after--li">dict</li> <li class="graf graf--li graf-after--li">set</li> <li class="graf graf--li graf-after--li">bytearray</li> <li class="graf graf--li graf-after--li">user-defined classes (unless specifically made immutable)</li> </ul> <p class="graf graf--p graf-after--li">The following are some immutable objects:</p> <ul class="postList"> <li class="graf graf--li graf-after--p">int</li> <li class="graf graf--li graf-after--li">float</li> <li class="graf graf--li graf-after--li">decimal</li> <li class="graf graf--li graf-after--li">complex</li> <li class="graf graf--li graf-after--li">bool</li> <li class="graf graf--li graf-after--li">string</li> <li class="graf graf--li graf-after--li">tuple</li> <li class="graf graf--li graf-after--li">range</li> <li class="graf graf--li graf-after--li">frozenset</li> <li class="graf graf--li graf-after--li">bytes</li> </ul> <p class="graf graf--p graf-after--li">The distinction is rather simple: mutable objects can change, whereas immutable objects cannot. Immutable literally means not mutable.</p> <p class="graf graf--p graf-after--p">A standard example are <code class="markup--code markup--p-code">tuple</code> and <code class="markup--code markup--p-code">list</code>: A <code class="markup--code markup--p-code">tuple</code> is filled on creation, and then is frozen – its content cannot change anymore. To a <code class="markup--code markup--p-code">list</code>, one can append elements, set elements and delete elements at any time. Although keep in mind exceptions: <em class="markup--em markup--p-em">tuple</em> is an immutable list whereas <em class="markup--em markup--p-em">frozenset</em> is an immutable <em class="markup--em markup--p-em">set</em>. Quoting <a class="markup--anchor markup--p-anchor" href="https://stackoverflow.com/a/14422446">stackoverflow </a>answer-<code class="markup--code markup--p-code">Tuples</code> are indeed an ordered collection of objects, but they can contain duplicates and unhashable objects, and have slice functionality <code class="markup--code markup--p-code">frozensets</code> aren’t indexed, but you have the functionality of <code class="markup--code markup--p-code">sets</code> – O(1) element lookups, and functionality such as unions and intersections. They also can’t contain duplicates, like their mutable counterparts.</p> <p class="graf graf--p graf-after--p">Let’s create a dictionary with immutable objects for keys —</p> <pre class="graf graf--pre graf-after--p"> >>> a = {‘blu’: 42, True: ‘woof’, (‘x’, ‘y’, ‘z’): [‘hello’]} >>> a.keys() dict_keys([‘blu’, True, (‘x’, ‘y’, ‘z’)])</pre> <p class="graf graf--p graf-after--pre">As seen above keys in a are immutable, hashable objects, but if you try to call hash() on a mutable object(such as sets), or trying to use a mutable object for a dictionary key, an error will be raised:</p> <pre class="graf graf--pre graf-after--p"> >>> spam = {['hello', 'world']: 42} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list' >>> d = {'a': 1} >>> spam = {d: 42} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'dict'</pre> <p class="graf graf--p graf-after--pre">So, tuples, being immutable objects, can be used as dictionary keys?</p> <pre class="graf graf--pre graf-after--p"> >>> spam = {('a', 'b', 'c'): 'hello'} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list'</pre> <p class="graf graf--p graf-after--pre">As seen above, if a tuple contains a mutable object, according to the previous explanation about hashability it cannot be hashed. So, immutable objects <em class="markup--em markup--p-em">can</em> be hashable, but this doesn’t necessarily mean they’re <em class="markup--em markup--p-em">always</em>hashable. And remember, the hash is derived from the object’s value.</p> <p class="graf graf--p graf-after--p">This is an interesting corner case: a tuple (which should be immutable) that contains a mutable list cannot be hashed. This is because the hash of the tuple depends on the tuple’s value, but if that list’s value can change, that means the tuple’s value can change and therefore the hash can change during the tuple’s lifetime.</p> <p class="graf graf--p graf-after--p">So far it is now understood that some tuples are hashable — immutable but some other tuple are not hashable — mutable. According to official Python documentation immutable and mutable are defined as — <a class="markup--anchor markup--p-anchor" href="https://docs.python.org/3/glossary.html#term-immutable">“An object with a fixed value”</a> and <a class="markup--anchor markup--p-anchor" href="https://docs.python.org/3/glossary.html#term-mutable">“Mutable objects can change their value”</a>. This can possibly mean that mutability is a property of objects, hence it makes sense that some tuples will be mutable while others won’t be.</p> <pre class="graf graf--pre graf-after--p"> >>> a = ('dogs', 'cats', [1, 2, 3]) >>> b = ('dogs', 'cats', [1, 2, 3]) >>> a == b True >>> a is b False >>> a[2].append(99) >>> a ('dogs', 'cats', [1, 2, 3, 99]) >>> a == b False</pre> <p class="graf graf--p graf-after--pre">In this example, the tuples a and b have equal (==) values but are different objects, so when list is changed in tuple a the values get changed as a is not longer == b and did not change values of b. This example states that tuples are mutable.</p> <p class="graf graf--p graf-after--p">While Python tends towards mutability, there are many use-cases for immutability as well. Here are some straightforward ones:</p> <ul class="postList"> <li class="graf graf--li graf-after--p">Mutable objects are great for efficiently passing around data. Let’s say object <code class="markup--code markup--li-code">anton</code> and <code class="markup--code markup--li-code">berta</code> have access to the same <code class="markup--code markup--li-code">list</code>. <code class="markup--code markup--li-code">anton</code> adds <code class="markup--code markup--li-code">“lemons”</code> to the list, and <code class="markup--code markup--li-code">berta</code> automatically has access to this information.<br />If both would use a <code class="markup--code markup--li-code">tuple</code>, anton would have to <em class="markup--em markup--li-em">copy</em> the entries of his shopping-tuple, <em class="markup--em markup--li-em">add</em> the new element, <em class="markup--em markup--li-em">create</em> a new tuple, then <em class="markup--em markup--li-em">send</em> that to <code class="markup--code markup--li-code">berta</code>. Even if both can talk directly, that is a lot of work.</li> <li class="graf graf--li graf-after--li">Immutable objects are great for working with the data. So <code class="markup--code markup--li-code">berta</code> is going to buy all that stuff – she can read everything, make a plan, and does not have to double check for changes. If next week, she needs to buy more stuff for the same shopping-tuple, <code class="markup--code markup--li-code">berta</code> just reuses the old plan. She has the guarantee that <code class="markup--code markup--li-code">anton</code> <em class="markup--em markup--li-em">cannot</em> change anything unnoticed.<br />If both would use a <code class="markup--code markup--li-code">list</code>, <code class="markup--code markup--li-code">berta</code> could not plan ahead. She has no guarantee that <code class="markup--code markup--li-code">“lemons”</code> are still on the list when she arrives at the shop. She has no guarantee that next week, she can just repeat what was appropriate last week.</li> </ul> <p class="graf graf--p graf-after--li">You should generally use mutable objects when having to deal with growing data. For example, when parsing a file, you may append information from each line to a list. Custom objects are usually mutable, buffering data, adjusting to new conditions and so on. In general, whenever something can change, mutable objects are much easier.</p> <p class="graf graf--p graf-after--p">Immutable objects are sparingly used in python — usually, it is implicit such as using <code class="markup--code markup--p-code">int</code> or other basic, immutable types. Often, you will be using mutable types as de-facto immutable – many <code class="markup--code markup--p-code">list</code>s are filled at construction and never changed. There is also no immutable <code class="markup--code markup--p-code">dict</code>. You should enforce immutability to optimise algorithms, e.g. to do caching.</p> <p class="graf graf--p graf-after--p">Interestingly enough, python’s often-used <code class="markup--code markup--p-code">dict</code> requires keys to be immutable. It is a data structure that <em class="markup--em markup--p-em">cannot</em> work with mutable objects, since it relies on some features being guaranteed for its elements.</p> <h4 class="graf graf--h4 graf-after--p">Mutable example</h4> <pre class="graf graf--pre graf-after--h4"> >>> my_list = [10, 20, 30] >>> print(my_list)</pre> <pre class="graf graf--pre graf-after--pre"> [10, 20, 30]</pre> <pre class="graf graf--pre graf-after--pre"> >>> my_list = [10, 20, 30] >>> my_list[0] = 40 >>> print(my_list)</pre> <pre class="graf graf--pre graf-after--pre"> [40, 20, 30]</pre> <h4 class="graf graf--h4 graf-after--pre">Immutable example</h4> <pre class="graf graf--pre graf-after--h4"> >>> tuple_ = (10, 20, 30) >>> print(tuple_)</pre> <pre class="graf graf--pre graf-after--pre"> [10, 20, 30]</pre> <pre class="graf graf--pre graf-after--pre"> >>> tuple_ = [10, 20, 30] >>> tuple_[0] = 40 >>> print(tuple_)</pre> <pre class="graf graf--pre graf-after--pre"> Traceback (most recent call last): File "test.py", line 3, in < module > my_yuple[0] = 40 TypeError: 'tuple' object does not support item assignment</pre> <p class="graf graf--p graf-after--pre">If you want to write most efficient code, you should be the knowing difference between mutable and immutable in python. Concatenating string in loops wastes lots of memory , because strings are immutable, concatenating two strings together actually creates a third string which is the combination of the previous two. If you are iterating a lot and building a large string, you will waste a lot of memory creating and throwing away objects. Use list compression join technique.</p> <p class="graf graf--p graf-after--p">Python handles mutable and immutable objects differently. Immutable are quicker to access than mutable objects. Also, immutable objects are fundamentally expensive to “change”, because doing so involves creating a copy. Changing mutable objects is cheap.</p> <h3 class="graf graf--h3 graf-after--p">Interning, integer caching and everything called: NSMALLPOSINTS & NSMALLNEGINTS</h3> <p class="graf graf--p graf-after--h3">Easy things first —</p> <p class="graf graf--p graf-after--p"><code class="markup--code markup--p-code">NSMALLNEGINTS</code> is in the range -5 to 0 and <code class="markup--code markup--p-code">NSMALLPOSINTS</code> is in the 0 to 256 range. These are macros defined in Python — earlier versions ranged from <a class="markup--anchor markup--p-anchor" href="https://github.com/python/cpython/commit/842d2ccdcd540399501a918b9724d2eaf5599f39">-1 to 99</a>, then <a class="markup--anchor markup--p-anchor" href="https://github.com/python/cpython/commit/c91ed400e053dc9f11dd30c84e2bb611999dce50">-5 to 99</a> and finally <a class="markup--anchor markup--p-anchor" href="https://github.com/python/cpython/commit/418a1ef0895e826c65d4113be5d86891c199e15d">-5 to 256</a>. Python keeps an array of integer objects for “all integers between -5 and 256”. When creating an int in that range, it is actually just getting a reference to the existing object in memory.</p> <p class="graf graf--p graf-after--p">If x = 42, what happens actually is Python performing a search in the integer block for the value in the range -5 to 256. Once x falls out of the scope of this range, it will be garbage collected (destroyed) and be an entirely different object. The process of creating a new integer object and then destroying it immediately creates a lot of useless calculation cycles, so Python preallocated a range of commonly used integers.</p> <p class="graf graf--p graf-after--p">There are exception to immutable objects as stated above by making a tuple “mutable”. As it is known a new object is created each time a variable makes a reference to it, it does happen slightly differently for a few things –</p> <p class="graf graf--p graf-after--p">a) Strings without whitespaces and less than 20 characters<br />b) Integers between -5 to 256 (including both as explained above)<br />c) empty immutable objects (tuples)</p> <p class="graf graf--p graf-after--p">These objects are always reused or <em class="markup--em markup--p-em">interned</em>. This is due memory optimization in Python implementation. The rationale behind doing this is as follows:</p> <ol class="postList"> <li class="graf graf--li graf-after--p">Since programmers use these objects frequently, interning existing objects saves memory.</li> <li class="graf graf--li graf-after--li">Since immutable objects like tuples and strings cannot be modified, there is no risk in interning the same object.</li> </ol> <p class="graf graf--p graf-after--li">So what does it mean by “interning”?</p> <p class="graf graf--p graf-after--p"><em class="markup--em markup--p-em">interning </em>allows two variables to refer to the <em class="markup--em markup--p-em">same string object</em>. Python automatically does this, although the exact rules remain fuzzy. One can also forcibly intern strings by calling the <code class="markup--code markup--p-code">intern()</code>function. <a class="markup--anchor markup--p-anchor" href="http://guilload.com/python-string-interning/">Guillo’s article</a>provides an in-depth look into string interning.</p> <p class="graf graf--p graf-after--p">Example of string interning with more than 20 characters or whitespace will be new objects:</p> <pre class="graf graf--pre graf-after--p"> >>> a = "Howdy! How are you?" >>> b = "Howdy! How are you?" >>> a is b False</pre> <p class="graf graf--p graf-after--pre">but, if a string is less than 20 char and no whitespace it will look somewhat like this:</p> <pre class="graf graf--pre graf-after--p"> >>> a = "python" >>> b = "python" >>> a is b True</pre> <p class="graf graf--p graf-after--pre">As a and b refer to the same objects.</p> <p class="graf graf--p graf-after--p">Let’s move on to integers now.</p> <p class="graf graf--p graf-after--p">As explained above in macro definition integer caching is happening because of preload python definition of commonly used integers. Hence, variables referring to an integer within the range would be pointing to the same object that already exists in memory:</p> <pre class="graf graf--pre graf-after--p"> >>> a = 256 >>> b = 256 >>> a is b True</pre> <p class="graf graf--p graf-after--pre">This is not the case if the object referred to is outside the range:</p> <pre class="graf graf--pre graf-after--p"> >>> a = 1024 >>> b = 1024 >>> a is b False</pre> <p class="graf graf--p graf-after--pre">Lastly, let’s talk about empty immutable objects:</p> <pre class="graf graf--pre graf-after--p"> >>> a = () >>> b = () >>> a is b True</pre> <p class="graf graf--p graf-after--pre">Here a and b refer to the same object in memory as it is an empty tuple, but this changes if the tuple is not empty.</p> <pre class="graf graf--pre graf-after--p"> >>> a = (1, 2) >>> b = (1, 2) >>> a == b True >>> a is b False</pre> <h3 class="graf graf--h3 graf-after--pre">Passing mutable and immutable objects into functions:</h3> <p class="graf graf--p graf-after--h3">Immutable and mutable objects or variables are handled differently while working with function arguments. In the following diagram, variables <code class="markup--code markup--p-code">a</code>, <code class="markup--code markup--p-code">b</code>and <code class="markup--code markup--p-code">name</code> point to their memory locations where the actual value of the object is stored.</p> <h3 class="graf graf--h3 graf-after--figure">Major Concepts of Function Argument Passing in Python</h3> <p class="graf graf--p graf-after--h3">Arguments are always passed to functions by object in Python. The caller and the function code blocks share the same object or variable. When we change the value of a function argument inside the function code block scope, the value of that variable also changes inside the caller code block scope regardless of the name of the argument or variable. This concept behaves differently for both mutable and immutable arguments in Python.</p> <p class="graf graf--p graf-after--p">In Python, <code class="markup--code markup--p-code">integer</code>, <code class="markup--code markup--p-code">float</code>, <code class="markup--code markup--p-code">string</code> and <code class="markup--code markup--p-code">tuple</code> are immutable objects. <code class="markup--code markup--p-code">list</code>, <code class="markup--code markup--p-code">dict</code> and <code class="markup--code markup--p-code">set</code> fall in the mutable object category. This means the value of <code class="markup--code markup--p-code">integer</code>, <code class="markup--code markup--p-code">float</code>, <code class="markup--code markup--p-code">string</code> or <code class="markup--code markup--p-code">tuple</code> is not changed in the calling block if their value is changed inside the function or method block but the value of <code class="markup--code markup--p-code">list</code>, <code class="markup--code markup--p-code">dict</code> or <code class="markup--code markup--p-code">set</code> object is changed.</p> <h3 class="graf graf--h3 graf-after--p">Python Immutable Function Arguments</h3> <p class="graf graf--p graf-after--h3">Python immutable objects, such as <code class="markup--code markup--p-code">numbers</code>, <code class="markup--code markup--p-code">tuple</code> and <code class="markup--code markup--p-code">strings</code>, are also passed by reference like mutable objects, such as <code class="markup--code markup--p-code">list</code>,<code class="markup--code markup--p-code"> set</code> and <code class="markup--code markup--p-code">dict.</code> Due to state of immutable (unchangeable) objects if an integer or string value is changed inside the function block then it much behaves like an object copying. A local new duplicate copy of the caller object inside the function block scope is created and manipulated. The caller object will remain unchanged. Therefore, caller block will not notice any changes made inside the function block scope to the immutable object. Let’s take a look at the following example.</p> <h3 class="graf graf--h3 graf-after--p">Python Immutable Function Argument — Example and Explanation</h3> <pre class="graf graf--pre graf-after--h3"> def foo1(a): <em class="markup--em markup--pre-em"># function block </em>a += 1 print(‘id of a:’, id(a)) <em class="markup--em markup--pre-em"># id of y and a are same</em></pre> <pre class="graf graf--pre graf-after--pre"> return a</pre> <pre class="graf graf--pre graf-after--pre"> <em class="markup--em markup--pre-em"># main or caller block </em>x = 10 y = foo1(x)</pre> <pre class="graf graf--pre graf-after--pre"> <em class="markup--em markup--pre-em"># value of x is unchanged </em>print(‘x:’, x)</pre> <pre class="graf graf--pre graf-after--pre"> <em class="markup--em markup--pre-em"># value of y is the return value of the function foo1</em></pre> <pre class="graf graf--pre graf-after--pre"> <em class="markup--em markup--pre-em"># after adding 1 to argument ‘a’ which is actual variable ‘x’ </em>print(‘y:’, y) print(‘id of x:’, id(x)) <em class="markup--em markup--pre-em"># id of x </em>print(‘id of y:’, id(y)) <em class="markup--em markup--pre-em"># id of y, different from x</em></pre> <p class="graf graf--p graf-after--pre">Result:</p> <pre class="graf graf--pre graf-after--p graf--trailing"> <code class="markup--code markup--pre-code u-paddingRight0 u-marginRight0">id of a: 1456621360 x: 10 y: 11 id of x: 1456621344 id of y: 1456621360</code></pre> </div> |