I have created a subclass called list that inherits list. I have managed to create .push() as it is in Javascript, however when I made push it did not show up as a method.
I tried this:
class list(list):
def push(self,element):
list.append(element)
l=len(self)
return l
def pop(self):
x=self[len(self)-1]
list.remove(x)
return x
array=[0,1,2,3,4]
print(array.pop())
print(array.push(4))
Which got me:
4
Traceback (most recent call last):
File "c:Usersdefault.n100-cottageDownloadsCodePythonClass-Testing.py", line 16, in <module>
print(array.push(4))
^^^^^^^^^^
AttributeError: 'list' object has no attribute 'push'
I also tried:
class list(list):
def push(self,element):
list.append(element)
return len(list)
def pop(self):
x=self[len(self)-1]
list.remove(x)
return x
array=[0,1,2,3,4]
print(array.pop())
print(array.push(4))
Which got me:
4
Traceback (most recent call last):
File "c:Usersdefault.n100-cottageDownloadsCodePythonClass-Testing.py", line 15, in <module>
print(array.push(4))
^^^^^^^^^^
AttributeError: 'list' object has no attribute 'push'
Thanks in advance for any help you might render.
New contributor
SuperC SuperC is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.