我想使用变量'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
答案 0 :(得分:-1)
某些对象知道其名称。如果A
和B
是类,函数,方法,描述符或生成器实例,则可以使用definition.__name__
确定其名称。
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'>]