-1
def main() :
    a = Employee("Boss",11000)
    b = Employee("Jacob",3000)
    c = Employee("Michael",5000)

    print(a)
    print(b)
    print(c)

class Employee:
    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary  

    def name(self):
        self.__name = name

    def salary(self):
        self.__salary = salary

    def count(self, count=0):
        self.__count=count
        self.__count += 1

    def __str__(self):
        return "No : %d name : %s salary : %d" %(self.__count, self.__name, self.__salary)

main()

I want it to be printed like this and I don't want to change my main function.

No : 1 name : Boss salary : 11000
No : 2 name : Jacob salary : 3000
No : 3 name : Michael salary : 5000

Is there a problem while counting from self.__count or is there something to add?

The error what I got is 'AttributeError: 'Employee' object has no attribute '_Employee__count''

And when I try to change my code like this:

def main() :
    a = Employee("Boss",11000)
    b = Employee("Jacob",3000)
    c = Employee("Michael",5000)

    print(a)
    print(b)
    print(c)

class Employee:
    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary
        **self.__count =0**

    def name(self):
        self.__name = name

    def salary(self):
        self.__salary = salary

    **def count(self, count=0):
        self.__count += 1**

    def __str__(self):
        return "No : %d name : %s salary : %d" %(self.__count, self.__name, self.__salary)

main()

The result was like this:

''' No : 0 name : Boss salary : 11000 No : 0 name : Jacob salary : 3000 No : 0 name : Michael salary : 5000 '''

At the end, 'self'.__count = 0' I think program took this part and printed it out, where is the problem?

Prune
  • 76,765
  • 14
  • 60
  • 81
dkaelfkae
  • 13
  • 3
  • 2
    Why exactly are you using leading double underscores? they mangle the namespace https://stackoverflow.com/questions/7456807/python-name-mangling – DeepSpace May 13 '21 at 15:28

1 Answers1

0

You need to learn the difference between class attributes and instance attributes. In this case, you need a class attribute: a single variable that is shared by the entire class.

class Employee:

    employee_count = 0

    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary
        Employee.employee_count += 1
        self.ID = Employee.employee_count

Eliminate your count method. __str__ should reference self.ID.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • You mean 1. Delete 'count' function. 2. And change '__init__' function as you wrote. 3. And change self.__count to self.count in __str__ ?? When I tried this, I got all of the self.count '3' – dkaelfkae May 13 '21 at 16:14
  • I think I fixed it ... you need a class count and an employee ID number. – Prune May 13 '21 at 16:21
  • Now I understand how this works. Thank you very much for your kindness. Have a good day :) – dkaelfkae May 13 '21 at 16:30