Python allows us to create an inner class within a python class just like how we create any ordinary class.
In following example I have created a new class within the PersonalData class which I have created before so that new class will behave like a list which allows us to insert the previous job history of an employee into it so we can retrieve them later on.
class JobHistory(list): def __init__(self): list.__init__()
As you can see we make that inner class extends the list class so it behaves just like a list. Now we can use it just like any other class by initiating that inner class and then inserted job history of an employee into it in the runmyclass.py module.
personalData = PersonalData("John", "computer programmer","1000") #create an instance of the PersonalData class job_history = personalData.JobHistory() #create an instance of the JobHistory inner class job_history.append('python programmer') #append history to the list job_history.append('web developer') job_history.append('android developer')
Print out the job history is also very simple.
print ("Before this " + personalData.name + " is a : ") print("") for job in job_history: print(job)
The outcome for the entire program is as follow:
John works as a computer programmer and he has a monthly income of 1000
John now works as a project manager and he has a monthly income of 3000
Before this John is a :