#compsci #python [[Python core data types summary#^aca865|Dynamic typing]] is the basis of much of both the conciseness and flexibility of Python. Because of that, to create an object, Python will perform three distinct steps: 1. Create an object to represent some value (e.g. 3) 2. Create a variable (e.g. number), if it does not yet exist 3. Link the variable to the new object These links from variables to objects are called **references** in Python. Because of that, when you change an example variable a from its original value of 3 to, say, 'something', the variable itself doesn't change its type, as it has none. It simply starts referencing another object, and that's all. The other object, 3, is [[Python core data types summary#^9420a4|garbage collected]] Conceptually, each time you generate a new value in your script, Python creates a new object to represent that value. Internally, as an optimization, Python caches and reuses certain kinds of unchangeable objects, such as small integers and strings. Each object has two standard header fields: a **type designator** used to mark the type of the object, and a **reference counter** used determine when it's OK to reclaim the object. You can always ask Python how many references there are to an object: ```python import sys sys.getrefcount(%objectname%) ``` ^1264ba It is also possibly for several variables to have shared references: ```python a=3 b=a ``` Both of these variables reference the object "3". Beware that if you change an object that has shared references, you might fuck up: ```python L1=[2,3,4] L2=L1 L1[0]=24 # Since both L1 and L2 reference the same list, the first object is going to be different in both of them ``` ```python L1=[2,3,4] L2=L1[:] # now you can change it ``` ## Equality checking It is important to know that the == operator tests whether the two referenced objects have the same values, while the **is** operator tests whether both names point to **the exact same object**: ```python L=[1,2,3] M=[1,2,3] L==M # True L is M # False A=1 B=A A==B # True A is B # True ```