如何在python中将以下内容组合成一个循环?
try:
[fb.delete() for fb in FacebookProfile.objects.filter(user_id=user.id)]
except FacebookProfile.DoesNotExist:
pass
try:
[fb.delete() for fb in FacebookUser.objects.filter(user_id=user.id)]
except FacebookUser.DoesNotExist:
pass
try:
[fb.delete() for fb in FacebookLike.objects.filter(user_id=user.id)]
except FacebookLike.DoesNotExist:
pass
try:
[fb.delete() for fb in FacebookInvite.objects.filter(user_id=user.id)]
except FacebookInvite.DoesNotExist:
pass
正如我们所看到的,它是相同的代码块,只是每个类中的类名都发生了变化,所以我希望遍历一个类名数组,并通过一个与上面相同语义的生成函数来运行每个类名。