Finding the instance reference of an object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Astley Le Jasper

    Finding the instance reference of an object

    Sorry for the numpty question ...

    How do you find the reference name of an object?

    So if i have this

    bob = modulename.obje ctname()

    how do i find that the name is 'bob'
  • Carsten Haese

    #2
    Re: Finding the instance reference of an object

    Astley Le Jasper wrote:
    Sorry for the numpty question ...
    >
    How do you find the reference name of an object?
    >
    So if i have this
    >
    bob = modulename.obje ctname()
    >
    how do i find that the name is 'bob'
    Why do you need to find that? You know that its name is 'bob'.

    --
    Carsten Haese

    Comment

    • Michele Simionato

      #3
      Re: Finding the instance reference of an object

      On Oct 16, 4:01 pm, Astley Le Jasper <Astley.lejas.. .@gmail.com>
      wrote:
      Sorry for the numpty question ...
      >
      How do you find the reference name of an object?
      >
      So if i have this
      >
      bob = modulename.obje ctname()
      >
      how do i find that the name is 'bob'

      This is a FAQ:

      Contents: Programming FAQ- General Questions- Is there a source code level debugger with breakpoints, single-stepping, etc.?, Are there tools to help find bugs or perform static analysis?, How can ...

      Comment

      • Astley Le Jasper

        #4
        Re: Finding the instance reference of an object

        On 16 Oct, 16:52, Carsten Haese <carsten.ha...@ gmail.comwrote:
        Astley Le Jasper wrote:
        Sorry for the numpty question ...
        >
        How do you find the reference name of an object?
        >
        So if i have this
        >
        bob = modulename.obje ctname()
        >
        how do i find that the name is 'bob'
        >
        Why do you need to find that? You know that its name is 'bob'.
        >
        --
        Carsten Haesehttp://informixdb.sour ceforge.net
        I'm creating mulitple instances, putting them in a list, iterating
        through the list to send them to some functions where process them
        with some instance specific parameters. Something along the lines of:

        bob = someobject()
        harry = someobject()
        fred = someobject()

        parameterdict = {'bob':(0,1,2), 'harry':(3,4,5) ,'fred':(6,7,8) }
        people_list = (bob, harry, fred)

        for person in people_list:
        add_parameters( person)

        def add_parameters( person)
        mytuple = parameterdict[??????instance. name????]
        person.x = mytuple[0]
        person.y = mytuple[1]
        person.z = mytuple[2]

        .... alternatively there is probably a very much easier way of doing
        it.

        Comment

        • Steven D'Aprano

          #5
          Re: Finding the instance reference of an object

          On Thu, 16 Oct 2008 08:04:23 -0700, Astley Le Jasper wrote:
          I'm creating mulitple instances, putting them in a list, iterating
          through the list to send them to some functions where process them with
          some instance specific parameters. Something along the lines of:
          >
          bob = someobject()
          harry = someobject()
          fred = someobject()
          >
          parameterdict = {'bob': (0,1,2), 'harry': (3,4,5), 'fred': (6,7,8)}
          people_list = (bob, harry, fred)
          >
          for person in people_list:
          add_parameters( person)
          >
          def add_parameters( person)
          mytuple = parameterdict[??????instance. name????]
          person.x = mytuple[0]
          person.y = mytuple[1]
          person.z = mytuple[2]
          >
          ... alternatively there is probably a very much easier way of doing it.

          Assuming that someobject() objects are hashable, you can do this:

          # use the objects themselves as keys, not their names
          parameterdict = {bob: (0,1,2), harry: (3,4,5), fred: (6,7,8)}
          people_list = [bob, harry, fred]

          but of course that doesn't work if someobject() items aren't hashable
          (say, lists or dicts).

          But in my opinion, this is probably the best way (untested):

          def add_parameters( person, x, y, z):
          # note we don't use any global variables
          person.x = x
          person.y = y
          person.z = z

          parameters = [(0,1,2), (3,4,5), (6,7,8)]
          people = [bob, harry, fred]
          for person, args in zip(people, parameters):
          add_parameters( person, *args)



          --
          Steven

          Comment

          • Diez B. Roggisch

            #6
            Re: Finding the instance reference of an object

            Astley Le Jasper schrieb:
            On 16 Oct, 16:52, Carsten Haese <carsten.ha...@ gmail.comwrote:
            >Astley Le Jasper wrote:
            >>Sorry for the numpty question ...
            >>How do you find the reference name of an object?
            >>So if i have this
            >>bob = modulename.obje ctname()
            >>how do i find that the name is 'bob'
            >Why do you need to find that? You know that its name is 'bob'.
            >>
            >--
            >Carsten Haesehttp://informixdb.sour ceforge.net
            >
            I'm creating mulitple instances, putting them in a list, iterating
            through the list to send them to some functions where process them
            with some instance specific parameters. Something along the lines of:
            >
            bob = someobject()
            harry = someobject()
            fred = someobject()
            >
            parameterdict = {'bob':(0,1,2), 'harry':(3,4,5) ,'fred':(6,7,8) }
            people_list = (bob, harry, fred)
            >
            for person in people_list:
            add_parameters( person)
            >
            def add_parameters( person)
            mytuple = parameterdict[??????instance. name????]
            person.x = mytuple[0]
            person.y = mytuple[1]
            person.z = mytuple[2]
            >
            ... alternatively there is probably a very much easier way of doing
            it.
            Why not simply do

            bob = someobject(0, 1, 2)

            ?

            Diez

            Comment

            • Larry Bates

              #7
              Re: Finding the instance reference of an object

              Astley Le Jasper wrote:
              Sorry for the numpty question ...
              >
              How do you find the reference name of an object?
              >
              So if i have this
              >
              bob = modulename.obje ctname()
              >
              how do i find that the name is 'bob'
              Short answer is that you can't. This because Python's names (bob) are bound to
              objects (modulename.obj ectname()). They are NOT variables as they are in
              "other" programming languages. It is perfectly legal in Python to bind multiple
              names to a single object:

              a=b=c=modulenam e.objectname()

              a, b, and c all point to the same object. An object can have an unlimited
              number of names bound to it. This is one of the most difficult concepts for
              many beginning Python programmers to understand (I know I had a difficult time
              at first). It is just not how we taught ourselves to think about "variables"
              and you can write quite a lot of Python treating the names you bind to objects
              like they were "variables" .

              To accomplish what you want, put your instances in a dictionary.

              instances = {}
              instances['bob'] = modulename.obje ctname()
              instances['joe'] = modulename.obje ctname()
              ..
              ..
              ..

              Then you can reference them as:

              instances[name]

              and/or you can pass the name in as an argument to the __init__ method of
              objectname so that it can "hold" the name of the dictionary key that references
              it (good for debugging/logging).

              -Larry

              Comment

              • Joe Strout

                #8
                Re: Finding the instance reference of an object

                On Oct 16, 2008, at 10:59 AM, Larry Bates wrote:
                >how do i find that the name is 'bob'
                >
                Short answer is that you can't. This because Python's names (bob)
                are bound to objects (modulename.obj ectname()). They are NOT
                variables as they are in "other" programming languages.
                Which other programming languages? I've never seen an OOP language
                that didn't work the same way as Python.

                However, 'bob' here really is a variable. It's a variable whose value
                (at the moment) is a reference to some object.
                It is perfectly legal in Python to bind multiple names to a single
                object:
                >
                a=b=c=modulenam e.objectname()
                Right -- three variables (a, b, and c) that all have the same value,
                i.e. all refer to the same object. There's nothing more mysterious
                here than

                i=j=k=42

                where i, j, and k all have the same value. (The OP's question would
                be like asking "what is the name of the variable referring to 42? And
                while you might, in theory, be able to produce a list of all such
                variables by trolling through the Python's internals, it's a bit of a
                silly thing to do.)
                a, b, and c all point to the same object. An object can have an
                unlimited number of names bound to it. This is one of the most
                difficult concepts for many beginning Python programmers to
                understand (I know I had a difficult time at first). It is just not
                how we taught ourselves to think about "variables" and you can write
                quite a lot of Python treating the names you bind to objects like
                they were "variables" .
                Well, they are variables. I'm not quite grasping the difficulty
                here... unless perhaps you were (at first) thinking of the variables
                as holding the object values, rather than the object references. That
                is indeed something important to grasp, since it explains why if you do

                a = b # where b was some object with an attribute 'foo'...
                a.foo = 42

                ....you now find that b.foo is 42 too. Nothing mysterious once you
                realize that the value of a and b is a reference to some object that
                has a "foo" attribute.

                Not sure if all this was helpful to anyone, but I hope so!

                Best,
                - Joe

                Comment

                • Astley Le Jasper

                  #9
                  Re: Finding the instance reference of an object

                  On 16 Oct, 18:53, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                  Astley Le Jasper schrieb:
                  >
                  >
                  >
                  On 16 Oct, 16:52, Carsten Haese <carsten.ha...@ gmail.comwrote:
                  Astley Le Jasper wrote:
                  >Sorry for the numpty question ...
                  >How do you find the reference name of an object?
                  >So if i have this
                  >bob = modulename.obje ctname()
                  >how do i find that the name is 'bob'
                  Why do you need to find that? You know that its name is 'bob'.
                  >
                  --
                  Carsten Haesehttp://informixdb.sour ceforge.net
                  >
                  I'm creating mulitple instances, putting them in a list, iterating
                  through the list to send them to some functions where process them
                  with some instance specific parameters. Something along the lines of:
                  >
                  bob = someobject()
                  harry = someobject()
                  fred = someobject()
                  >
                  parameterdict = {'bob':(0,1,2), 'harry':(3,4,5) ,'fred':(6,7,8) }
                  people_list = (bob, harry, fred)
                  >
                  for person in people_list:
                    add_parameters( person)
                  >
                  def add_parameters( person)
                    mytuple = parameterdict[??????instance. name????]
                    person.x = mytuple[0]
                    person.y = mytuple[1]
                    person.z = mytuple[2]
                  >
                  ... alternatively there is probably a very much easier way of doing
                  it.
                  >
                  Why not simply do
                  >
                  bob = someobject(0, 1, 2)
                  >
                  ?
                  >
                  Diez
                  Because that was pseudo code to demonstrate what I was trying to
                  achieve. The parameters are more complicated than that.

                  Comment

                  • Astley Le Jasper

                    #10
                    Re: Finding the instance reference of an object

                    Thanks for all the responses. That helps.

                    Ta

                    ALJ

                    Comment

                    • Aaron \Castironpi\ Brady

                      #11
                      Re: Finding the instance reference of an object

                      On Oct 16, 12:25 pm, Astley Le Jasper <Astley.lejas.. .@gmail.com>
                      wrote:
                      Thanks for all the responses. That helps.
                      >
                      Ta
                      >
                      ALJ
                      If you're sure it's unique, why not just scan through the pairs in
                      locals()?

                      for k, v in locals():
                      if v is the_object_im_l ooking_for:
                      name_im_looking _for= k

                      This method can sometimes return more than one name, due to the
                      variable ordering of objects in dictionaries.

                      Comment

                      • Steven D'Aprano

                        #12
                        Re: Finding the instance reference of an object

                        On Thu, 16 Oct 2008 11:24:28 -0600, Joe Strout wrote:
                        On Oct 16, 2008, at 10:59 AM, Larry Bates wrote:
                        >
                        >>how do i find that the name is 'bob'
                        >>
                        >Short answer is that you can't. This because Python's names (bob) are
                        >bound to objects (modulename.obj ectname()). They are NOT variables as
                        >they are in "other" programming languages.
                        >
                        Which other programming languages? I've never seen an OOP language that
                        didn't work the same way as Python.
                        >
                        However, 'bob' here really is a variable. It's a variable whose value
                        (at the moment) is a reference to some object.

                        Traditionally, a "variable" is a named memory location.

                        The main objection I have to using "variable" to describe Python name/
                        value bindings is that it has connotations that will confuse programmers
                        who are familiar with C-like languages. For example:

                        def inc(x):
                        x += 1

                        n = 1
                        inc(n)
                        assert n == 2

                        Why doesn't that work? This is completely mysterious to anyone expecting
                        C-like variables.

                        At this point people will often start confusing the issue by claiming
                        that "all Python variables are pointers", which is an *implementation
                        detail* in CPython but not in other implementations , like PyPy or Jython.

                        Or people will imagine that Python makes a copy of the variable when you
                        call a function. That's not true, and in fact Python explicitly promises
                        never to copy a value unless you explicitly tell it to, but it seems to
                        explain the above, at least until the programmer starts *assuming* call-
                        by-value behaviour and discovers this:

                        def inc(alist):
                        alist += [1] # or alist.append(1) if you prefer
                        return alist

                        a = [1, 2, 3]
                        b = inc(a)
                        assert a == [1, 2, 3]

                        Are functions call by value or call by reference???

                        (Answer: neither. They are call by name.)


                        I myself often talk about variables as shorthand. But it's a bad habit,
                        because it is misleading to anyone who thinks they know how variables
                        behave, so when I catch myself doing it I fix it and talk about name
                        bindings. Noobs might not know what that means, but that's a feature, not
                        a bug, because it gets them paying attention instead of making faulty
                        assumptions.

                        Of course, you're entitled to define "variable" any way you like, and
                        then insist that Python variables don't behave like variables in other
                        languages. Personally, I don't think that's helpful to anyone.

                        [snip]
                        Well, they are variables. I'm not quite grasping the difficulty here...
                        unless perhaps you were (at first) thinking of the variables as holding
                        the object values, rather than the object references.
                        But that surely is what almost everyone will think, almost all the time.
                        Consider:

                        x = 5
                        y = x + 3

                        I'm pretty sure that nearly everyone will read it as "assign 5 to x, then
                        add 3 to x and assign the result to y" instead of:

                        "assign a reference to the object 5 to x, then dereference x to get the
                        object 5, add it to the object 3 giving the object 8, and assign a
                        reference to that result to y".

                        Of course that's what's really happening under the hood, and you can't
                        *properly* understand how Python behaves without understanding that. But
                        I'm pretty sure few people think that way naturally, especially noobs.
                        References are essentially like pointers, and learning pointers is
                        notoriously difficult for people. Python does a magnificent job of making
                        references easy, but it does so by almost always hiding the fact that it
                        uses references under the hood. That's why talk about variables is so
                        seductive and dangerous: Python's behaviour is *usually* identical to the
                        behaviour most newbies expect from a language with "variables" .



                        --
                        Steven

                        Comment

                        • Steven D'Aprano

                          #13
                          Re: Finding the instance reference of an object

                          On Thu, 16 Oct 2008 11:51:43 -0700, Aaron \"Castironpi \" Brady wrote:
                          If you're sure it's unique, why not just scan through the pairs in
                          locals()?
                          >
                          for k, v in locals():
                          if v is the_object_im_l ooking_for:
                          name_im_looking _for= k
                          >
                          This method can sometimes return more than one name, due to the variable
                          ordering of objects in dictionaries.
                          Because:

                          (1) in general, objects can have no name at all, or multiple names, so
                          this won't work in general (although it may work in a specific case);

                          (2) it's awfully inefficient if you are doing it a lot; and

                          (3) even if it works and you can afford to pay the cost, it is almost
                          certainly the Wrong Way to solve the problem at hand.




                          --
                          Steven

                          Comment

                          • Aaron \Castironpi\ Brady

                            #14
                            Re: Finding the instance reference of an object

                            On Oct 16, 8:30 pm, Steven D'Aprano <st...@REMOVE-THIS-
                            cybersource.com .auwrote:
                            On Thu, 16 Oct 2008 11:51:43 -0700, Aaron \"Castironpi \" Brady wrote:
                            If you're sure it's unique, why not just scan through the pairs in
                            locals()?
                            >
                            for k, v in locals():
                              if v is the_object_im_l ooking_for:
                                name_im_looking _for= k
                            >
                            This method can sometimes return more than one name, due to the variable
                            ordering of objects in dictionaries.
                            >
                            Because:
                            >
                            (1) in general, objects can have no name at all, or multiple names, so
                            this won't work in general (although it may work in a specific case);
                            >
                            (2) it's awfully inefficient if you are doing it a lot; and
                            >
                            (3) even if it works and you can afford to pay the cost, it is almost
                            certainly the Wrong Way to solve the problem at hand.
                            >
                            --
                            Steven
                            In fact, what's more called-for, is a mapping in reverse:

                            name_im_looking _for= lookup[ the_object_im_l ooking_for ]

                            You'll just have to modify it in parallel with your local variables,
                            which is a (yet another) bad sign.

                            Comment

                            • Terry Reedy

                              #15
                              Re: Finding the instance reference of an object

                              Astley Le Jasper wrote:
                              I'm creating mulitple instances, putting them in a list, iterating
                              through the list to send them to some functions where process them
                              with some instance specific parameters. Something along the lines of:
                              >
                              bob = someobject()
                              harry = someobject()
                              fred = someobject()
                              >
                              parameterdict = {'bob':(0,1,2), 'harry':(3,4,5) ,'fred':(6,7,8) }
                              people_list = (bob, harry, fred)
                              >
                              for person in people_list:
                              add_parameters( person)
                              >
                              def add_parameters( person)
                              mytuple = parameterdict[??????instance. name????]
                              person.x = mytuple[0]
                              person.y = mytuple[1]
                              person.z = mytuple[2]
                              If you want an object to have a 'personal' name, give it a name
                              attribute, just like python does to modules, classes, and functions.

                              class someob():
                              def __init__(self,n ame):
                              self.name = name

                              people = {someob('bob'), someob('harry') , someob('fred')
                              ....
                              def add_param(perso n):
                              mytuple = parameterdict[person.name]
                              ...

                              Comment

                              Working...