-1

I want to remove an object from a list using a the variable 'classname'

list = [A, A, B]                    #objects inside a list  
classname = input() .               #x = A
list.remove(classname)              #the problem is that classname is a string
                                    #and the list has no strings
Miguel Yurivilca
  • 375
  • 5
  • 12

1 Answers1

-1

Some objects are aware of their names. If A and B are classes, functions, methods, descriptors, or generator instances, you can use definition.__name__ to determine their names.

class A:
    pass

class B:
    pass

l = [A, B, A, B]
remove = "A"
print([x for x in l if x.__name__ != remove])
# [<class '__main__.B'>, <class '__main__.B'>]
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96