Type Error


# Default list

class defaultlist:

# Created a list and a default value

    def __init__ (self,lst = [],default_value = ['Bye']):

        self.lst = lst

# defaul value if list is empty

        if self.lst == None:

            self.lst = default_value

            return self.lst

#Extend the createdlist

    def extend (self,extend):

        self.extend = extend

        return self.lst.extend(self.extend)

#Pop items either from the last index or the index stated

    def pop (self,ind):

        self.ind = ind

        if ind == None:

            return self.lst.pop(len(self.lst)-1)

        else:

            return self.lst.pop(ind)

    

# Remove items from the lst at the stated index

    def remove (self,remove):

        self.remove = remove

        self.lst.remove(self.remove)

lst1 = defaultlist([1,2,3,4,'Hi','One'])

print("this checks for extend",lst1.extend(30,40,50))

print("this checks for pop",lst1.pop())

print("this checks for remove",lst1.remove(0))

This is the error l am getting

PS C:\Users\simmy.vscode> & c:/Users/simmy/AppData/Local/Programs/Python/Python39-32/python.exe c:/Users/simmy/.vscode/test.py
Traceback (most recent call last):
File “c:\Users\simmy.vscode\test.py”, line 37, in
print(“this checks for extend”,lst1.extend(30,40,50))
TypeError: extend() takes 2 positional arguments but 4 were given
PS C:\Users\simmy.vscode>

Someone please help

Many Thanks,
Jagmeet

you are getting this error because you defined extend to only take one positional argument and you put in multiple arguments when you called extend.

if you want to extend to take in multiple arguments but not sure as to how many then use *

what do i mean by *

let me give you a example
when you want a function to take in multiple parameters but are not sure how many then you use *

example

def somefunction(*someparameter):
print(some_parameter)

somefunction(45, 68, 84)

more info on this topic read the below link

Over here I have just put extend and not specified the number of arguments,

Now this list above I thought I am passing one argument that represents extend

I will try solving the other problems after this is solved but if l have a problem/fail to solve I will post it here

Many thanks

Jagmeet

list.extend takes an iterable, then maps it to the existing list.

>>> lst = [10, 20, 30]
>>> lst.extend([40, 50, 60])   # list sequence
>>> lst
[10, 20, 30, 40, 50, 60]
>>> lst.extend(70, 80, 90)
Traceback (most recent call last):
  File "<pyshell#189>", line 1, in <module>
    lst.extend(70, 80, 90)
TypeError: extend() takes exactly one argument (3 given)
>>> lst.extend((70, 80, 90))    #  tuple sequence
>>> lst
[10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> lst.extend('abcdefghi')    # str sequence
>>> lst
[10, 20, 30, 40, 50, 60, 70, 80, 90, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> lst.extend({1, 2, 3, 4, 5, 6, 7, 8, 9})    # set sequence
>>> lst
[10, 20, 30, 40, 50, 60, 70, 80, 90, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 

@akshit.ireddy points out how the splat, or unpacking operator can turn a loose sequence into a list, which list can then be given to list.extend().


# Default list

class defaultlist:

# Created a list and a default value

    def __init__ (self,lst = [],default_value = ['Bye']):

        self.lst = lst

# defaul value if list is empty

        if self.lst == None:

            self.lst = default_value

            return self.lst

#Extend the createdlist

    def extend (self,*extend):

        self.extend = extend

        return self.lst.extend(self.extend)

#Pop items either from the last index or the index stated

    def pop (self,ind = None):

        self.ind = ind

        if self.ind == None:

            return self.lst.pop(len(self.lst)-1)

        else:

            return self.lst.pop(self.ind)

    

# Remove items from the lst at the stated index

    def remove (self,remove):

        self.remove = remove

        return self.lst.remove(self.remove)

    def __repr__(self):

        return self.lst

lst1 = defaultlist([1,2,3,4,'Hi','One'])

print("this checks for extend",lst1.extend(30,40,50))

print("this checks for pop",lst1.pop())

print("this checks for remove",lst1.remove(1))

print ("lst", lst1)

This is the error now

PS C:\Users\simmy.vscode> & c:/Users/simmy/AppData/Local/Programs/Python/Python39-32/python.exe c:/Users/simmy/.vscode/test.py
this checks for extend None
this checks for pop 50
this checks for remove None
lst Traceback (most recent call last):
File “c:\Users\simmy.vscode\test.py”, line 46, in
print (“lst”, lst1)
TypeError: str returned non-string (type list)
PS C:\Users\simmy.vscode>

Please help

ManyThanks

In order to think things through I use symbols. Names just clutter things for me, don’t know why.

def extend (self, *x):
    self.lst.extend(x)

If it doesn’t work, it at least tells us what we’re after. Might have to work super() into this. I’m tossing around ideas.

This error is regarding the repr method

Thanks