The code looks fine. However, there are a few things you can do to improve it:
- first, avoid using Capital letters for variable. In Python, they should be named in snake_case;
- use snake_case for
setters
(e.g., set_name
) and getters
as well, or use properties instead. See https://www.python-course.eu/python3_properties.php for more information.
- You can avoid instantiating the variable in the
__init__
.
- If you are using Python 3.X, consider specifying the parameters types and return types.
Here is how I would refactor your code:
class Employee:
@property
def name(self):
return self.__name
@name.setter
def name(self, name: str):
self.__name = name
@property
def age(self):
return self.__age
@age.setter
def age(self, age: int):
self.__age = age
@property
def company(self):
return self.__company
@company.setter
def company(self, company: str):
self.__company = company
@property
def xp(self):
return self.__xp
@xp.setter
def xp(self, lvlup: float):
self.__xp = lvlup
However, you should use setters if you need to perform some checks before assigning the value to the variable (e.g., you want to check that the passed age > 18
or that the title is at least "a bachelor degree", and so forth.
If you do not need those checks, you can simply call the variable, for example:
class Employee:
def __init__(self):
self.name = ''
self.id = 0
self.age = 0
self.company = 0
self.title = ''
self.level = 0
self.xp = 0.0
self.tasks = []
self.projects = []
employee = Empolyee()
employee.name = 'Foo Bar'
employee.age = 30
Instead of using setters or populating the variables one by one, you can further improve the code by passing the parameters to the Employee class constructor:
class Employee:
def __init__(self, name:str, age:int, company:int, id:int=None, title:str=None,
xp=float=None, task:list=None, projects:list=None):
self.name = name
self.id = id
self.age = age
self.company = company
self.title = title
self.level = level
self.xp = xp
self.tasks = tasks
self.projects = projects
# Properties here (getters and setters) ...
Then, read_info
will become:
def read_info(self, name, age, company):
e = Employee.Employee(name=name, age=age, company=company)
self.employees.append(e)
Update
As highlighted by hjpotter, a more suitable approach in this case would use dataclasses.
Therefore, the class can be rewritten as:
from dataclasses import dataclass
@dataclass
class Employee:
name: str
age: int
company: int
id: int = 0
title: str = None
level: int = 0
xp: float = 0.0
tasks: list = None
projects: list = None
The __init__
is created automatically, and a new object can be instantiated as in the previous example:
def read_info(self, name, age, company):
e = Employee.Employee(name=name, age=age, company=company)
self.employees.append(e)