0

Hey everyone,

I have a set of classes which, when instantiated, take up a lot of memory. I'm trying to write a program which can inspect class attributes and methods given a list of classes, without actually instantiating them. Let's say I wanted to see if the following class had an attribute called "foo"

class Test(object):
   foo=str()
   bar=int()

I know I can do something like:

test=Test()
if test.f is not None:
    print 'found'

However, is it possible to do this without instantiating Test? In reality, I need to do this with 10 objects, all of which are rather large and when instantiated, take up a great deal of memory and slow down my code.


Thanks

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by hughesadam_87
0

In principle, you can inspect the static members, either with dir(Test) or inspect.getmembers(Test), but you can't inspect instance attributes because they are usually created by __init__(). If the Test class, and/or classes in its mro implement __slots__ attributes, you can have some instances attribute names.

0

Thanks Gribouillis. I didn't know of this inspect module, which seems to be precisely what I needed.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.