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:


class Student:

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

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

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

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

    def lname(self, lname):
        """Sets last name (no numbers or spaces allowed)"""
        if lname.isalpha():
            self.__lname = lname
            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)

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

    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)

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

    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)
        student = Student(first)

    # Assign last_name using our object's property then return student
    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)

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

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

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: