2017-08-07 31 views
1

私の質問は、2つのモジュールから同じ名前の関数が2つあるときにコンピュータがどのように関数を実行するかを選択することです。モジュールの1つがインポートされます。同じ関数名を持つ2つのファイルPython

これはPythonschoolの例です。

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は、クラスと呼ばれる作物の子である:

は私がcrops.pyと呼ばれるファイルを持っています。 作物クラスは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のmain関数を実行すると、crop_pyのdisplay_menu()は new_crop = create_crop() で実行され、crop_class.pyのdisplay_menu()は manage_crop(new_crop)で実行されます。

両方の機能が特定のクラスに起因していないため、私は混乱しました。 crop_class.pyのdisplay_menu()は、Cropクラスの一部ではない方法で字下げされています。 したがって、コンピュータが実行するコードを選択する方法については混乱しています。このルールの実行が本当に役立つでしょう。

答えて

0

import*を使用するPythonモジュールでは、モジュール内のすべてのクラスとメソッドにアクセスできます。 manage_cropcreate_cropについて

crops.pyファイルは、それが(wheat_class及び/又はpotato_class介して間接的に)インポートされたとしてcrop_class.pyからメソッドにアクセスすることができます。

希望に役立ちます!

詳しくはimportとのドキュメントをご覧ください。

+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 *のようなものかもしれません。なぜ 'display_menu'が' crop_class.py'ファイルにあるのですか? – Milk

+0

はい、私は 'manage_crop'を' crop_class.py'で実行すると、そのモジュールの機能が使用されると思います。 –

関連する問題