[Tutor] Why None?

Christopher Spears cspears2002 at yahoo.com
Tue Feb 7 21:08:56 CET 2006


Here is some code I wrote:

class Food:
	def __init__(self, name):
		Food.foodName = name

class Customer:
	def __init__(self,name):
		Customer.name = name
		Customer.food = 0
	def placeOrder(self, foodName, employee):
		print "Hi %s!" % employee.name
		print "I want %s please! " % foodName
		self.food = employee.takeOrder(foodName)
	def printFood(self):
		print self.food.foodName
		

class Employee:
	def __init__(self, name):
		Employee.name = name
	def takeOrder(self, foodName):
		print "%s coming up!" % foodName
		food = Food(foodName)
		return food
		
class Lunch:
	def __init__(self):
		self.employee = Employee('Dave')
		self.customer = Customer('Chris')
	def order(self, foodName):
		self.customer.placeOrder(foodName, self.employee)
	def result(self):
		print self.customer.name, "has",
self.customer.printFood()
		
if __name__ == '__main__':
	meal = Lunch()
	meal.order('spam')
	meal.result()
	
	
Basically, a Lunch creates a Customer and an Employee.
 When Lunch's order method is called, the Customer
places an order with placeOrder, which activates
takeOrder from the Employee.  takeOrder creates a Food
object using the foodName string that has been passed
down from Lunch's order method.  The Food object is
then passed to Customer.  I then run Lunch's result
method to see if Customer received the Food object.

cspears at iaws09:/imports/home/cspears/Documents/Python/chap23/ex07>
python lunch.py
Hi Dave!
I want spam please!
spam coming up!
Chris has spam
None

This is almost correct.  Why does None appear?  This
is the only fly in the ointment.  I'm probably missing
something subtle.


More information about the Tutor mailing list