Confusion within python pointers

Hi. I am currently working on the Blossom project in CS102 regarding HashMap data structure ( One thing that I am confuse is that how am I able to update the value inside a list just by using the reference variable?
I’ve searched through the internet and I’ve learned that there is no such thing as pointer in Python, therefore how could this happened?

In the first picture, Notice the 21th line in the code. The “node” variable is just the value yielded by the function in LinkedList class.

The second pic shows the code that yield the list to the function caller.

In the third pic, when I created my own class and try to reassign the value using the same way. It doesn’t work!

Any help is greatly appreciated!


A few notes:

  • python doesn’t support pointers (that’s more for C/C++ type languages). Sometimes for certain algorithms there’s a notion of pointer used but if you see that it will always be a pseudopointer (i.e. not really a pointer)
  • in object-oriented programming, you generally don’t use the assignment operator = unless its overloaded. What does this mean?

If I have a student object: myStudent (note: I haven’t looked at your class methods, these are very generic examples):

  • to change it I have to use some class method like: myStudent.setName("Johnny")
  • in some languages you can re-assign values via specific constructor (here assume the constructor just takes in a name: myStudent = Student("Johnny")… but this is not what you want to do in your scenario.

In your example you saved the value into a variable. But that variable isn’t a data member of your Student object. You could save it into a newVariable and then student1.set(newVariable) or simply enter the number directly as an argument.


If you’re trying to make your code more object oriented then toastedpitabread’s method would be sensible. To answer the question about updating the contents of the list it would help to understand how names work in Python.

Moderate length read ahead, the shorter description is at the end…

Python is a little different to some, names are merely references to objects. An object can have multiple names attached to it and names can be removed from this object and attached to another object more or less whenever you want. This is often compared to adding labels to a physical object, you simply pull your label off one object and stick it on another.

This has a couple of unexpected consequences if you treat Python’s name like variables.Firstly it is worth nothing that assignment isn’t altering any existing memory, it’s a method of attaching a name to an object.

x = 3
y = x
x = 4
# x now refers to a completely different object
print(y)  # Out: 3

In the example above there are two int objects. The assignment is merely changing what object the name refers to. You can use the id function if you want to test this out a little.

Secondly any changes to an object would inevitably be reflected by any reference to that object since it’s the same object-

lst = [1]
other_name = lst
print(lst)  # Out: [1, 2]

This is of course only applicable to mutable objects since instances of something like a tuple or int are objects which cannot be modified.

Thinking this way might take a little while and there are some much better resources out there describing how Python deals with names and objects that would be worth looking into. So back to the list for now you can treat a list like an array of references. So when you alter an element of the list, e.g. lst[3] = "Boo" you’re just reassigning the reference to a different object.

Back to the actual issue-

In the second example you attach a name stud1_value to the object returned from .get_value(). But in the assignment afterwards you merely reassign the name stud1_value to a different object.

When it comes to int in Python there is no route to change the object itself (ignoring silly hacks). If you want to change what value your Student instance refers to then you’ll need to reassign the .value attribute of that object. Given you’ve written a setter you may as well use it.

If it were a mutable object then you could change the contents of said object but that’s really just adding a layer of misdirection for the reassignment of a reference (may be useful in the right circumstances but probably not in this example).


@tgrtim @toastedpitabread Thanks for the detailed explanation, I’m clear now. Really appreciate it!

1 Like