Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Regular Expressions in Python Introduction to Regular Expressions Reading Files

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

How is a pointer different from a variable?

I'm hoping this isn't a dumb question.

Whenever I've created something like the list below, I've called it a "variable".

my_list = [1, 2, 3]

However, Kenneth, the treehouse python teacher, said that the following is a "pointer".

my_file = open(thisfile.txt, encoding='utf-8')

I thought up until now that whenever I was assigning anything - a list, int, object, etc. - to something then that something was a variable. Basically, the term "pointer" is new to me. Can someone explain the difference between a variable and pointer?

2 Answers

Gianpaul Rachiele
Gianpaul Rachiele
4,844 Points

A variable is an integer, list, string, or some other datatype that is assigned to one name whereas a pointer is when two different names are assigned to the same thing.

i.e.

i = [1, 3, 5]
j = i

In this example i and j are both going to return the same thing. So they are pointers. I hope this makes sense.

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Thanks, Gianpaul! So, is it fair to say that the line below is a variable since it's a file object?

my_file = open(thisfile.txt, encoding='utf-8')

If I wrote the line below, then another_file would be a pointer, right?

another_file = my_file
Gianpaul Rachiele
Gianpaul Rachiele
4,844 Points

Exactly, the difference is that with a variable it is tied to one name whereas with a pointer there is sort of "levels". There is a variable which has a name then there is another new name that points to the original variable.

Gabriel Precup
Gabriel Precup
4,565 Points

In Python, actually, I've read that pointers actually don't exist/are not used, but I could explain what he may be referring to. Pointers actually don't save any type of data. They store an address in the computer's memory that stores the value of a variable.

So for example if you have a pointer named 'p', you can store a variable's address, let's say 'x''s. Think of this address as a box. In this box, you have the value of x, that can be changed as usual.

In the context given in the video, I don't thing it's referring literally to a pointer, but to the name of the file that, through this function, sends you to the value of the file.