Python Class Creation Confusion?

A forum for general discussion of the Python programming language.

Python Class Creation Confusion?

Postby forum_dweller » Sun Jun 29, 2014 7:21 pm

My program is suppose to create a class named Empoyee that holds the following data about an empoyee in attributes: name, id number, department, & job title. Second, create 3 objects to hold specified data. Finally, the program should store/display the specified data in the 3 objects.

I am receiving the following Traceback error:

TypeError: 'str' object is not callable

Code: Select all
while True:
   try:
      class Employee:
         def __init__(self, name, idNumber, department, jobTitle):
            self.name = name
            self.idNumber = idNumber
            self.department = department
            self.jobTitle = jobTitle
         def employee_1_information(self, name, idNumber, department, jobTitle):
            self.name = name
            self.idNumber = idNumber
            self.department = department
            self.jobTitle = jobTitle
            return self.name, self.idNumber, self.department, self.jobTitle
         def employee_2_information(self, name, idNumber, department, jobTitle):
            self.name = name
            self.idNumber = idNumber
            self.department = department
            self.jobTitle = jobTitle
            return self.name, self.idNumber, self.department, self.jobTitle
         def employee_3_information(self, name, idNumber, department, jobTitle):
            self.name = name
            self.idNumber = idNumber
            self.department = department
            self.jobTitle = jobTitle
            return self.name, self.idNumber, self.department, self.jobTitle
         
      def main():
      
         name = "Susan Meyers"
         idNumber = "47899"
         department = "Accounting"
         jobTitle = "Vice President"
         employeeData1 = Employee(name, idNumber, department, jobTitle)
         employeeData1.name(name)
         employeeData1.idNumber(idNumber)
         employeeData1.department(department)
         employeeData1.jobTitle(jobTitle)
         
         name = "Mark Jones"
         idNumber = "39119"
         department = "IT"
         jobTitle = "Programmer"
         employeeData2 = Employee.employee_2_information(name, idNumber, department, jobTitle)
         employeeData2.name(name)
         employeeData2.idNumber(idNumber)
         employeeData2.department(department)
         employeeData2.jobTitle(jobTitle)
         
         name = "Joy Rogers"
         idNumber = "81774"
         department = "Manufacturing"
         jobTitle = "Engineer"
         employeeData3 = Employee.employee_3_information(name, idNumber, department, jobTitle)
         employeeData3.name(name)
         employeeData3.idNumber(idNumber)
         employeeData3.department(department)
         employeeData3.jobTitle(jobTitle)
         
         print(employeeData1.employee_1_informaton())
         print(employeeData2.employee_2_information())
         print(employeeData3.employee_3_information())
               
   except ValueError:
      print("Error.")
      break
   else:
      break

main()
Last edited by micseydel on Sun Jun 29, 2014 7:43 pm, edited 1 time in total.
Reason: Embedded attached file in code tags, deleted file, locked OP.
forum_dweller
 
Posts: 9
Joined: Sun Jun 29, 2014 7:18 pm

Re: Python Class Creation Confusion?

Postby Yoriz » Sun Jun 29, 2014 8:08 pm

Welcome to the forum.
Please read the new users read this link in my signature.

That is not how classes are used, you dont create an employee class and add each detail of each employee into it
Create one class that has the attribrutes once that each employee has, and then create instance of each employee using the employee class.
Code: Select all
class Employee:
    def __init__(self, name, idNumber, department, jobTitle):
        self.name = name
        self.idNumber = idNumber
        self.department = department
        self.jobTitle = jobTitle

    def __str__(self):
        strings = ('Employee(name={}'.format(self.name),
                   'idNumber={}'.format(self.idNumber),
                   'department={}'.format(self.jobTitle),
                   'jobTitle={})'.format(self.jobTitle))
        return ', '.join(strings)


def main():

    name = "Susan Meyers"
    idNumber = "47899"
    department = "Accounting"
    jobTitle = "Vice President"
    employeeData1 = Employee(name, idNumber, department, jobTitle)

    name = "Mark Jones"
    idNumber = "39119"
    department = "IT"
    jobTitle = "Programmer"
    employeeData2 = Employee(name, idNumber, department, jobTitle)

    name = "Joy Rogers"
    idNumber = "81774"
    department = "Manufacturing"
    jobTitle = "Engineer"
    employeeData3 = Employee(name, idNumber, department, jobTitle)

    print employeeData1
    print employeeData2
    print employeeData3


main()

Output
Code: Select all
Employee(name=Susan Meyers, idNumber=47899, department=Vice President, jobTitle=Vice President)
Employee(name=Mark Jones, idNumber=39119, department=Programmer, jobTitle=Programmer)
Employee(name=Joy Rogers, idNumber=81774, department=Engineer, jobTitle=Engineer)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 2 guests