We call them references. They work like this
i = 5 # create int(5) instance, bind it to i
j = i # bind j to the same int as i
j = 3 # create int(3) instance, bind it to j
print i # i still bound to the int(5), j bound to the int(3)
(Sidenote: Small ints are interned, but that isn't important to this explanation)
i = [1,2,3] # create the list instance, and bind it to i
j = i # bind j to the same list as i
i[0] = 5 # change the first item of i
print j # j is still bound to the same list as i
Answer from John La Rooy on Stack OverflowWhy doesn't Python have pointers?
Why is there no concept of a pointer or pass-by reference in python? Is there something I can use
We call them references. They work like this
i = 5 # create int(5) instance, bind it to i
j = i # bind j to the same int as i
j = 3 # create int(3) instance, bind it to j
print i # i still bound to the int(5), j bound to the int(3)
(Sidenote: Small ints are interned, but that isn't important to this explanation)
i = [1,2,3] # create the list instance, and bind it to i
j = i # bind j to the same list as i
i[0] = 5 # change the first item of i
print j # j is still bound to the same list as i
Variables are not pointers. When you assign to a variable you are binding the name to an object. From that point onwards you can refer to the object by using the name, until that name is rebound.
In your first example the name i is bound to the value 5. Binding different values to the name j does not have any effect on i, so when you later print the value of i the value is still 5.
In your second example you bind both i and j to the same list object. When you modify the contents of the list, you can see the change regardless of which name you use to refer to the list.
Note that it would be incorrect if you said "both lists have changed". There is only one list but it has two names (i and j) that refer to it.
Related documentation
- Execution Model - Naming and Binding
No, really. I've just started practicing problems on Leetcode and today I tried to do a bunch of things with Linked lists in python. I always second guess myself when I'm writing assignment statements like node = next_node because I'm worried that using shallow copies might screw things up for me.
Wouldn't it have been way easier to directly reference objects by their memory location?
I'm not going to use C for coding interviews so I'd also appreciate any ways I can idiot proof my code when I'm dealing with moving and assigning objects in Python.
One idea I had was to include a deepcopy method in the List node class definition but I can't make any changes to the leetcode question setup.
I am a C++ and assembly developer mainly, but as someone entering the job market that just doesn't cut it. I have a job I got hired for where I'm taking 4+ years of a python codebase that no one has documented and is spaghetti and updating it, and was supposed to be adding multithreading (EXCEPT THERE IS NO TRUE MULTITHREADING SO I HAVE TO DO MULTIPROCESSING WHICH IS A CLUSTER-F***). So that blows.
BUT
why is there no pointers!? or way to go references? I need to store data in an object that gets passed into some libraries, and then the library runs callbacks where I analyze the crap. From there I need to set the values so in other callbacks I can work with the values. It's a dictionary so I can add some new keys to store the data... except I can't because it's like pass by value so the data instantly gets deleted once it returns from the callback. If I passed in a pointer I'd be set, except that's not possible...
That's just one issue in a mirad of issues I'm facing with this junk language. Not to even mention the horribleness of everything using like 2gb of memory because each process copies the entire memory space of the head process....
Please tell me there's some kinda object or pointer thing I can use.
EDIT : I got the help I needed here, thanks everyone! sorry to trash the language a bit above I was just frustrated lol
This can be done explicitly.
class ref:
def __init__(self, obj): self.obj = obj
def get(self): return self.obj
def set(self, obj): self.obj = obj
a = ref([1, 2])
b = a
print(a.get()) # => [1, 2]
print(b.get()) # => [1, 2]
b.set(2)
print(a.get()) # => 2
print(b.get()) # => 2
You may want to read Semantics of Python variable names from a C++ perspective. The bottom line: All variables are references.
More to the point, don't think in terms of variables, but in terms of objects which can be named.