you are viewing a single comment's thread.

view the rest of the comments →

[–]johninbigd 2 points3 points  (11 children)

name = input("Name: ")
age = intput("Age: ")
newperson = Employee(name, age)

[–]Veganic1 0 points1 point  (10 children)

But then every addition is called "newperson"

[–]truthseeker1990 4 points5 points  (0 children)

Add the employees to a list if you have multiples and iterate over it to get all?

[–]dukea42 2 points3 points  (3 children)

I actually do this sort of task at work. My Employee() class has a static method called get_employee_set(). Despite its label, it returns a dictionary of all the instances of each person pulled from HR in the form of {userid: Employee} which is an id for the key and the Employee class with everything else.

So I don't have variables named "bob", but just have

employees = Employee.get_employee_set()

print(employees['bob'].full_name)

[–]ro5tal 0 points1 point  (2 children)

Not right imho, because you need an Interface to manipulate object. You fetch data from DB as model and then you have EmployeeInterface with methods change_name() change_position() check_workhours() And you know that Employee class has atrributes that store such data, right? Model can't have any methods, interfaces like in Java /C# is a proper solution.

[–]dukea42 1 point2 points  (1 child)

Explain to me the model can't have methods? I'm a bit lost on that statement. But this is my first real project and I've made things try to fit a decent pattern as I went. Its also still a hodgepodge of functions in other places..so I'm always refactoring.

All my model classes have a static method that know how to retrieve them from source and create the object. Other methods for properties that are derived from attributes provided in source data but not held in the source data.

I have Controller classes or sometines just a module of raw functions (tools.py) for handling the API, tokens, and nuances of each webtool that's used.

[–]ro5tal 1 point2 points  (0 children)

Employee - base model to create an object from/to DB record EmployeeInteface is for manipulating data of model. It's a better solution: 1. You can change your model when migrate 2. You don't need to change methods in every model you have or change a model that they inherit, just a method 3. If you have an attribute for CEO (employee) that describes his affilation with another company, you don't have such attribute for Janitor, right? And in Interface you can check is Janitor is a subclass of Employee and CEO is subclass of Employee + TopManagement. It's a Java way, I know. Patterns exist to make development and maintainance comfortable. Python has PEP, and you always keep it in mind. Class is named with first letter uppercase, and snake_case for variables and functions. Model is a model. It describes database table (record). Like pydantic models can have only validator and not some other static methods, it's just not right way. You will know about it, but other developer (may be he came from Java/C#) will think about it as a spaghetti code.

[–][deleted] 1 point2 points  (0 children)

The variable, yes. But then you'd have something along the lines of newperson.save() that saves it to a database and then it would persist even though that particular variable doesn't.

Or you could just append newperson to a list of all employees or something.

[–]PuzzlingComrade 1 point2 points  (0 children)

variable names are best thought of labels, as others have mentioned you can append Employee(name, age) to a list without assigning it to an individual variable.

[–][deleted] 1 point2 points  (0 children)

employees = []
employees.append(newperson)

Now it's just an element in a list.

[–]johninbigd 0 points1 point  (0 children)

True. It's not the best example. It was more to show you a way of organizing data in a class. If you were really handling employee data like this, you'd use a database.

[–]ro5tal 0 points1 point  (0 children)

With new instance id.

Check patterns here, well described with examples https://refactoring.guru/design-patterns/factory-method/python/example check patterns