Why would I use inspect.isclass()?
me at yahoo.com
Thu Dec 30 09:03:37 CET 2004
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?
"Nicolas Fleury" <nid_oizo at yahoo.com_removethe_> wrote in message
news:kDNAd.30796$%k.2417869 at weber.videotron.net...
> It's me wrote:
> >>I guess another example would be an assert on the type of argument:
> >>def foo(someClass):
> >> assert inspect.isclass(someClass)
> >> # rest of code
> > But that would always fail! (I just tried it).
> Are you sure you haven't pass an instance instead of a class? Remember,
> classes are also objects in Python:
> >>> import inspect
> >>> class A: pass
> >>> def foo(a): assert inspect.isclass(a)
> >>> foo(A)
> Works fine and makes sense, no? Of course, if I pass an instance
> instead of a class it would fail:
> >>> myA = A()
> >>> foo(myA)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 1, in foo
> Maybe your question is in what situations passing a class can be useful?
> There's many examples and once you're used to that capability it can
> be powerful. One simple example could be the generation of
> documentation; since you usually want to doc your classes, not
> instances. So it would make sense to do something like:
> def writeHtmlDoc(file, someClass): ...
More information about the Python-list