Just want to ask some questions


#1

Hi everyone! I just want to ask some questions:
1) When I write code

milton = PartTimeEmployee(14)
print milton

It prints <PartTimeEmployee object at 0x7fa1c8291810> - is it some kind of variable's memory cell?
2) What exactly argument I should give to milton = PartTimeEmployee(14), what type? Now I see that it mentioned to be just name of employee but there is no TypeError.


This Looks Like a Job For... : arguments taken by calculate_wage
#2
  1. It is a pointer to the object in memory, yes.

  2. The instance variable is employee_name, a string.

    def __init__(self, employee_name):
        self.employee_name = employee_name

#3

Hi @bahamutfury,

Those are good questions.

<PartTimeEmployee object at 0x7fa1c8291810> is a default representation of the PartTimeEmployee instance that you created, and does include the memory address where it is stored.

If you want a print statement to output a nicer representation of the object, you can do so. One technique would be to define a __str__ method for that type of object. Here is an example that can be added to the PartTimeEmployee class definition ...

    def __str__(self):
        return "Part Time Employee: {:s}".format(self.employee_name)

Now you can do this ...

jane = PartTimeEmployee("Jane Doe")
print jane

Output ...

Part Time Employee: Jane Doe

If you want this to produce a TypeError, you need to let the Python interpreter know that only a str is acceptable ...

milton = PartTimeEmployee(14)

Here's a revised __init__ method for the Employee class that does that ...

    def __init__(self, employee_name):
        if not isinstance(employee_name, str):
            raise TypeError("The name needs to be a string.")
        self.employee_name = employee_name

Now, if you do this ...

jack = PartTimeEmployee(17)

... you'll see something like this ...

Traceback (most recent call last):
  File "python", line 27, in <module>
  File "python", line 5, in __init__
TypeError: The name needs to be a string.

#4

Thank you.The same as for__str__ I did for __repr__ in lesson a few days ago. And what's about raise - I readed about it previously and I expected that error will be shown without calling except handler.


#5

As far as the Python interpreter is concerned, there is nothing wrong with supplying an int as the argument when an Employee is instantiated, and the following __init__ method is called ...

    def __init__(self, employee_name):
        self.employee_name = employee_name

We, of course, are aware that employee_name should be a str, but the interpreter does not know that, so no error is raised.

One issue that Codecademy does not delve into very deeply is how to design objects in a manner that makes debugging easier. This exercise could be made into a case study for improving software design. Since the __init__ method for this exercise was not designed to prevent an int from being assigned to self.employee_name, a programmer could instantiate an Employee with an int or even a bool. That could lead to an error later on in the program, when some code attempts to process it as a str. By that stage, the bug is more complicated to trace. That is one reason why it is good to write functions that catch potential problems as early as possible, and why good programmers think about the kinds of issues that you raised with your questions.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.