[Tutor] lunch.py

Ian Jones ianjones at umich.edu
Tue Feb 7 19:35:53 CET 2006


In article <20060207165036.30979.qmail at web51603.mail.yahoo.com>,
 Christopher Spears <cspears2002 at yahoo.com> wrote:

> When I run the program, I get the following error [snip]

The simple mechanical error is that when you're substituting more than 
one value, you need to wrap the value list in parens:

      print "%s, I want, %s please! " % (Employee.name, food.foodName)

(from Customer.placeOrder())


At this point your program, as written, gives the expected output. There 
is a more serious conceptual error, though. In your various __init__() 
methods, you are assigning the name attribute of the *class*, rather 
than the name of the *instance*.

That is, you are saying "the name of all employees is Dave" rather than 
"the name of this employee is Dave."

class Employee:
   def __init__(self, name):
      Employee.name = name  # <-- assigns class attribute (name of all 
Employees)

To illustrate, try running this as your __main__:

if __name__ == '__main__':
   meal = Lunch()
   meal.order('Chris', 'spam')
   # next line shouldn't replace Dave, but it does
   firedEmployee = Employee("Bill")
   meal.order('Jake', 'eggs')

In your __init__() methods, you should assign to self instead of to the 
class. For example:

class Employee:
   def __init__(self, name):
      self.name = name  # <-- assigns instance attribute (this 
Employee's name)



More information about the Tutor mailing list