Что вызывает «Unbound Local Error» в Python? - PullRequest
0 голосов
/ 10 ноября 2019

У меня есть следующий фрагмент кода, который дает мне Unbound Local Error. Я хотел бы знать, что является причиной Unbound Local Error и как это исправить?

def main():
    # TODO 0: Measures total program runtime by collecting start time
    start_time = time()
    # creates and retrieves command Line Arguments
    in_arg = get_input_args()
    # Function that checks command line arguments using in_arg
    check_command_line_arguments(in_arg)
    # Creates pet image labels by creating a dictionary
    answers_dic = pet_labels()
    check_creating_pet_image_labels(answers_dic)
    # Creates classifier labels with classifier function, compares labbels and createsa results 
    # dictionary
    result_dic = classify_images(in_arg.dir, answers_dic, in_arg.arch)
    # Function that checks results dictionary result_dic
    check_classifying_images(result_dic)
    # Adjusts the results dictionary to determine if classifier correctly classified images 'a dog'
    # or 'not a dog'
    adjust_results4_isadog(result_dic,in_arg.dogfile)
    # Function that checks results dictionary for is-a -dog adjustment - result-dic
    check_classifying_labels_as_dogs(result_dic)
    # Calculates results of run and puts statistics in results_stats_dic
    results_stats_dic = calculates_results_stats(result_dic)
    # Function that checks results stats dictionary - results_stats_dic
    check_calculating_results(result_dic,results_stats_dic)
    # Prints Summary results, incorrect classifications of dogs and breeds if requested
    prints_results(result_dic, results_stats_dic, in_arg.arch)
    # Measure total program runtime by collecting end time
    end_time = time()
    # Computes overall runtime in seconds and prints it hh:mm:ss format
    tot_time = end_time - start_time
    print('\n** Total elapsed runtime:', str(int((tot_time / 3600))) + ':' + str(int((tot_time % 3600) / 60))
         + ':' +str(int((tot_time % 3600) % 60)))

Выдает следующую ошибку:

   Traceback (most recent call last):
File 'check_images.py' , line 417, in main
in_arg = get_input_args()
Unboundlocal error:Local variable 'get_input_args' referenced before assignment

Любая помощь будет принята с благодарностью.

1 Ответ

0 голосов
/ 10 ноября 2019

Где определена функция get_input_args()?

Скорее всего, он не определен в том же файле, и вы должны определить такую ​​функцию.

Чтобы понять эту ошибку, вы можете обратиться - Python FAQ - https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value - сообщение в блоге - https://eli.thegreenplace.net/2011/05/15/understanding-unboundlocalerror-in-python

Если вам нужно реализовать такую ​​функцию, я бы порекомендовал вам иметьПосмотрите на модуль argparse. https://docs.python.org/3/library/argparse.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...