Home > database >  Python Class Function not defined
Python Class Function not defined

Time:02-16

I'm creating a class with function within, but i keep getting error "name 'direct_report' is not defined"

Basically im tring to make an organization chart, creating a list using the direct_report function to add people under each position

class employee:
def __init__(self, name , title, salary):
    self.name = name
    self.title = title
    self.salary = salary
    self.direct_reports_list = direct_report()

def __str__(self):
    #otheremp_list = []
    print(self.title,'-', self.name)
    print('Direct Reports:')
    for emp in self.direct_reports_list:
        print(emp.title,'-', emp.name)
    #    otheremp_list.append(emp.direct_reports_list)
    #print('other employees:')
    #for emp in otheremp_list:
    #    print(emp.title,'-', emp.name)
    #    otheremp_list.append(emp.direct_reports_list)

def direct_report(self,value):
    print(value)
    direct_reports_list = []
    direct_reports_list.append(value)
    print(direct_reports_list)
    return direct_reports_list

ceo = employee("Elon Musk", "CEO",1000000)
devdir = employee("Jeff Bezos","Development Director",500000)
devassoc1 = employee("Beyonce Knowles","Development Associate", 50000)
devassoc2 = employee("Taylor Swift","Development Associate", 50000)
ceo.direct_report(devdir)
ceo.direct_report(devdir2)
devdir.direct_report(devassoc1)
devdir.direct_report(devassoc2)
print(ceo)

The # is my further plan to print the full organization chart, but currently im still stuck at the "direct report" parts

CodePudding user response:

Try this code.

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary
        self.direct_reports_list = []

    def __str__(self):
        #otheremp_list = []
        print(self.title,'-', self.name)
        print('Direct Reports:')
        for emp in self.direct_reports_list:
            print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)
        #print('other employees:')
        #for emp in otheremp_list:
        #    print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)
        return "Done"

    def direct_report(self,value):
        self.direct_reports_list.append(value)

ceo = employee("Elon Musk", "CEO",1000000)
devdir = employee("Jeff Bezos","Development Director",500000)
devassoc1 = employee("Beyonce Knowles","Development Associate", 50000)
devassoc2 = employee("Taylor Swift","Development Associate", 50000)
ceo.direct_report(devdir)
devdir.direct_report(devassoc1)
devdir.direct_report(devassoc2)
print(ceo)
print(devdir)
print(devassoc1)
print(devassoc2)

CodePudding user response:

You need to add one indentation level for the classes methods like that:

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary
        self.direct_reports_list = direct_report()

    def __str__(self):
        #otheremp_list = []
        print(self.title,'-', self.name)
        print('Direct Reports:')
        for emp in self.direct_reports_list:
            print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)
        #print('other employees:')
        #for emp in otheremp_list:
        #    print(emp.title,'-', emp.name)
        #    otheremp_list.append(emp.direct_reports_list)

    def direct_report(self,value):
        print(value)
        direct_reports_list = []
        direct_reports_list.append(value)
        print(direct_reports_list)
        return direct_reports_list

ceo = employee("Elon Musk", "CEO",1000000)
devdir = employee("Jeff Bezos","Development Director",500000)
devassoc1 = employee("Beyonce Knowles","Development Associate", 50000)
devassoc2 = employee("Taylor Swift","Development Associate", 50000)
ceo.direct_report(devdir)
ceo.direct_report(devdir2)
devdir.direct_report(devassoc1)
devdir.direct_report(devassoc2)
print(ceo)

CodePudding user response:

Call the function in this way.

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary
        self.direct_reports_list = self.direct_report()

CodePudding user response:

try self.direct_report() instead as you are calling a method within the class

  • Related