-1
import employee
def main():
    print('This program will create employee objects and store their information')
    print()
    ledger = []
    numOfEmployees = int(input("Enter the number of employee's to enter: "))
    for count in range(numOfEmployees):
        name = input("Enter employee's name: ")
        id_number = input("Enter employee's ID number: ")
        department = input("Enter employee's department: ")
        job_title = input("Enter employee's job title: ")
        count = employee.Employee(name, id_number, department, job_title)
        ledger.append(count)
    print("This is the list of employee's:")
    for person in ledger:
        print(person)
main()

the following is my employee module. I know making the ledger inside the module would make more sense but it seems like I should probably figure out how to do it an easier way before I try implementing that.

class Employee:
    def __init__(self, name, id_number, department, job_title):
        self.__name = name
        self.__id_number = id_number
        self.__department = department
        self.__job_title = job_title
    def set_name(self, name):
        self.__name = name
    def set_id_number(self, id_number):
        self.__id_number = id_number
    def set_department(self, department):
        self.__department = department
    def set_job_title(self, job_title):
        self.__job_title = job_title
    def get_name(self):
        return self.__name
    def get_id_number(self):
        return self.__id_number
    def get_department(self):
        return self.__department
    def get_job_title(self):
        return self.__job_title

the following is the output.

This program will create employee objects and store their information

Enter the number of employee's to enter: 1
Enter employee's name: adf
Enter employee's ID number: afd
Enter employee's department: asdf
Enter employee's job title: asdf
This is the list of employee's:
<employee.Employee object at 0x000001A96A92FF98>

I want it to actually print out the values and not just its location in memory... How can I do this?

chepner
  • 497,756
  • 71
  • 530
  • 681
GOD
  • 33
  • 9
  • you need to add a `__str__` method to your `Employee` class. See [here](https://stackoverflow.com/a/7784214/2958070) . Also, your code looks a lot more like Java than Python. I suggest looking [here](https://www.youtube.com/watch?v=wf-BqAjZb8M) for that – Ben Apr 24 '18 at 18:26
  • Your `Employee` class has a lot of unnecessary clutter, especially for a MCVE. Get rid of all the getter/setter methods and use regular attribute names. – chepner Apr 24 '18 at 18:27

2 Answers2

3

To print objects, you need to supply a definition for what their string representation is. And then, you describe that in the __str__() override:

For example, yours could be something like:

class Employee
    ... # other functions
    def __str__(self):
        return "{}, {}, {}".format(self.__name, self.__id_number, self.__department)

Obviously, you can decide to format it however you want.

Also, you should look up @property decorators so you don't need to make your own getters and setters, but that's aside from your question.

OneRaynyDay
  • 3,658
  • 2
  • 23
  • 56
  • In a custom class, `__str__()` is not an *override*. – wwii Apr 24 '18 at 18:28
  • @wwii if you call `dir(cls)` on any class, you can see that one of the callable attributes of a class is `__str__`. Perhaps `override` isn't the right term, what would you say it is? – OneRaynyDay Apr 24 '18 at 18:32
  • I take your point, I'll delete my comment. I guess I don't think of it that way - I think of it as defining a `__str__` method for a class instead of overriding `type`'s `__str__` method. – wwii Apr 24 '18 at 18:39
0

Define a __str__ method for your object. This method is automatically called by Python when the object is used in a context where a string is expected (typically, print).

E.g.:

class Employee:
    def __str__(self):
        return self.__name
Guybrush
  • 2,680
  • 1
  • 10
  • 17