4

I'm collecting instances using the following code:

class Hand():
    instances = []
    def __init__(self):
        Hand.instances.append(self)
        self.value = 5

    def do_something(self, a):
        self.value = self.value * a

class Foo():
    def __init__(self):
        pass

    def insty(self):
        self.hand1 = Hand()
        self.hand2 = Hand()


foo = Foo()
foo.insty()
print Hand.instances

for hand in Hand.instances:
    print "how do I print the instance name?"

The last line is just a way to learn how to to access the instance name so i can call the 'do_something' method on each instance in order.

How do I access the instance name for each instance of Hand?

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • 1
    What do you mean by "instance name"? What do you think the "name" of each instance is? – BrenBarn Aug 26 '12 at 03:13
  • "hand1" and "hand2" is what i'm referring to... perhaps erroneously. I'm a newbie... be gentle! – DBWeinstein Aug 26 '12 at 03:14
  • 2
    What's wrong with `for hand in Hand.instances: hand.do_something()` ? – qiao Aug 26 '12 at 03:15
  • @dwstein instance name is just a variable pointing to an object, for eg if you've , `foo=Foo()`, and then `foo1=foo`, then both `foo` and `foo1` point to `Foo()`, how will you name the instance then? – Ashwini Chaudhary Aug 26 '12 at 03:19
  • 1
    So I met this person the other day... how come I wasn't able to determine his name, just by looking at him? And what do you *mean* people sometimes have more than one name? – Karl Knechtel Aug 26 '12 at 03:22
  • @qiao Thanks! You've solved my problem and I see there's no real need to access the variable names 'hand1' or 'hand2'. – DBWeinstein Aug 26 '12 at 03:29
  • ...why on earth has this question been upvoted 3 times? – Erik Kaplun Aug 26 '12 at 13:48
  • @ErikAllik If you're asking me, I think its important for other newbie knuckle-heads like me to see answers to dumb questions. Many of the tutorials on line and much of the documentation are heavy on theory and light on examples. The answers here leave me understanding this concept in no uncertain terms. – DBWeinstein Aug 26 '12 at 18:14

4 Answers4

7

If you mean how to get hand1 from the instance you assigned to self.hand1, the answer is that you can't. When you do self.hand1 = Hand(), you tell the Foo object it has a Hand, but the Hand object has no knowledge that it has been assigned to a Foo. You could do this:

h = Hand()
self.bob = h
self.larry = h

Now what is the "name" of that Hand supposed to be? You assigned the same hand to both "bob" and "larry", so there's no way it can have a single unique name.

If you want to have a name for each hand, you need to tell the hand what name you want to give it. You would have to modify your Hand code to allow you to pass a name to the constructor, then create the Hand with Hand("some name").

You can of course give the hands "names" by assigning attributes on them:

self.hand1 = Hand()
self.hand1.name = "hand 1"

. . . but these names are not special or "automatic" in any way.

The bottom line is that if you want something to have a name, you need to decide how to handle that name. You need write your own code that gives it its name, and your own code that retrieves its name.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
1

foo = Foo() means that the variable foo just points to the object returned by Foo(), there's no concept of name here.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

I don't know if this would solve your problem or not. I needed to get instance names in order to do clear error reporting. Everywhere I looked, folks said "variables don't have names! The name is just a pointer to the thing!"

But it turns out that getting instance names in python is pretty straightforward. Here's how I did it:

import gc
def instance_names(self):
    referrers = gc.get_referrers(self)
    result = []
    dict_of_things = {}
    for item in referrers:
        if isinstance(item, dict):
            dict_of_things = item
    for k, v in dict_of_things.items():
        if v == self:
            result.append(k)
    if not result:
        result = ['unnamed instance']
    return result
0

foo.__dict__ will have "hand1" and "hand2" keys (among others). But you're probably going about this the wrong way. If the names are significant, you should use them as explicit indices in Foo (or somewhere).

e.g.

class Foo():
    def __init__(self):
        self.hands = {}

    def insty(self):
        self.hands['hand1'] = Hand()
        self.hands['hand2'] = Hand()
Brian Cain
  • 14,403
  • 3
  • 50
  • 88