How can I combine the following into a loop in 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
As we can see it’s the same code block, just with the class name changing in each one, so I’m looking to iterate over an array of class names and run each through a generated function with the same semantics as above.
1 Answer