2017-08-07 141 views
1

我的問題是關於當兩個模塊中有兩個相同的命名函數時,計算機如何選擇運行函數。其中一個模塊是導入的。具有相同函數名稱的兩個文件Python

這是Pythonschool的一個例子。

我有一個名爲crops.py文件:

from wheat_class import * 
from potato_class import * 

#test program to select a crop and manage the crop 

def display_menu(): 
    print() 
    print("Which crop would you like to create?") 
    print() 
    print("1. Potato") 
    print("2. Wheat") 
    print() 
    print("Please select an option from the above menu") 

def select_option(): 
    valid_option = False 
    while not valid_option: 
     try: 
      choice = int(input("Option selected: ")) 
      if choice in (1,2): 
       valid_option = True 
      else: 
       print("Please enter a valid option") 
     except ValueError: 
      print("Please enter a valid option") 
    return choice 

def create_crop(): 
    display_menu() 
    choice = select_option() 
    if choice == 1: 
     new_crop = Potato() 
    elif choice == 2: 
     new_crop = Wheat() 
    return new_crop 

def main(): 
    new_crop = create_crop() 
    manage_crop(new_crop) 

if __name__ == "__main__": 
    main() 

wheat_class和potato_class是類叫做作物的孩子。 作物類中定義crop_class.py:

class Crop: 
    """A generic food crop""" 

    #constructor = runs automatically when instantiating 
    def __init__(self,growth_rate,light_need,water_need): 
     #set the attributes 
     #if underscore in front of name, private attributes 

     self._growth = 0 
     self._days_growing = 0 
     self._growth_rate = growth_rate 
     self._light_need = light_need 
     self._water_need = water_need 
     self._status = "Seed" 
     self._type = "Generic" 

    def needs(self): 
     some code 

    def report(self): 
     some code 

    def _update_status(self): 
     #code for updating status of crop 

    def grow(self,light,water): 
     #code increasing growth value 

def auto_grow(crop,days): 
    some code 

def manual_grow(crop): 
    some code 

def display_menu(): 
    print("1. Grow manually over 1 day") 
    print("2. Grow automatically over 30 days") 
    print("3. Report status") 
    print("0. Exit test program") 
    print() 
    print("Please select an option from the above menu") 

def get_menu_choice(): 
    option_valid = False 
    while not option_valid: 
     try: 
      choice = int(input("Option Selected: ")) 
      if 0 <= choice <= 3: 
       option_valid = Tsame furue 
      else: 
       print("Value entered not valid - please enter a value between 0 and 3") 
     except ValueError: 
      print("Value entered not valid - please enter a value between 0 and 3") 
    return choice 

def manage_crop(crop): 
    print("This is the crop management program") 
    print() 
    noexit = True 
    while noexit: 
     display_menu() 
     option = get_menu_choice() 
     if option == 1: 
      manual_grow(crop) 
     elif option == 2: 
      auto_grow(crop,30) 
     elif option == 3: 
      print(crop.report()) 
      print() 
     elif option == 0: 
      noexit = False 
      print() 

我的問題是關於函數display_menu()。 正如所見,函數在crops.py和crop_class.py中都存在。從crops.py 當crops.py主要功能是運行時,display_menu()被從crop_class.py運行 new_crop = create_crop() 而display_menu()被運行 manage_crop(new_crop)

我很困惑,因爲這兩個功能都不歸功於特定的類。 crop_class.py中的display_menu()縮進的方式不是Crop類的一部分。 因此,我很困惑計算機如何選擇運行哪些代碼。對此規則的破壞會很有幫助。

回答

0

當您使用*的Python中的模塊import時,您可以訪問模塊中的所有類和方法。

對於manage_cropcreate_crop,所述crops.py文件能夠從crop_class.py訪問方法,因爲它已經被導入(間接通過wheat_class和/或potato_class)。

希望有幫助!

查看importmodules文檔以獲取更多信息。

+0

謝謝。不幸的是我仍然感到困惑,因爲如果'crops.py'可以訪問'crop_class.py'中的'display_menu()',那麼'create_crop()'如何在'crop.py'中使用'display_menu()'?我正在閱讀文檔。 –

+0

'crops.py'可以訪問'crop_class.py'中的函數,因爲它是按照答案間接導入的。 'crop_class.py'不能訪問'crops.py'。菜單可能是* crop * specific,所以'display_menu'爲什麼在'crop_class.py'文件中 – Milk

+0

是的,我認爲在'crop_class.py'中運行'manage_crop'會導致該模塊中的函數被使用。 –

相關問題