469,916 Members | 2,562 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

checking user defined types

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 ?

Thanks in advance
Jul 18 '05 #1
1 1431
In article <Rm**********************@telenews.teleline.es>,
"Luis Solís" <ls****@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
object?

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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tongu? Yumruk | last post: by
67 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Les Stockton | last post: by
125 posts views Thread by jacob navia | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.