TypeError: ‘str’ object is not callable – Python (already tried debugging)

It doesn't seem like I am making lname a string. The error is being thrown at:

student.lname(last)

class Student:

    def __init__(self, fname=""):
        """Initializes with passed first name and blank last name"""
        self.__fname = fname
        self.__lname = ""

    @property
    def fname(self):
        """Getter for first name"""
        return self.__fname.upper()

    @fname.setter
    def fname(self, fname):
        """Sets last name (no numbers or spaces allowed)"""
        if fname.isalpha():
            self.__fname = fname
        else:
            self.__fname = "Unknown"

    @property
    def lname(self):
        """Getter for last name"""
        return self.__lname.upper()

    @lname.setter
    def lname(self, lname):
        """Sets last name (no numbers or spaces allowed)"""
        if lname.isalpha():
            self.__lname = lname
        else:
            self.__lname = "Unknown"

    def __str__(self):
        """Override for str, returns student name"""
        return self.__fname + self.__lname


class GradStudent(Student):

    def __init__(self, thesis, fname=""):
        """Initializes gradstudent with thesis field. (fname passed to superclass)"""
        self.__thesis = thesis
        Student.__init__(self, fname)

    @property
    def thesis(self):
        """Getter for thesis"""
        return self.__thesis.upper()

    @thesis.setter
    def thesis(self, thesis):
        """Setter for thesis"""
        self.__thesis = "Thesis: " + thesis

    def __str__(self):
        """Override the overriden __str__ from the superclass"""
        return f'{Student.__str__()}\n\t{self.thesis}'


class PhDStudent(Student):

    def __init__(self, dissertation, fname=""):
        """Initializer for phdstudent. adds dissertaion field. fname passed to superclass"""
        self.__dissertation = dissertation
        Student.__init__(self, fname)

    @property
    def dissertation(self):
        """Getter for dissertation"""
        return self.__dissertation.upper()

    @dissertation.setter
    def dissertation(self, dissertation):
        """Setter for dissertation"""
        self.__dissertation = "Dissertation: " + self.__dissertation


def add_student(studentType):
    """Get student data and create an object to be returned"""
    student = None
    # Get first and last name here because all students need this data
    first = input('Enter first name: ')
    last = input('Enter last name: ')

    # Determine student type and construct an object and save in student
    if studentType.upper() == "G":
        thesisTitle = input("Enter the thesis title: ")
        student = GradStudent(thesisTitle, first)
    elif studentType.upper() == "P":
        dissertationTitle = input("Enter the dissertation title: ")
        student = PhDStudent(dissertationTitle, first)
    else:
        student = Student(first)

    # Assign last_name using our object's property then return student
    student.lname(last)
    return student


# Main Function def main():
    """Main program logic"""
    students = []
    entry = ''
    print("{:^50}".format('Student Management System'))

    while entry != 'X':
        studentTypes = ['S', 'G', 'P']
        # Get user entry and capitalize the entry
        entry = input(
            '\nEnter (S)tudent, (G)radStudent, (P)hDStudent or (X)exit: ')
        entry = entry.upper()

        # Is user entry one of studentTypes. Yes - add_student to list
        if entry in studentTypes:
            student = add_student(entry)
            students.append(student)

    # print students and dissertation if the student is a PhD type
    print("\nThe following students were added...")
    for s in students:
        print(s)
        if isinstance(s, PhDStudent):
            print(f'\t{s.dissertation}')


if __name__ == "__main__":
    # call and execute the main function
    main()


Read more here: https://stackoverflow.com/questions/64399449/typeerror-str-object-is-not-callable-python-already-tried-debugging

Content Attribution

This content was originally published by TonyVance at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: