In Python, list is implemented by pointer array, so the elements are actually pointers. How can I get the address of the pointers?

For example:

a = [1, 2]
print(id(a[0])

I can only get the address of element’s dereference, the address of object ‘1’.

How can I get the address of the element itself?

1

Python doesn’t have the concept of “address” or “lvalue”.
For example the only ways you can have a called function to mutate an element of an array or an attribute of a class is either

  1. pass the array and the index, or pass the class instance and the attribute name
  2. pass a setter function

Example of mode 1 is

def mutator(array, index, value):
    array[index] = value

mylist = [1, 2, 3]
mutator(mylist, 1, 99) # now list is [1, 99, 3]

example of mode 2 is

def mutator(setter, value):
    setter(value)

class P2d:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = P2d(10, 20)
L = [1, 2, 3]
def x_changer(value):
    p.x = value
def item_changer(array, index):
    def f(value):
        array[index] = value
    return f
mutator(x_changer, 99) # now p.x is 99
mutator(item_changer(L, 1), 99)  # now L is [1, 99, 3]