Подход 1: Добавить print_all()
метод
def print_all(self):
print(self.Array[:self.length])
Подход 2: Создать строковое представление класса
def __str__(self):
return str(self.Array[:self.length])
Простой тест:
arr = myArray()
arr.push(5)
arr.push(2)
arr.push(3)
arr.push(5)
arr.push(4)
arr.push(6)
arr.print_all()
print(arr)
Вывод:
Hello
Hello
Hello
Привет
Привет
Привет
[5, 2, 3, 5, 4, 6]
[5, 2,3, 5, 4, 6]
Полное определение класса:
import ctypes
class myArray(object):
def __init__(self):
self.length = 0
self.capacity = 1
self.Array = self.make_array(self.capacity)
def push(self, item):
if self.length == self.capacity:
self.resize(2*self.capacity)
self.Array[self.length] = item
self.length += 1
print("Hello")
def getitem(self, index):
if index >= self.length:
return IndexError('Out Of Bounds')
return self.Array[index]
def resize(self, new_cap):
newArray = self.make_array(new_cap)
for k in range(self.length):
newArray[k] = self.Array[k]
self.Array = newArray
self.capacity = new_cap
def make_array(self, new_cap):
return (new_cap * ctypes.py_object)()
def print_all(self):
print(self.Array[:self.length])
def __str__(self):
return str(self.Array[:self.length])