Prev: - Language execution model | Next: Variables and Dynamic typing
- (a) a name 'x' (a simple sequence of characters, an identifier) is created somewhere in the system memory
- (b) an object (a specific data structure) with value '1' is created somewhere in memory
- (c) a binding between name x and object '1' is established (that is: a pointer is created directing from 'x' to '1')
This binding is typically called 'object reference'
It is advisable to use online pythontutor to visualize execution of Python code
See how pythontutor visualizes object reference
- x = 1 (a name is almost always created ('is born') by some assignment command)
- x = 'spam' (other assignments may alter the name binding, that is its 'object reference')
- del x (one way for a name to 'die' - that is, expelled from the namespace- is by using the 'del' command)
x=1
x='spam'
print(x)
del x
print(x)
x = 1
y = 2.5
x + y
# Just writing the expression will present the outcome
x = 1
y = 2.5
print(x, y, 2*x**y)
# Better use print() when you want to present more than one values
import module_name
import random
print(random.randint(1,10))
from random import *
print(randint(1,10))
import random as rn
print(rn.randint(1,10))
- (a) Use a two-letter alias for the imported module, so the overload from writing the prefix is minimized
- (b) You still have a visual cue about the namespace. You easily understand, for example, that the 'randint' function comes from the 'rn' module and this is a very very useful information in developing and debugging.
. Free learning material
. See full copyright and disclaimer notice