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
#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


Messages In This Thread
RE: How to define a variable in Python that points to or is a reference to a list member - by deanhystad - Feb-28-2021, 05:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Project Structure for Modularity and Reusability with Multiple Entry Points b19wh33l5 0 1,734 Apr-24-2024, 12:21 PM
Last Post: b19wh33l5
  Class member become static Quasar999 1 1,374 Sep-16-2023, 12:52 PM
Last Post: deanhystad
  Define Variable xinyulon 5 2,513 Nov-03-2022, 01:12 AM
Last Post: Larz60+
  Split string using variable found in a list japo85 2 2,202 Jul-11-2022, 08:52 AM
Last Post: japo85
  can Inner Class reference the Outer Class's static variable? raykuan 6 9,164 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Points not plotting with reference to x-tick labels Mark17 2 2,137 Jun-14-2022, 05:38 PM
Last Post: Mark17
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 2,678 Dec-15-2021, 01:53 PM
Last Post: deanhystad
  An IF statement with a List variable dedesssse 3 23,096 Jul-08-2021, 05:58 PM
Last Post: perfringo
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 3,203 May-07-2021, 12:39 AM
Last Post: MDRI
  Determine number of all the immediately adjacent points in python zackk 1 2,492 Feb-06-2021, 09:23 AM
Last Post: zackk

Forum Jump:

User Panel Messages

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