Python Forum
How to define a variable in Python that points to or is a reference to a list member - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to define a variable in Python that points to or is a reference to a list member (/thread-32723.html)



How to define a variable in Python that points to or is a reference to a list member - JeffDelmas - Feb-28-2021

I have a Python list, r[], and would like to define references, or aliases to specific members of the list. This is easy to do in languages with pointers (like C or C++), but I don't see how to do this in Python.

Specifically, I'd like do something like this:

r[0] = 1
ac (=addressof) r[0]
ac = 2

Now if I evaluate r[0] I need it to be 2

Is there a way to define such a reference in Python? I'm not looking for defining a class such that objects of that class can be references to each other. I'm looking for a simple means of pointing one variable to any other variable, but my current specific problem is a reference to a list member.


RE: How to define a variable in Python that points to or is a reference to a list member - deanhystad - Feb-28-2021

I think you are asking the wrong question. Instead of asking "How can I do this thing I do in C for solving some problem?", skip the preamble and ask "How do I solve this problem?"

It's possible that Numpy arrays may work for your problem, but there is probably a better solution to the actual problem if you forget about trying to make Python work like C.

That said, you can always write a class.
class ArrayRef():
    def __init__(self, array, index=0, length=None):
        if length is None:
            length = len(array) - index
        self.length = length
        self.array = array
        self.index = index

    def __getitem__(self, index):
        if index >= self.length:
            raise IndexError
        return self.array[self.index + index]

    def __setitem__(self, index, value):
        if index >= self.length:
            raise IndexError
        self.array[self.index + index] = value

    def __iter__(self):
        return iter(self.array[self.index:self.index+self.length])

    def __repr__(self):
        return repr(self.array[self.index:self.index+self.length])

array = list(range(1, 21))
array_ref = ArrayRef(array, 5,  10)
array_ref[3] = 3.14
array[10] = -5
print(array)
print(array_ref)
print(sum(array_ref))
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 3.14, 10, -5, 12, 13, 14, 15, 16, 17, 18, 19, 20] [6, 7, 8, 3.14, 10, -5, 12, 13, 14, 15] 83.14
Still think you should reevaluate the problem you are trying to solve.


RE: How to define a variable in Python that points to or is a reference to a list member - JeffDelmas - Feb-28-2021

Thanks for the response.

I'm actually teaching a class in computing to seniors and I use Python for its simplicity in my programming examples. I've implemented a simple hypothetical processor simulator to illustrate machine language and assembly language concepts (opcodes, memory addressing, etc.) to seniors who use computers but have no idea how they work. In my simulated machine, I have a set of registers implemented as a Python list. For simplicity, I want the accumulator to be reg[0] and the general registers to be reg[1] through reg[maxreg]. My goal is to emphasize reg[0] as the accumulator by using a reference called ac. This is not something I'd normally even try to do in Python and have never had the need to do it in the past. But I would like to do it here more for pedagogical reasons. I don't want to complicate things with a class implementation. The Numpy array recommendation may be the best approach, but again, I'm trying to keep this code very simple and I'm avoiding libraries if possible.

I'm OK just telling the students that reg[0] is the accumulator or in having a separate ac variable that has nothing to do with the reg[] list. If that's the best and simplest solution, I'm fine with that.

Thanks again for responding.


RE: How to define a variable in Python that points to or is a reference to a list member - deanhystad - Feb-28-2021

I would write a class to represent the accumulator and general registers. It could be a very simple class. Or you could define a literal constant AC so registers[AC] is the accumulator.


RE: How to define a variable in Python that points to or is a reference to a list member - JeffDelmas - Feb-28-2021

Yeah, I think I'll do what you recommend and define a literal constant AC = 0 so reg[AC] is the accumulator. Thanks again.