为什么"食物= 4"和" fun = 4",内在的逻辑是什么?

时间:2015-08-28 02:54:28

标签: python python-3.x

# Critter Caretaker
# A virtual pet to care for


   class Critter(object):
       def __init__(self, name, hunger = 0, boredom = 0):
           self.name = name
           self.hunger = hunger
           self.boredom = boredom

       def __pass_time(self):
           self.hunger += 1
           self.boredom += 1

       @property
       def mood(self):
           unhappiness = self.hunger + self.boredom
           if unhappiness < 5:
               m = "happy"
           elif 5 <= unhappiness <= 10:
               m = "okay"
           elif 11 <= unhappiness <= 15:
               m = "frustrated"
           else:
               m = "mad"
           return m
       def talk(self):
           print("I'm", self.name, "and I feel", self.mood, "now.\n")
           self.__pass_time()

       def eat(self, food = 4): # why food = 4?
           print("Brruppp. Thank you.")
           self.hunger -= food
           if self.hunger < 0:
               self.hunger = 0
           self.__pass_time()

       def play(self, fun = 4): # why fun = 4?
           print("Wheee!")
           self.boredom -= fun
           if self.boredom < 0:
               self.boredom = 0
           self.__pass_time()

   def main():
       crit_name = input("What do you want to name your critter?: ")
       crit = Critter(crit_name)

       choice = None
       while choice != "0":
           print \
           ("""
           Critter Caretaker

           0 - Quit
           1 - Listen to your critter
           2 - Feed your critter
           3 - Play with your critter
           """)

           choice = input("Choice: ")
           print()

           #exit
           if choice == "0":
               print("Good-bye.")

           # listen to your critter
           elif choice == "1":
               crit.talk()

           # feed your critter
           elif choice == "2":
               crit.eat()

           # play with your critter
           elif choice == "3":
               crit.play()

           # some unknown choice
           else:
               print("\nSorry, but", choice, "isn't a valid choice.")

   main()
   ("\n\nPress the enter key to exit.")

1 个答案:

答案 0 :(得分:1)

针对函数(或方法)声明中的参数的替换操作定义其默认值。

def eat(self, food = 4):
    ...

此声明表示调用eat方法时不带参数,参数food的值将为4。 当然,当使用特定参数调用eat方法时,参数food的值将从参数的值中获取。