---
# System prepended metadata

title: 'Objects: Examples'

---

# Objects: Examples


([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/By6YoM8Gw) ... [next](https://hackmd.io/@alexhkurz/rkXh-CUGP) ... )

One reason the previous session was difficult is that lists are "objects" and we did not yet understand what objects are.

In Python everything is an objet.

Download the program [objects.py](https://github.com/alexhkurz/introduction-to-programming/blob/master/src/objects.py). 

You may have to run in a terminal

    pip install termcolor
    
in order to install the package `termcolor` that I use to print coloured output.

**Activity:** Run `objects.py`. 
- Which line of the output stems from which line of the program?
- Which of the results would you expected and which did surprise you? 
- In a terminal, call `python` 

        python
        >>>

    and run your own interactive experiments. Here is an example of such an interactive session:

		>>> a = []
		>>> a
		[]
		>>> id(a)
		4407006064
		>>> a = a + [1]
		>>> a
		[1]
		>>> id(a)
		4408064352
		>>> a += [2]
		>>> a
		[1, 2]
		>>> id(a)
		4408064352    
        
    If you want to recreate this example enter the text after the python prompts `>>>`, line by line.
        
    `id(a)` returns the name of the object `a`, or, in fact, the address where the object `a` is stored in memory. How does this shed light on the difference between `... = ... + ...` and `... += ...`?
    
- Create experiments, similar to the one in the item above, that check the following.
  - Create the same list twice as in `a = []` and `b = []`. Do the variables `a` and `b` refer to the same object?
  - What happens if you replace lists by numbers as eg in `a=0` and `b=0`?
  - What happens if you use larger numbers as eg in `a=257` and `b=257`? 
  - Can you make two variables refer to the same list? 

  Btw, you can use `is` to test whether the objects named by two variabels are the same: `a is b` is equivalent to `id(a) == id(b)`, which is different to `a==b`:

  - The phrase "the same object" can have two different interpretations. Accordingly, Python has two different operators, `==` and `is`. Run some experiments that explain the difference between the two. (At first, do not look up `==` and `is` but try to find out for yourself.)
    
- Can you design a memory model for Python that explains the results of the program `object.py` and your own experiments?

## References

From the official Python Tutorial:


- [3.1.3. Lists](https://docs.python.org/3/tutorial/introduction.html#lists)
- [5.1. More on Lists](https://docs.python.org/3/tutorial/datastructures.html)

The built-in function [`id`](https://docs.python.org/3/library/functions.html#id). Btw, it can't harm to browse through the list of all [built-in functions](https://docs.python.org/3/library/functions.html). There should be a couple you have seen already and others whose meaning you can probably guess from the name.

From Stackoverflow:
- [Append in Python](https://stackoverflow.com/questions/725782/in-python-what-is-the-difference-between-append-and/725882
)
- [Implementation on Python lists](https://stackoverflow.com/questions/3917574/how-is-pythons-list-implemented
)
- [Print coloured text in Python](https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-python
)



