Feb-28-2021, 05:48 PM
(This post was last modified: Feb-28-2021, 05:48 PM by deanhystad.)
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.
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.