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

Recommended Answers

All 3 Replies

Anyone ever come across this?

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.