What is a good way to avoid duplication of a class instance when it is created using the __init
__() function.
This question is a result of this issue.
Context (using employee class example):
- Lets say I have an employee class:
__init
__(self,name,dept) - I also have a method, employee.info(self) that prints out name and dept of any employee.
- However a user could just add an employee by calling a=employee(args..). They could do it multiple times using the same instance variable a, but different employee names.
- This will cause issues if they try to print a.info(), as each time a different employee name will be printed.
Is there a better way to do this? I know it is possible to have the __init
__() "pass" and define a new method to create an instance.
Expect results:
>>Adam=employee('marketing')
>>Adam.info()
>>Adam works in marketing.
OR
>>a=employee('Adam','marketing')
>>a=employee('Mary','marketing')
>>Error: employee instance with identifier "a" already exists.
>>Use employee.update() method to modify existing record.
Is there a cleaner way of doing it? (as you might guess, I am still learning python). Or is it good practice to write an explicit function (instead of a class method) to add new employees?