Finding the instance reference of an object - Python

This is a discussion on Finding the instance reference of an object - Python ; Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'...

+ Reply to Thread
Page 1 of 20 1 2 3 11 ... LastLast
Results 1 to 10 of 198

Finding the instance reference of an object

  1. Default 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.objectname()

    how do i find that the name is 'bob'

  2. Default 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.objectname()
    >
    > 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
    http://informixdb.sourceforge.net

  3. Default 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.objectname()
    >
    > how do i find that the name is 'bob'



    This is a FAQ:

    http://www.python.org/doc/faq/progra...e-of-an-object

  4. Default Re: Finding the instance reference of an object

    On 16 Oct, 16:52, Carsten Haese <carsten.ha...@gmail.com> wrote:
    > 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.objectname()

    >
    > > 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.sourceforge.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.

  5. Default 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

  6. Default Re: Finding the instance reference of an object

    Astley Le Jasper schrieb:
    > On 16 Oct, 16:52, Carsten Haese <carsten.ha...@gmail.com> wrote:
    >> 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.objectname()
    >>> 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.sourceforge.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

  7. Default 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.objectname()
    >
    > 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.objectname()). 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=modulename.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.objectname()
    instances['joe'] = modulename.objectname()
    ..
    ..
    ..

    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

  8. Default 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.objectname()). 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=modulename.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


  9. Default Re: Finding the instance reference of an object

    On 16 Oct, 18:53, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
    > Astley Le Jasper schrieb:
    >
    >
    >
    > > On 16 Oct, 16:52, Carsten Haese <carsten.ha...@gmail.com> wrote:
    > >> 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.objectname()
    > >>> 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.sourceforge.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.

  10. Default Re: Finding the instance reference of an object

    Thanks for all the responses. That helps.

    Ta

    ALJ

+ Reply to Thread
Page 1 of 20 1 2 3 11 ... LastLast