Throwing Methods Around : Class Definition « Class « Python Tutorial






class FoodExpert:
   def init(self):
      self.goodFood = []

   def addGoodFood(self, food):
      self.goodFood.append(food)

   def likes(self, x):
      return x in self.goodFood

   def prefers(self, x, y):
      x_rating = self.goodFood.index(x)
      y_rating = self.goodFood.index(y)
      if x_rating > y_rating:
         return y
      else:
         return x 

f = FoodExpert()
f.init()
map(f.addGoodFood, ['A', 'B', 'C', 'D', 'E'])
print f.goodFood

menu = ['a', 'b', 'c']
rec = filter(f.likes, menu)
print rec








11.9.Class Definition
11.9.1.Demonstrates a basic class and object
11.9.2.Creating a Class (Class Definition)
11.9.3.Classes and Types
11.9.4.Defining Class Methods with the def Statement
11.9.5.Rectangle class
11.9.6.Throwing Methods Around
11.9.7.Simple definition of class Time.
11.9.8.Definition of class Date.