What is the class of myobj?
Erik Max Francis
max at alcyone.com
Wed Oct 16 01:06:01 EDT 2002
JXStern wrote:
> Sometimes it's too easy, I guess. I actually looked at isinstance()
> but somehow didn't note the second parameter? Is that possible? No,
> I looked at isclass(), which only takes one! Oy.
>
> Now, you raise the issue whether I want to use it anyway. Have to
> think about that. I just wanted to assert the type of an argument
> being passed in, but maybe that's not Pythonic.
It usually isn't. When you're expecting a sequence type is a good
example, because there are some basic builtin sequence types (tuples and
lists), but there's also builtin functions which generate objects that
_act_ like builtin sequence types (for instance, the return value of
xrange or F.xreadlines), and of course there are user-defined sequence
types.
These are good examples where there really isn't a good way to test for
"sequencity" except by just using. Asserting on the type will _exclude_
a lot of potential objects which do export a sequencing interface but
which aren't actually (say) tuples or lists. If you really do want to
"preflight" the object, perhaps asserting on some elementary
functionality of a sequence object might be appropriate, such as:
assert a[0] # provided you know there's at least one element!
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ Seat thyself sultanically among the moons of Saturn.
\__/ Herman Melville
PyUID / http://www.alcyone.com/pyos/uid/
A module for generating "unique" IDs in Python.
More information about the Python-list
mailing list