TypeError: **str** returned non-string (type list)

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)

This is the error

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

Could you show the rest of your code as well? I’m not seeing anything in this part that might explain the error. What’s lst1?


# 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)

Take a closer look at the __repr__ method. The value returned by it should be a string.

1 Like

# 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 str(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)

It works now

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.