9

I was wondering if there is a way to print the object name in python as a string. For example I want to be able to say ENEMY1 has 2 hp left or ENEMY2 has 4 hp left. Is there a way of doing that?\

class badguy:
    def __init__(self):
        self.hp = 4

    def attack(self):
        print("hit")
        self.hp -= 1

    def still_alive(self):
        if self.hp <=0:
            print("enemy destroyed")
        else :
            print (str(self.hp) + " hp left")

    # creating objects

    enemy1 = badguy()
    enemy2 = badguy()

    enemy1.attack()
    enemy1.attack()
    enemy1.still_alive()
    enemy2.still_alive()
user94559
  • 59,196
  • 6
  • 103
  • 103
netrate
  • 423
  • 2
  • 8
  • 14
  • 2
    You can, but you may find that it makes more sense to just have a `name` field on the `badguy` class. Reflecting against variable names is almost always a bad idea. Plus, what if you had a huge list of enemies? – Athena Jul 26 '16 at 20:10
  • 2
    Objects don't have unique names. If you do `newname = enemy1`, then what is the "name" of that object? – BrenBarn Jul 26 '16 at 20:11
  • Most objects don't have names. Variables have names, but there is no one-to-one correspondence between variables and objects. – user2357112 Jul 26 '16 at 20:12
  • It's actually impossible to do this, since multiple "names" can refer to the same object: `a = object(); b = a; print(a is b) # True` – Zac Crites Jul 26 '16 at 20:26

3 Answers3

6

A much better design principle is not to rely on the specific name of the object as shown below:

class badguy(object):
    def __init__(self):
        pass

b = badguy()
print b
>>> <__main__.badguy object at 0x7f2089a74e50>  # Not a great name huh? :D

This can lead to a whole wealth of issues with assignment binding, referencing, and most importantly does not allow you to name your objects per user or program choice.

Instead add an instance variable to your class called self._name (9.6 Classes - Private Variables) or self.name if you want to allow access outside the scope of the class (in this example, you can name it anything). Not only is this more Object-Oriented design, but now you can implement methods like __hash__ to be able to create a hash based on a name for example to use an object as a key (there are many more reasons why this design choice is better!).

class badguy(object):
    def __init__(self, name=None):
        self.hp = 4
        self._name = name   

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        self._name = name

    def attack(self):
        print("hit")
        self.hp -= 1

    def still_alive(self):
        if self.hp <=0:
            print("enemy destroyed")
        else :
            print ("{} has {} hp left.".format(self.name, self.hp))

Sample output:

b = badguy('Enemy 1')
print b.name
>>> Enemy 1

b.still_alive()
>>> Enemy 1 has 4 hp left.

b.name = 'Enemy One'  # Changing our object's name.
b.still_alive()
>>> Enemy One has 4 hp left.
ospahiu
  • 3,465
  • 2
  • 13
  • 24
3

You'd have to first give them names. E.g.

class badguy:
    def __init__(self, name):
        self.hp = 4
        self.name = name

    def attack(self):
        print("hit")
        self.hp -= 1

    def still_alive(self):
        if self.hp <=0:
            print("enemy destroyed")
        else :
            print (self.name + " has " + str(self.hp) + " hp left")

    # creating objects

    enemy1 = badguy('ENEMY1')
    enemy2 = badguy('ENEMY2')

    enemy1.attack()
    enemy1.attack()
    enemy1.still_alive()
    enemy2.still_alive()
user94559
  • 59,196
  • 6
  • 103
  • 103
  • 1
    I see the issue not putting in the self, name. That does make it easier to use, although I was hoping that the actual variable name could have been used so there isn't the redundancy of making the object name and then naming it again in quotes. Thank you. – netrate Jul 26 '16 at 22:42
  • 1
    @netrate As commenters have pointed out, there's no such thing as an "object name." The *variable* has a name, but there can be lots of variables pointing to the same object, so how would you decide which one to use? – user94559 Jul 26 '16 at 22:48
1

I have posted a complete solution here:

https://stackoverflow.com/a/49331683/7386061

It works without parameters. For example you could just do:

class badguy(RememberInstanceCreationInfo):
    def __init__(self):
        super().__init__()
        self.hp = 4

    def attack(self):
        print("hit")
        self.hp -= 1

    def still_alive(self):
        if self.hp <=0:
            print("enemy destroyed")
        else :
            print (self.creation_name + " has " + str(self.hp) + " hp left")

enemy1 = badguy()
enemy2 = badguy()

enemy1.attack()
enemy1.attack()
enemy1.still_alive()
enemy2.still_alive()

out: hit
out: hit
out: enemy1 has 2 hp left
out: enemy2 has 4 hp left
TheoRet
  • 31
  • 4