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()
Instances don't have names. By the time the global name ThisObject gets bound to the instance created by evaluating the SomeObject constructor, the constructor has finished running. If you want an object to have a name, just pass the name along in the constructor.
Print an Object in Python Using the __repr__() Method It, by default, returns the name of the object's class and the address of the object. When we print an object in Python using the print() function, the object's __str__() method is called.
Objects do not necessarily have names in Python, so you can't get the name. It's not unusual for objects to have a __name__ attribute in those cases that they do have a name, but this is not a part of standard Python, and most built in types do not have one.
Everything in Python is an object such as integers, lists, dictionaries, functions and so on. Every object has a type and the object types are created using classes. Instance is an object that belongs to a class. For instance, list is a class in Python. When we create a list, we have an instance of the list class.
Suppose we want to print a class instance using the print () function, like viewing the object’s data or values. We can do so by using the methods explained below. The __repr__ () method returns the object’s printable representation in the form of a string.
A new print () method can be described within the class, which will print the class attributes or values of our choice. The below example code demonstrates how to define and then use the object.print () method to print an object in Python. DelftStack is a collective effort contributed by software geeks like you.
When we print an object in Python using the print () function, the object’s __str__ () method is called. If it is not defined then the __str__ () returns the return value of the __repr__ () method. That is why when we try to print an object of a user-defined class using the print () function, it returns the return value of the __repr__ () method.
Creating Instance Objects in Python Python Server Side Programming Programming To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With