Pointers in Python

Pointers in Python?

There's no way you can do that changing only that line. You can do:

a = [1]
b = a
a[0] = 2
b[0]

That creates a list, assigns the reference to a, then b also, uses the a reference to set the first element to 2, then accesses using the b reference variable.

Understanding pointers and merging lists in python

Every Python variable is a reference to a value; if you already understand how pointers work in C, then it might be useful to think of variables which reference mutable objects as working like C pointers. They aren't really the same thing, but a Python variable that references a ListNode acts a lot more like a C ListNode* than a C ListNode, so the pointer metaphor is a good conceptual starting point if that's the language you're coming from.

  1. dummy always points to the node that you created in the first line of the function, which is the same node that cur starts at. The first time you update cur.next, you are also updating dummy.next. cur is then reassigned to a new node, but the modification you made to dummy persists.

This Python code:

        cur = dummy = ListNode()
while list1 and list2:
if list1.val < list2.val:
cur.next = list1
list1, cur = list1.next, list1

is essentially the same as:

        ListNode* cur, dummy;
cur = dummy = new ListNode();
while (list1 && list2) {
if (list1->val < list2->val) {
cur->next = list1;
cur = list1;
list1 = list1->next;
}
}

  1. One is modifying the next attribute of the node that cur points to; the other is modifying cur itself to point at a new node.
                cur.next = list2
cur = cur.next

is the same as:

                cur->next = list2;
cur = cur->next;

For a more in-depth explanation of how exactly variables work in Python, read https://nedbatchelder.com/text/names.html

Are Python variables pointers? Or else, what are they?

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)

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

function pointers in python

Did you try it? What you wrote works exactly as written. Functions are first-class objects in Python.

How to implement pointers in Python? (or any similar solution else)

Python doesn't need pointers in order to achieve this as every variable is a reference to an object. These references are slightly different from C++ references, in that they can be assigned to - much like pointers in C++.

So to achieve what you're looking for, you'd just need to do something like this:

class A(object):
def __init__( self, connections, sum ):
self.connections = connections
self.sum = sum

def passToConnections( self, index ):
self.connections[ index ].receive( self.sum )

def receive( self, input ):
self.sum += input

And just to prove that this works as expected:

>>> a1 = A( [], 0 )
>>> a2 = A( [], 0 )
>>> a3 = A( [ a1, a2 ], 10 )
>>> a3.passToConnections( 0 )
>>> a3.passToConnections( 1 )
>>> a3.passToConnections( 1 )
>>> print a1.sum
10
>>> print a2.sum
20

So as you can see we have altered the original objects a1 and a2 by calling through to them via the references in a3

Simulating Pointers in Python

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


Related Topics



Leave a reply



Submit