How can I iterate over a list of all classes loaded in memory?
I’m thinking of doing it for a backup, looking for all classes inheriting from db.Model (Google App Engine).
Thanks,
Neal Walters
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In “normal” Python, you can reach all objects via the
gc.getobjects()function of thegcstandard library module; it’s then very easy to loop on them, checking which one are classes (rather than instances or anything else — I do believe you mean instances of classes, but you can very easily get the classes themselves too if that’s really what you want), etc.Unfortunately, the
gcmodule in App Engine does NOT implementgetobjects— which makes it extremely difficult to reach ALL classes. For example, a class created by calling:and hidden into a list somewhere, IS going to be very difficult to reach.
But fortunately, since you say in your question’s text that you only care about subclasses of
db.Model, that’s even easier thangcwould allow:Just make sure you explicitly ignore such classes you don’t care about, such as
Expando;-).Note that this DOES give you only and exactly the CLASSES, not the instances — there is no similarly easy shortcut if those are what you’re really after!