Jac Mulder
Jac Mulder

Reputation: 47

How do I generate a new instance inside of a class method?

I created a class for my text adventure game where players can hire an applicant. But once they hire the applicant that person will become an employee. So I would like for that applicant instance to become an instance for my Employee class. Is there a way to do it or a better way?

Here is my applicant class:

class Applicant:
    def __init__(self, full_name, first_name, gender, previous_job, dob, 
                 hire_score):  # dob stands for date of birth.
        self.full_name = full_name
        self.first_name = first_name
        self.gender = gender
        self.previous_job = previous_job
        self.dob = dob
        self.hire_score = hire_score

Here is the method for when I hire the applicant. Before the applicant appends to my employee list I would like to automatically create a new instance for the applicant in the Employee class.

def hire(self):
    print(f"{mPlayer.name}: Okay {self.first_name}, I will be hiring you.")
    time.sleep(a)
    print(f"{self.first_name}: Thank you so much!! I look forward to working and will not let you down.")
    employee_list.append(self)
    applicant_list.remove(self)
    print(f"You have now hired {self.first_name}")
    time.sleep(a)
    print("You can view employee information and see their activities in the View Employee section.")

Here is my Employee class:

class Employee:
    def __init__(self, full_name, first_name, gender, previous_job, dob, 
                 hire_score, position, schedule):
        self.full_name = full_name
        self.first_name = first_name
        self.gender = gender
        self.previous_job = previous_job
        self.dob = dob
        self.hire_score = hire_score
        self.position = position
        self.schedule = schedule

Upvotes: 0

Views: 102

Answers (2)

Sazzy
Sazzy

Reputation: 1994

I have a few suggestions for you. I will use dataclasses to demonstrate the concepts (Ref: https://docs.python.org/3/library/dataclasses.html).

  1. Why not to have the same class, but have a flag specifying your person is an employee, eg:
from dataclasses import dataclass, asdict

@dataclass
class Person:  # <- name a better name
    is_employee: bool = False
  1. Use inheritance. Both classes have a lot of repeated fields, thus inheritance should work perfectly in your example.
class Applicant:
    name: str
    dob: datetime.date
    score: int

@dataclass
class Employee(Applicant):
    position: str
    schedule: object

Then if you have an instance of an applicant, you can easily create an employee instance from it, without repeating all the fields:

applicant = Applicant(name='John', ...)
employee = Employee(**asdict(applicant), position='Manager', ...)

Upvotes: 1

itprorh66
itprorh66

Reputation: 3288

You would need to do something like the following:

print(f"{self.first_name}: Thank you so much!! I look forward to working and will not let you down.")
new_employee = Employee(full_name, first_name, gender, previous_job, dob, hire_score, position, schedule)
employee_list.append(self)

You might also want to not append the applicant record, but change the last line to: employ_list.append(new_employee)

Upvotes: 1

Related Questions