checking user defined types

Discussion in 'Python' started by Luis Solís, Jun 24, 2004.

  1. Luis Solís

    Luis Solís Guest

    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
     
    Luis Solís, Jun 24, 2004
    #1
    1. Advertising

  2. In article <RmuCc.1142728$>,
    "Luis Solís" <-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
     
    David Eppstein, Jun 24, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guilherme Pinto
    Replies:
    1
    Views:
    317
    Victor Bazarov
    Oct 10, 2003
  2. grahamo
    Replies:
    4
    Views:
    1,002
    Thomas Wintschel
    Feb 23, 2004
  3. Debo
    Replies:
    3
    Views:
    624
    Swampmonster
    Dec 12, 2004
  4. Oodini
    Replies:
    1
    Views:
    1,802
    Keith Thompson
    Sep 27, 2005
  5. Replies:
    1
    Views:
    521
    Sion Arrowsmith
    Jul 10, 2008
Loading...

Share This Page