instance name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • max(01)*

    #1

    instance name

    hi.

    is there a way to define a class method which prints the instance name?

    e.g.:
    [color=blue][color=green][color=darkred]
    >>> class class_1:[/color][/color][/color]
    .... def myName(self):
    .... ????what should i do here????
    ....[color=blue][color=green][color=darkred]
    >>> instance_1 = class_1()
    >>> instance_1.myNa me()[/color][/color][/color]
    'instance_1'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    bye

    macs
  • Andrew Koenig

    #2
    Re: instance name

    "max(01)*" <max2@fisso.cas a> wrote in message
    news:99z3e.1157 607$35.42889387 @news4.tin.it.. .
    [color=blue]
    > is there a way to define a class method which prints the instance name?[/color]

    The term "the instance name" is misleading, because it assumes, without
    saying so explicitly, that every instance has a unique name.

    In fact, there is no reason that an instance needs to have a name at all, or
    that it should have only one.

    You gave this example:

    instance_1 = class_1()
    instance_1.myNa me()

    but what if I did this instead?

    class_1().myNam e()

    Or this?

    instance_1 = class_1()
    instance_2 = instance_1
    instance_2.myNa me()


    Comment

    • Irmen de Jong

      #3
      Re: instance name

      max(01)* wrote:[color=blue]
      > hi.
      >
      > is there a way to define a class method which prints the instance name?
      >
      > e.g.:
      >[color=green][color=darkred]
      >>>> class class_1:[/color][/color]
      > ... def myName(self):
      > ... ????what should i do here????
      > ...[color=green][color=darkred]
      >>>> instance_1 = class_1()
      >>>> instance_1.myNa me()[/color][/color]
      > 'instance_1'[color=green][color=darkred]
      >>>>[/color][/color]
      >
      > bye
      >
      > macs[/color]

      What should the following do, you think?
      [color=blue][color=green][color=darkred]
      >>> a=class_1()
      >>> b=a
      >>> b is a[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> b.myName()[/color][/color][/color]
      ????print what????

      There is no such thing as "the" instance name.
      (a and b both point to the same instance, in my example)

      Also: why do you want this? It smells like you're
      actually looking for the use of a dict to do your job.

      --Irmen

      Comment

      • max(01)*

        #4
        Re: instance name

        Andrew Koenig wrote:[color=blue]
        > "max(01)*" <max2@fisso.cas a> wrote in message
        > news:99z3e.1157 607$35.42889387 @news4.tin.it.. .
        >
        >[color=green]
        >>is there a way to define a class method which prints the instance name?[/color]
        >
        >
        > The term "the instance name" is misleading, because it assumes, without
        > saying so explicitly, that every instance has a unique name.
        >
        > In fact, there is no reason that an instance needs to have a name at all, or
        > that it should have only one.
        >
        > You gave this example:
        >
        > instance_1 = class_1()
        > instance_1.myNa me()
        >
        > but what if I did this instead?
        >
        > class_1().myNam e()
        >
        > Or this?
        >
        > instance_1 = class_1()
        > instance_2 = instance_1
        > instance_2.myNa me()
        >
        >[/color]

        excellent points.

        i'll get back with proper questions afterwards.

        thanks

        macs

        Comment

        • max(01)*

          #5
          Re: instance name

          Irmen de Jong wrote:[color=blue]
          > max(01)* wrote:
          >[color=green]
          >>hi.
          >>
          >>is there a way to define a class method which prints the instance name?
          >>
          >>e.g.:
          >>
          >>[color=darkred]
          >>>>>class class_1:[/color]
          >>
          >>... def myName(self):
          >>... ????what should i do here????
          >>...
          >>[color=darkred]
          >>>>>instance _1 = class_1()
          >>>>>instance_1 .myName()[/color]
          >>
          >>'instance_1 '
          >>
          >>bye
          >>
          >>macs[/color]
          >
          >
          > What should the following do, you think?
          >
          >[color=green][color=darkred]
          >>>>a=class_1 ()
          >>>>b=a
          >>>>b is a[/color][/color]
          >
          > True
          >[color=green][color=darkred]
          >>>>b.myName( )[/color][/color]
          >
          > ????print what????
          >
          > There is no such thing as "the" instance name.
          > (a and b both point to the same instance, in my example)
          >
          > Also: why do you want this? It smells like you're
          > actually looking for the use of a dict to do your job.[/color]

          right. it seems i need to dive deeper in the language "spirit".

          thanks a lot

          bye

          macs

          Comment

          • Mark Winrock

            #6
            Re: instance name

            max(01)* wrote:[color=blue]
            > hi.
            >
            > is there a way to define a class method which prints the instance name?
            >
            > e.g.:
            >[color=green][color=darkred]
            > >>> class class_1:[/color][/color]
            > ... def myName(self):
            > ... ????what should i do here????
            > ...[color=green][color=darkred]
            > >>> instance_1 = class_1()
            > >>> instance_1.myNa me()[/color][/color]
            > 'instance_1'[color=green][color=darkred]
            > >>>[/color][/color]
            >
            > bye
            >
            > macs[/color]

            macs,

            The object instance doesn't know about the identifier to which you
            assigned it (but see my example below using the inspect module).

            If you are just trying to identify different instances, you can get a
            unique instance identifier from hash() function
            hash(instance1)

            or
            self.__hash__() , or hash(self) from within the object

            The default __str__ for a class will normally return something similar
            with more information , so that when you
            print instance_1
            you get a string in the following format:
            <module.classna me object at hashvalue>


            The following is a very, very, very weak example of how you might use
            inspect to get the information you want. This is not a robust example at
            all, and I imagine it could have many ways it will fail. The init uses
            inspect information to dip into the source file and parse out the info
            -- like I said, this is not a robust parse.
            # ------------------------
            import os,sys

            class class_1(object) :
            def __init__(self):
            import inspect
            self.lineno = inspect.current frame().f_back. f_lineno
            self.filename = inspect.current frame().f_back. f_code.co_filen ame
            self.initial_in stance = None

            try:
            # i'm not sure if filename is full path or not at this time
            f=open(self.fil ename,'r')
            lines=f.readlin es()
            s = lines[self.lineno-1]
            split = s.split()
            try:
            # find the assignment if possible
            i = split.index('=' )
            self.initial_in stance = split[i-1]
            except ValueError:
            pass
            finally:
            f.close()

            def myName(self):
            print 'my initial instance was \'%s\''%(self.i nitial_instance )

            if __name__ == '__main__':

            # two straight up examples
            instance_1 = class_1()
            instance_2 = class_1()

            instance_1.myNa me()
            instance_2.myNa me()

            #
            class_1().myNam e()

            c = instance_1

            c.myName()
            # -----------------------------------------------

            I got the following results:
            my initial instance was 'instance_1'
            my initial instance was 'instance_2'
            my initial instance was 'None'
            my initial instance was 'instance_1'



            Best-regards,
            Mark

            Comment

            • Bengt Richter

              #7
              Re: instance name

              On Sat, 02 Apr 2005 15:48:21 GMT, "max(01)*" <max2@fisso.cas a> wrote:
              [color=blue]
              >hi.
              >
              >is there a way to define a class method which prints the instance name?
              >
              >e.g.:
              >[color=green][color=darkred]
              > >>> class class_1:[/color][/color]
              >... def myName(self):
              >... ????what should i do here????
              >...[color=green][color=darkred]
              > >>> instance_1 = class_1()
              > >>> instance_1.myNa me()[/color][/color]
              >'instance_1'[color=green][color=darkred]
              > >>>[/color][/color]
              >[/color]
              Names in any given name space do not have a 1:1 relationship with class instances.
              If you want to give an instance its own name, give it a name when you create it,
              or attach a name to the instance. E.g, maybe this will give you an idea:
              [color=blue][color=green][color=darkred]
              >>> class Ego(object):[/color][/color][/color]
              ... def __init__(self, name='(anonymou s)'): self.name = name
              ... def myName(self): return self.name
              ...[color=blue][color=green][color=darkred]
              >>> instance_1 = Ego('one')
              >>> instance_2 = Ego('two')
              >>> instance_1.myNa me()[/color][/color][/color]
              'one'[color=blue][color=green][color=darkred]
              >>> instance_2.myNa me()[/color][/color][/color]
              'two'[color=blue][color=green][color=darkred]
              >>> instance_2 = instance_1
              >>> instance_2.myNa me()[/color][/color][/color]
              'one'
              Did you catch that ;-)

              Ok, now without instance name bindings at all, just references from a list:[color=blue][color=green][color=darkred]
              >>> egoes = [Ego(name) for name in 'one two three'.split()]
              >>> egoes[/color][/color][/color]
              [<__main__.Ego object at 0x02EF1A4C>, <__main__.Ego object at 0x02EF1A6C>, <__main__.Ego object
              at 0x02EF1A8C>][color=blue][color=green][color=darkred]
              >>> for ego in egoes: print ego.myName()[/color][/color][/color]
              ...
              one
              two
              three

              We don't have to access instance by names like instance_1, if we didn't make
              them with bindings like that. E.g., egoes is a name bound to a list of Ego instances,
              so we can get at the second (counting from index 0) instance thus, and change it's
              name from the outside, since we know it's stored as an attribute called 'name':
              [color=blue][color=green][color=darkred]
              >>> egoes[1].name = 'Zeppo'
              >>> for ego in egoes: print ego.myName()[/color][/color][/color]
              ...
              one
              Zeppo
              three

              HTH

              Regards,
              Bengt Richter

              Comment

              • max(01)*

                #8
                Re: instance name

                many many thanks to each and everyone who bothered to answer my op.

                best regards

                macs

                Comment

                Working...