Creating objects in bulk and prining the names of the objects
Question:
I wanted to make a 100 objects from the same class and print the names of the objects each time they were created. Something like this:
class Human():
def __init__(self):
#print the name of the object
human1 = Human()
human2 = Human()
human3 = Human()
human4 = Human()
human5 = Human()
.......
I was able to come this far:
class Human():
def __init__(self):
#print the name of the object
for i in range(1,101):
#'human' + str(i) = Human()
But I got stuck since in python you cannot define objects as the way I tried to and I couldn’t find a way to print the name of the object. Anything that could help?
Answers:
class Human():
def __init__(self, name="default name"):
self.name = name
def __str__(self):
return self.name
# print the name of the object
human1 = Human(name="sanjay")
print(human1)
if you pass name
parametr then it will print it other wise default name
would get printed.
You can form variable names out of strings using the exec function:
for i in range(0,101):
exec("human"+str(i)+"=Human()")
print("human"+str(i))
But if you want to access those class instances later without manually referencing them, you’ll have to build the same string and put it inside another exec (or eval) function.
A less cumbersome alternative might be to keep all your instances in a data structure like a list or a dict:
human_list = []
human_dict = {}
for i in range(0,101):
human_list.append(Human())
human_dict[i] = Human()
And then reference them like
human_list[i]
human_dict[i]
I wanted to make a 100 objects from the same class and print the names of the objects each time they were created. Something like this:
class Human():
def __init__(self):
#print the name of the object
human1 = Human()
human2 = Human()
human3 = Human()
human4 = Human()
human5 = Human()
.......
I was able to come this far:
class Human():
def __init__(self):
#print the name of the object
for i in range(1,101):
#'human' + str(i) = Human()
But I got stuck since in python you cannot define objects as the way I tried to and I couldn’t find a way to print the name of the object. Anything that could help?
class Human():
def __init__(self, name="default name"):
self.name = name
def __str__(self):
return self.name
# print the name of the object
human1 = Human(name="sanjay")
print(human1)
if you pass name
parametr then it will print it other wise default name
would get printed.
You can form variable names out of strings using the exec function:
for i in range(0,101):
exec("human"+str(i)+"=Human()")
print("human"+str(i))
But if you want to access those class instances later without manually referencing them, you’ll have to build the same string and put it inside another exec (or eval) function.
A less cumbersome alternative might be to keep all your instances in a data structure like a list or a dict:
human_list = []
human_dict = {}
for i in range(0,101):
human_list.append(Human())
human_dict[i] = Human()
And then reference them like
human_list[i]
human_dict[i]