Python Forum
How to define a variable in Python that points to or is a reference to a list member
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to define a variable in Python that points to or is a reference to a list member
#1
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.
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
Yeah, I think I'll do what you recommend and define a literal constant AC = 0 so reg[AC] is the accumulator. Thanks again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class member become static Quasar999 1 643 Sep-16-2023, 12:52 PM
Last Post: deanhystad
  Define Variable xinyulon 5 1,195 Nov-03-2022, 01:12 AM
Last Post: Larz60+
  Split string using variable found in a list japo85 2 1,237 Jul-11-2022, 08:52 AM
Last Post: japo85
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,772 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Points not plotting with reference to x-tick labels Mark17 2 1,220 Jun-14-2022, 05:38 PM
Last Post: Mark17
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 1,873 Dec-15-2021, 01:53 PM
Last Post: deanhystad
  An IF statement with a List variable dedesssse 3 7,948 Jul-08-2021, 05:58 PM
Last Post: perfringo
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,283 May-07-2021, 12:39 AM
Last Post: MDRI
  Determine number of all the immediately adjacent points in python zackk 1 1,824 Feb-06-2021, 09:23 AM
Last Post: zackk
  Create variable and list dynamically quest_ 12 4,287 Jan-26-2021, 07:14 PM
Last Post: quest_

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020