2017-08-07 162 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'会导致该模块中的函数被使用。 –

相关问题