I am trying to print instances of a class, however I get a result that seems to be in the correct format but is not the output I am looking for.
I have used def str(self) as someone recommend but I am still getting the same result
This is the entire code:
class Employee:
empCount = 0
def __init__(self,name,salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def __str__(self):
print (self.name)
def displayCount(self):
print("Total Employee %d" % Employee.empCount)
def displayEmployee(self):
print ("Name : ", self.name, ", Salary: ",self.salary)
global emp1
global emp2
global n
global s
emp1 = ''
emp2 = ''
alist = [emp1,emp2]
alist_final =[]
for y in alist:
y = Employee('n',2)
alist_final.append(y)
y.displayEmployee()
print("Total Employee %d" % Employee.empCount)
print(alist_final)
This is the result i get on the command line:
C:\Users\Demi\Desktop\virtuallearner>pr.py
z
2
Name : z , Salary: 2
Total Employee 1
m
5
Name : m , Salary: 5
Total Employee 2
[<__main__.Employee object at 0x0000022AFA278550>, <__main__.Employee object
at 0x0000022AFA278550>]
I am trying to print out the instances with the changes made in the for loop