简体   繁体   中英

print object/instance name in python

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()

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.

You'd have to first give them names. Eg

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM