Don’t do it and don’t do it.
You want to print the variable name, and that you know to a certain extent what it is. What would be the advantage of printing the name of something that’s already in the code? What good would it do for the user to know the name of the variable that object is there? It makes no sense to your code and makes no sense to the user. Maybe you’re missing What is a variable? and In programming, what is an object?. May also be useful: Instances and variables in C#
When the object is used somewhere other than a variable named as it was in the list, there is obviously no way to print something that does not exist.
I’ll write a code that has exactly the same result (in fact the variables are completely redundant and unnecessary), tells me where is the name:
group = [Panzer(), Panzer(), Panzer()]
Instances (objects) have no names. You can print internal features of them, it depends on each object. If you need to have a name in it you need to define the object to have a name. Maybe you want something like this:
class Panzer():
def __init__(self, name):
self.name = name
self.life = 100
self.blin = 100
group = [Panzer("drogo"), Panzer("cabal"), Panzer("sirius")]
for i in group:
print(i.name)
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero