I am trying to create Array which will get bunch of customization. However, I was stuck while adding custom insert method which just adds new value to the end of array. This is my code:
class Array():
type_object = [int, str, float]
count = 0
def __init__(self, typecode):
self.array = []
if typecode in self.type_object:
self.typecode = typecode
def __str__(self):
return f"{self.array}"
def __iter__(self):
return iter(self.array)
def insert(self, value):
try:
if type(value) != self.typecode:
raise ValueError("Invalid type")
except ValueError as ex:
print(ex)
self.array[self.count] = value
self.count += 1
a = Array(int)
a.insert(1)
"Classic" Error:
List index out of range
I want to avoid .append() method in order to make it "super-customized" so to say. How to allocate a fixed size of memory ahead of time? I have seen "[None] * n" approach but the problem is when you print it out it gives bunch of None))
Any recomendation appreciated.