Why would I use inspect.isclass()?
Steve Holden
steve at holdenweb.com
Thu Dec 30 08:41:25 EST 2004
Martin v. Löwis wrote:
> it's me wrote:
>
>> Okay, Nick, I didn't know you can pass a "Class" rather then an
>> instance. I
>> have to chew on what your example does.
>>
>> But no, I want to pass an instance of a Class. But how do I know that
>> the
>> calling routine *did* pass me a class - pardon me: an instance of a
>> Class?
>
[...]
>
> The real purpose of why you have *inspect*.isclass is for inspecting.
> For example, assume I wanted to display the contents of module httplib.
> I would need to find out what the things in httplib are, and I do this
> with
>
> >>> for x in dir(httplib):
> .... print x,
> .... x = getattr(httplib, x)
> .... if inspect.isclass(x):print "class"
> .... elif inspect.isfunction(x):print "function"
> .... elif inspect.ismodule(x):print "module"
> .... else: print "something else"
> ....
This technique is often called "introspection", and involves having
programs indulge in the time-honored practice of contemplating their own
navels. The ability to introspect is one side of a major dichotomy in
programming languages. A C program has no real ability to introspect,
but Python and SmallTalk (and Java) programs do.
But it isn't really a novice topic, and many programmers spend entire
careers quite happily without using introspection.
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
More information about the Python-list
mailing list