0

I am trying to make a class, to store the biography data of employees and store them in a dictionary, as you can see.

I want to ask if it is a bad practice to change class variables. If yes, please explain why it is so. And what can be perfect way of doing the thing I am doing?

# Defining a class for storing the information related to the instructors at bcoder company
class Employee:
    company_records = {}
    Employee_count = 0

    def __init__(self, fname, lname, email_id, contact, gender):
        self.fname = fname
        self.lname = lname
        self.email_id = email_id
        self.contact = contact
        self.gender = gender
        self.add_records()

    # Defining a function for returning the full name of our employee
    def fullname(self):
        return("{} {}".format(self.fname, self.lname))

    def add_records(self):
        Employee.Employee_count += 1
        Employee.company_records[Employee.Employee_count] = {
            "Name": self.fullname(),
            "Email_id": self.email_id,
            "Contact": self.contact,
            "Gender": self.gender
        }


emp_1 = Employee("Vaibhav", "Yadav", "vaibhavy1912@gmail.com", 9432098712, "Male")
print(emp_1.fullname())
print(Employee.company_records)
emp_2 = Employee("vaibhav", "Gupta", "hg1123@gmail.com", 9323118976, "male")
print(Employee.company_records)
emp_2 = Employee("Yogesh", "Yadav", "yogesh1123@gmail.com", 9323118976, "male")
print(Employee.company_records)

What is an elegant way of doing the above thing?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • This should answer your question: https://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private – S P Sharan Aug 18 '21 at 06:01
  • can u please tell the answer to other questions also , i think this answered the part of hiding , i guess , but i had asked a couple of other things – Vaibhav Yadav Aug 18 '21 at 06:03
  • is it a good practice , to make changes in class variables , or could it lead to unexpected results ,as i am doing by making changes in a dictionary which is a class variable – Vaibhav Yadav Aug 18 '21 at 06:06
  • 2
    How do you do two separate companies within the same program with this? You can’t. — The `Employee` should model exactly *one employee*, nothing more. You can then instantiate multiple `Employee`s and put them in a list or dict; that’s your “company” then. You can write a second `Company` class that manages that list. – deceze Aug 19 '21 at 05:00
  • Yes , i can do that , but i am not doing for two separate companies. It is for a single company , to instantiate the employees , and store their data in a dictionary. – Vaibhav Yadav Aug 19 '21 at 05:42
  • I want to know , exactly , what is wrong with this approach. – Vaibhav Yadav Aug 19 '21 at 05:43
  • Well, is there anything fundamentally wrong in the sense that your program will crash? No. Is it possible to paint yourself into a corner with this because the structure isn't flexible enough to handle anything but one very specific use case? Yes. That's usually the issue: it works fine for you now, but if the requirements are even slightly changed, or you want to add something that requires more flexibility like unit testing, then you need to bend over backwards more than you would have to if you'd have structured it better to begin with. – deceze Aug 19 '21 at 06:45
  • Even a separate company does not make each employee update company records: that's what HR is for. – chepner Jan 15 '22 at 14:38

0 Answers0