Now that we have a better understanding of object,
it's time to look at an important related concept, that of variables.
In Python, a variable is a named reference to an object.
It's a line of code that looks like x equals 47,320.
You'll note that in comparison with other languages,
you don't need a semicolon at the end of variable declarations.
In this case, the variable is x,
it's a name x. Variables in Python are dynamically typed.
That means that they're declared without an explicit type.
So even though we just learned that objects have and know about a type, variables do not.
Variables are little more than a reference to a Python object.
In the language of our analogy,
a variable is a label for an item of baggage.
It's a label with the name x on it that's referring to some Python object that
itself has the value and the type and
any other important metadata associated with that object.
So how does Python keep track of all of these?
Well, Python uses the idea of namespaces.
Those will be mappings from names to objects.
If we have the lines of code x equals 47,320 and y equals the string "hello",
we end up with one mapping from the name x to a Python object,
and one mapping from the name y to some other Python objects,
perhaps off the slide.
But collectively these mappings are captured in
some symbol table in some space that maps names, to suitcases.
So the namespace here is important because it's
an association between the name x and the Python object,
as well as the name y and the Python object with the string hello inside of it.
Remember, from the side of Python,
namespaces are one honking, great idea.
So this fundamental idea makes an appearance all over the place in Python.
Indeed is even high enough level that we can access and manipulate
namespaces if we really want to get into the nitty gritty details of Python.
Namespaces are an important key in understanding how variable assignment works as well.
When you assign from a variable,
you don't copy an object,
but rather you add a reference to the same object.
If we have one line of code that says x equals
47,320 and another line of code that says y equals x,
in this case assigning from x to y,
we first understand that we live inside some namespace,
and inside that namespace we have an association between
the name x and the object containing 47,320.
When we assign x to the name y,
all we do is add an association from the name y to the same object.
Python always handles the creation and destruction of new objects for you.
So when we're doing manipulation with variables,
we are usually just changing what everything is referencing.
We are rarely creating or destroying Python objects our-self.
With all this in mind, let's take a look at variables in
the interactive interpreter to get a concrete understanding of this principle.
Let's go back to our example of some complex string.
We will have a string that says, "hello there."
If we take a new name y and associate it to the value at x,
what we end up with is an object referred to by x.
That's the same object as that referred to by y.
Concretely, we can ask whether x and y are the exact same object,
and indeed they are.
If we separately make an object under z that will start with hello,
and then change to have a new value, hello there.
Now, z is a different object.
Id of x is the same as it was before,
id of z is a different object.
So if we ask whether x is the exact same as z, it is false.
One thing we can use to access this information,
is we can use the locals built-in function
to try to access the namespace in the local environment.
If we ask for the locals association at x with syntax that's currently mysterious,
but that will eventually become clear,
we see that x refers to some name,
y refers to some object,
and z refers to some object.
So this locals mysterious thing is somehow maintaining information between names.
In this case as strings in the Python objects we're working with.
The real key thing here is the idea that namespaces are
associations between names and objects.
Variable assignment changes references but does not create new objects.
To recap what we've just seen,
we saw that everything in Python is an object.
That objects have type and identity,
and that an object can be thought of as a suitcase.
We also saw that you can use double equals to compare
values and 'is' to compare identities.
Seeing one, if the suitcases contain the same things,
and is to see if they are literally the same suitcase.
We also saw that variables are just names associated to some object via a namespace,
a symbol table mapping names to objects.
Where variable assignment adds new local references to objects and does not copy objects.
Where in this case, the variable is something like a baggage tag pointing to a suitcase.
Lastly, we also saw that
Python's dynamic typing system in which
objects have types but variables do not have types,
supports the idea of duck-typing and promotes
interface based generic programming that we'll see not only in the rest of this lesson,
but also throughout the lessons in the rest of this course.