checking user defined types

L

Luis Solís

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
 
D

David Eppstein

"Luis Solís said:
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): ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top