Easy Q: dealing with object type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik Johnson

    #1

    Easy Q: dealing with object type


    I quickly browsed through section 9 of the Tutorial, tried some simple
    Google searches: I'm not readily seeing how to test class type. Given some
    object (might be an instance of a user-created class, might be None, might
    be list, might be some other "standard" type object instance), how do you
    test its type?


    Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
    [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    .... pass
    ....[color=blue][color=green][color=darkred]
    >>> obj = A()
    >>> obj[/color][/color][/color]
    <__main__.A instance at 0x81778d4>

    # Here, it's fairly obvious its an A-type object. A as defined in module
    '__main__'.

    # Some of the comparisons against "Python primitives", work as you might
    expect...[color=blue][color=green][color=darkred]
    >>> int[/color][/color][/color]
    <type 'int'>[color=blue][color=green][color=darkred]
    >>> type(3) == int[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> ls = range(3)
    >>> ls[/color][/color][/color]
    [0, 1, 2][color=blue][color=green][color=darkred]
    >>> type(ls)[/color][/color][/color]
    <type 'list'>[color=blue][color=green][color=darkred]
    >>> type(ls) == list[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> type({}) == dict[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> type(3.14) == float[/color][/color][/color]
    1


    # but this doesn't seem to extend to user-defined classes.[color=blue][color=green][color=darkred]
    >>> dir(obj)[/color][/color][/color]
    ['__doc__', '__module__'][color=blue][color=green][color=darkred]
    >>> obj.__module__[/color][/color][/color]
    '__main__'[color=blue][color=green][color=darkred]
    >>> type(obj)[/color][/color][/color]
    <type 'instance'>[color=blue][color=green][color=darkred]
    >>> type(obj) == A[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> type(obj) is A[/color][/color][/color]
    0

    # The following "works", but I don't want to keep a set of instances to
    compare against[color=blue][color=green][color=darkred]
    >>> obj2 = A()
    >>> type(obj) == type(obj2)[/color][/color][/color]
    1



  • Daniel Bickett

    #2
    Re: Easy Q: dealing with object type

    On Erik Johnson wrote:[color=blue]
    > # The following "works", but I don't want to keep a set of instances to
    > compare against[color=green][color=darkred]
    > >>> obj2 = A()
    > >>> type(obj) == type(obj2)[/color][/color]
    > 1[/color]

    How about:
    [color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    pass[color=blue][color=green][color=darkred]
    >>> class B:[/color][/color][/color]
    pass[color=blue][color=green][color=darkred]
    >>> objA = A()
    >>> type( objA ) == type( A() )[/color][/color][/color]
    True

    then again....
    [color=blue][color=green][color=darkred]
    >>> objB = B()
    >>> type( objA ) == type( B() )[/color][/color][/color]
    True

    they're both of type 'instance'. So how about this:
    [color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    pass[color=blue][color=green][color=darkred]
    >>> class B( object ):[/color][/color][/color]
    pass[color=blue][color=green][color=darkred]
    >>> objA = A()
    >>> objB = B()
    >>> type( objA )[/color][/color][/color]
    <type 'instance'>[color=blue][color=green][color=darkred]
    >>> type( objB )[/color][/color][/color]
    <class '__main__.B'>[color=blue][color=green][color=darkred]
    >>> type( objB ) == B[/color][/color][/color]
    True

    I believe that achieves what you were aiming for.

    --
    Daniel Bickett
    dbickett at gmail.com

    Comment

    • Erick

      #3
      Re: Easy Q: dealing with object type

      Ah, you're running into the "old-style classes vs. new style classes".
      Try subclassing from "object".

      For example:
      [color=blue][color=green][color=darkred]
      >>> class A(object):[/color][/color][/color]
      .... pass
      ....[color=blue][color=green][color=darkred]
      >>> a=A()
      >>> type(a)[/color][/color][/color]
      <class '__main__.A'>[color=blue][color=green][color=darkred]
      >>> type(a) == A[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> type(a) is A[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> b=A()
      >>> type(a) == type(b)[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> type(a) is type(b)[/color][/color][/color]
      True


      Check out the following article, it should answer your questions:

      -e

      Comment

      • Erick

        #4
        Re: Easy Q: dealing with object type

        Ah, you're running into the "old-style classes vs. new style classes".
        Try subclassing from "object".

        For example:
        [color=blue][color=green][color=darkred]
        >>> class A(object):[/color][/color][/color]
        .... pass
        ....[color=blue][color=green][color=darkred]
        >>> a=A()
        >>> type(a)[/color][/color][/color]
        <class '__main__.A'>[color=blue][color=green][color=darkred]
        >>> type(a) == A[/color][/color][/color]
        True[color=blue][color=green][color=darkred]
        >>> type(a) is A[/color][/color][/color]
        True[color=blue][color=green][color=darkred]
        >>> b=A()
        >>> type(a) == type(b)[/color][/color][/color]
        True[color=blue][color=green][color=darkred]
        >>> type(a) is type(b)[/color][/color][/color]
        True


        Check out the following article, it should answer your questions:

        -e

        Comment

        • Erik Johnson

          #5
          Re: Easy Q: dealing with object type


          "Erick" <idadesub@gmail .com> wrote in message
          news:1107396669 .028096.301290@ g14g2000cwa.goo glegroups.com.. .[color=blue]
          > Ah, you're running into the "old-style classes vs. new style classes".
          > Try subclassing from "object".
          >
          > For example:
          >[color=green][color=darkred]
          > >>> class A(object):[/color][/color][/color]

          That works! :) I guess I am fortunate to be running 2.2 - looks kinda ugly
          prior to that.
          [color=blue]
          > Check out the following article, it should answer your questions:
          >[/color]



          Thank you both for your replies! :)
          -ej


          Comment

          • Steven Bethard

            #6
            Re: Easy Q: dealing with object type

            Erik Johnson wrote:[color=blue]
            > "Erick" <idadesub@gmail .com> wrote in message
            > news:1107396669 .028096.301290@ g14g2000cwa.goo glegroups.com.. .
            >[color=green]
            >>Ah, you're running into the "old-style classes vs. new style classes".
            >>Try subclassing from "object".
            >>
            >>For example:
            >>[color=darkred]
            >>>>>class A(object):[/color][/color]
            >
            > That works! :) I guess I am fortunate to be running 2.2 - looks kinda ugly
            > prior to that.[/color]

            It's not horrible:

            py> class A:
            .... pass
            ....
            py> class B:
            .... pass
            ....
            py> a = A()
            py> a.__class__ == A
            True
            py> a.__class__ == B
            False

            Still, if you can use new-style classes, you should.

            Also, you should probably Google for "duck typing". Generally, in
            Python it is frowned upon to check the type of an object. There are
            times when it's necessary, but if you're just starting off, my guess is
            that you haven't discovered one of these times yet...

            Steve

            Comment

            Working...