Python 3.x 输入类名并从列表中删除该类的对象
我想使用变量“classname”从列表中删除一个对象Python 3.x 输入类名并从列表中删除该类的对象,python-3.x,list,class,Python 3.x,List,Class,我想使用变量“classname”从列表中删除一个对象 list = [A, A, B] #objects inside a list classname = input() . #x = A list.remove(classname) #the problem is that classname is a string #an
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
有些对象知道它们的名称。如果
A
和B
是类、函数、方法、描述符或生成器实例,则可以使用来确定它们的名称
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'>]
A类:
通过
B类:
通过
l=[A,B,A,B]
remove=“A”
打印([x代表l中的x,如果x.\uuuuu name\uuuuuu!=删除])
# [, ]
A和B是什么?你不能@它们是物体