2

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

3 Answers3

1

There are two things to note here.

  1. You were not calling the str method of the instances. Rather you were calling print on the container, which is alist_final, which is a list.

    print(somelist) will call the str method somelist, not the elements in the list

  2. You were not returning the string representation. Instead of that, you were calling it with print.

You need to return the representation from the __str__ method you implemented like,

$ cat emp.py
class Employee:

    empCount = 0
    def __init__(self,name,salary):
        self.name = name 
        self.salary = salary
        Employee.empCount += 1

    def __str__(self):
        return '{}(name={}, salary={})'.format(
                        self.__class__.__name__,
                        self.name,
                        self.salary)

    def displayCount(self):
        print("Total Employee %d" % Employee.empCount)

    def displayEmployee(self):
        print ("Name : ", self.name, ", Salary: ",self.salary)


alist_final =[]
for _ in range(2):
    y = Employee('n',2)
    alist_final.append(y)
    y.displayEmployee()
    print("Total Employee %d" % Employee.empCount)

print([str(x) for x in alist_final])

Output:

$ python emp.py
('Name : ', 'n', ', Salary: ', 2)
Total Employee 1
('Name : ', 'n', ', Salary: ', 2)
Total Employee 2
['Employee(name=n, salary=2)', 'Employee(name=n, salary=2)']
han solo
  • 6,390
  • 1
  • 15
  • 19
1

Instead of using __str__ method, you can use __repr__ method. Using this method you don't need to explicitly convert the instance into string. Find the code:

class Employee:
    empCount = 0
    def __init__(self,name,salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1

    def __repr__(self):
        return 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)

Output would be like:

Name :  n , Salary:  2
Total Employee 1
Name :  n , Salary:  2
Total Employee 2
[n, n]

Hope this helps!

Bharat Gera
  • 800
  • 1
  • 4
  • 13
0

Add the __repr__ method which will run when a instance of your class is passed into the print function.

def __repr__(self) -> str:
    return f"employee<{name: {self.name}, salary: {self.salary}>"