How to give a custom object instance a type name ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Graeme Matthew

    How to give a custom object instance a type name ?

    Hi all, quick one,

    I hope I am explaining this properly, I am wanting to do some introspection
    on custom object instances, for example:

    import md5[color=blue][color=green][color=darkred]
    >>> m = md5.new()
    >>> type(m)[/color][/color][/color]
    <type 'md5.md5'>

    I can at least test here by doing something like if type(m) == 'md5.md5':


    Here is a custom Dispatcher class that I have written
    [color=blue][color=green][color=darkred]
    >>> from BI.System.Contr oller.Dispatche r import Dispatcher
    >>> x = Dispatcher()
    >>> type(x)[/color][/color][/color]
    <type 'instance'>

    How do I get the same as with instance 'm' above where the type displays the
    actual object instance name, however my
    custom dispatcher instance is just a generic 'instance', is there some
    builtin where this is set ?

    many thanks

    Graeme






  • Ulrich Petri

    #2
    Re: How to give a custom object instance a type name ?

    "Graeme Matthew" <gsmatthew@ozem ail.com.au> schrieb im Newsbeitrag
    news:DAwQa.168$ O05.9536@nnrp1. ozemail.com.au. ..[color=blue]
    >
    > Here is a custom Dispatcher class that I have written
    >[color=green][color=darkred]
    > >>> from BI.System.Contr oller.Dispatche r import Dispatcher
    > >>> x = Dispatcher()
    > >>> type(x)[/color][/color]
    > <type 'instance'>[/color]

    You did Java before ;)?
    [color=blue]
    > How do I get the same as with instance 'm' above where the type displays[/color]
    the[color=blue]
    > actual object instance name, however my
    > custom dispatcher instance is just a generic 'instance', is there some
    > builtin where this is set ?
    >[/color]

    how about[color=blue][color=green][color=darkred]
    >>> x.__class__.__n ame__[/color][/color][/color]

    HTH
    Ciao Ulrich


    Comment

    • Graeme Matthew

      #3
      Re: How to give a custom object instance a type name ?

      works fine, ta

      x.__class__.__n ame__
      'Dispatcher'[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]
      "Ulrich Petri" <ulope@gmx.de > wrote in message
      news:beu76g$8t8 vu$1@ID-67890.news.uni-berlin.de...[color=blue]
      > "Graeme Matthew" <gsmatthew@ozem ail.com.au> schrieb im Newsbeitrag
      > news:DAwQa.168$ O05.9536@nnrp1. ozemail.com.au. ..[color=green]
      > >
      > > Here is a custom Dispatcher class that I have written
      > >[color=darkred]
      > > >>> from BI.System.Contr oller.Dispatche r import Dispatcher
      > > >>> x = Dispatcher()
      > > >>> type(x)[/color]
      > > <type 'instance'>[/color]
      >
      > You did Java before ;)?
      >[color=green]
      > > How do I get the same as with instance 'm' above where the type displays[/color]
      > the[color=green]
      > > actual object instance name, however my
      > > custom dispatcher instance is just a generic 'instance', is there some
      > > builtin where this is set ?
      > >[/color]
      >
      > how about[color=green][color=darkred]
      > >>> x.__class__.__n ame__[/color][/color]
      >
      > HTH
      > Ciao Ulrich
      >
      >[/color]


      Comment

      • Kim Petersen

        #4
        Re: How to give a custom object instance a type name ?

        Graeme Matthew wrote:[color=blue]
        > Hi all, quick one,
        >
        > I hope I am explaining this properly, I am wanting to do some introspection
        > on custom object instances, for example:
        >
        > import md5
        >[color=green][color=darkred]
        >>>>m = md5.new()
        >>>>type(m)[/color][/color]
        >
        > <type 'md5.md5'>
        >
        > I can at least test here by doing something like if type(m) == 'md5.md5':[/color]

        how about doing this instead:

        isinstance(m,md 5.md5)
        [color=blue]
        >
        >
        > Here is a custom Dispatcher class that I have written
        >
        >[color=green][color=darkred]
        >>>>from BI.System.Contr oller.Dispatche r import Dispatcher
        >>>>x = Dispatcher()
        >>>>type(x)[/color][/color]
        >
        > <type 'instance'>
        >
        > How do I get the same as with instance 'm' above where the type displays the
        > actual object instance name, however my
        > custom dispatcher instance is just a generic 'instance', is there some
        > builtin where this is set ?[/color]

        isinstance(x,Di spatcher)

        otherwise if you _really_ need the name - just do as some others indicated.


        --
        Med Venlig Hilsen / Regards

        Kim Petersen - Kyborg A/S (Udvikling)
        IT - Innovationshuse t
        Havneparken 2
        7100 Vejle
        Tlf. +4576408183 || Fax. +4576408188

        Comment

        • Michael Hudson

          #5
          Re: How to give a custom object instance a type name ?

          "Graeme Matthew" <gsmatthew@ozem ail.com.au> writes:
          [color=blue]
          > works fine, ta
          >
          > x.__class__.__n ame__
          > 'Dispatcher'[color=green][color=darkred]
          > >>>[/color][/color][/color]

          You could also use new-style classes, btw.

          Cheers,
          M.

          --
          Windows installation day one. Getting rid of the old windows
          was easy - they fell apart quite happily, and certainly wont
          be re-installable anywhere else. -- http://www.linux.org.uk/diary/
          (not *that* sort of windows...)

          Comment

          Working...