checking user defined types
eppstein at ics.uci.edu
Thu Jun 24 08:31:10 CEST 2004
In article <RmuCc.1142728$A6.4637536 at telenews.teleline.es>,
"Luis Solís" <lsolis at mu.intecsa-inarsa.es> wrote:
> I have defined a class Myclass
> I instanciate the class and I use it in a function, and I could like check
> the argument type in the function, but this code don't works
> func (xMyclass,..):
> if type(xMyclass) is type(Myclass): ...
> then I must create a new object of the class and then
> if type(xMyclass) is type(Myclass()):
> this solution has the problem when Myclass has a complex constructor.
> Do you known another solution ?
Why do you think an instance object should have the same type as a class
You could do
if type(xMyclass) is Myclass: ...
but that only works for new-style classes and also doesn't match
instances of subclasses of your class. Probably what you really want is
if isinstance(xMyclass, Myclass): ...
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
More information about the Python-list