PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
Tutorial About References (by HonFu)
------------------------------------
'''
# For the most part, Python is an easy-to-learn language. Most things work in a way a regular non-programmer person would assume. Almost from day one, you can concentrate on writing code and don't have to think about hardware issues, like how to claim memory, or which memory, how to free it, after you're done and all of that.
# The downside: Many things, which Python does for you in the background, are thereby hidden from you, and this is an area that can be confusing - and not only for beginners.
# You try to change an item in a list, and suddenly another list changes too and you wonder how that could happen. This is a simple example, but in my experience, whenever I struggle to get something right in Python, it is really a reference issue.
# It's no surprise that many questions in Q&A are about these issues.
# In this mini-tutorial, I'll try to 'dumb it down' as much as I can. I will not get very technical (I couldn't even if I tried) but give you just enough to work with, so that you can recognize a reference issue when it comes up and hopefully find a solution (or at least know what exactly to ask about).
# Alright, let's begin.
x = 42
y = 42
# A variable like that is often described as a 'container' that stores a value. This may be true in close-to-the-metal languages like C, but in Python, we need a different imagination.
# Only Python knows where and how each value you use in your code is created and deleted from computer memory. This is all taken out of your hand. Python is like a bank, guarding your money. You don't even touch the real cash, you only see the numbers on the screen of the ATM.
# A variable in Python is always only a 'reference' to the actual value. You can imagine it like an address card, or a phone number, something like that.
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run