This is from the tutorial about Abstract Classes Python Abstract Class
In this tutorial they state: " To structure the program, we’ll use modules, where each class is placed in a separate module (or file)"
I created 5 files called employee.py, payroll.py fulltimeemployee.py etc and called them in app.py
All files in same folder. Here is the code of the app.py
from employee import Employee
from fulltimeemployee import FulltimeEmployee
from hourlyemployee import HourlyEmployee
from payroll import Payroll
payroll = Payroll()
payroll.add(FulltimeEmployee('John', 'Doe', 6000))
payroll.add(FulltimeEmployee('Jane', 'Doe', 6500))
payroll.add(HourlyEmployee('Jenifer', 'Smith', 200, 50))
payroll.add(HourlyEmployee('David', 'Wilson', 150, 100))
payroll.add(HourlyEmployee('Kevin', 'Miller', 100, 150))
Here is the abstract class in employee.py
from abc import ABC, abstractmethod
class Employee(ABC):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
@property
def full_name(self):
return f"{self.first_name} {self.last_name}"
@abstractmethod
def get_salary(self):
pass
Here is what I get in the shell when I run app.py:
= RESTART: /Volumes/HOMEX/brianx/Training n Studies/Brilliant courses/Python/pythontutorial.net/Test/app.py
Traceback (most recent call last):
File “/Volumes/HOMEX/brianx/Training n Studies/Brilliant courses/Python/pythontutorial.net/Test/app.py”, line 2, in
from fulltimeemployee import FulltimeEmployee
File “/Volumes/HOMEX/brianx/Training n Studies/Brilliant courses/Python/pythontutorial.net/Test/fulltimeemployee.py”, line 1, in
class FulltimeEmployee(Employee):
NameError: name ‘Employee’ is not defined
So it seems to me that python is seeing the files, but not importing them and executing the code.
This is my first run at importing modules in external files. Not sure where I screwed up. (but pretty sure the error is me!)
Any help, appreciated.
Cheers