Reputation: 639
I searched other posts in stackoverflow and even copied them to try on my machine as answered. However, it keep failing throwing "TypeError"
# this is as one of other post in StackOverflow.
class ListClass(list):
def __init__(self, *args):
super().__init__(self, *args)
self.append('a')
self.name = 'test'
I also tried empty class with pass. But, that also fails if I inherit and I guess I missed something instead of adding something more or wrong?
1) what is this "TypeError"
and why?
2) how to fix it?
further snapshot on TypeError:
>>> class ListClass(list):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (3 given)
Upvotes: 4
Views: 14368
Reputation: 78546
Pass the tuple of arguments args
to your instance's extend
:
>>> class ListClass(list):
... def __init__(self, *args):
... super().__init__()
... self.extend(args)
... self.append('a')
... self.name = 'test'
...
>>> ListClass(1, 2, 3)
[1, 2, 3, 'a']
Or pass the unpacked tuple of args
to super
's __init__
since list
can be initialized with an iterable
>>> class ListClass(list):
... def __init__(self, *args):
... super().__init__(args) # one argument: tuple
... self.append('a')
... self.name = 'test'
...
>>> ListClass(1, 2, 3)
[1, 2, 3, 'a']
Update:
What you've probably done is assigning an object to the name list
:
>>> list = []
>>> class ListClass(list):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (3 given)
Upvotes: 3
Reputation: 487
You are giving it to many paramerters when you create the ListClass object, this is what you are doing:
s = list(2,3,3)
This is what you shuold be doing:
s = list([2,3,3])
Try this snippets on you interpreter.
Upvotes: 6