0

I have two functions

read_operator_rate_file(filename = 'test_scenarios.csv') &
get_input(phone_number_to_dial = input("enter phone number>> "))

In main I am calling them in order and the first function checks the condition for the CSV and may exit if there's any error. But, now when I put input function as the parameter for get_input function I am reading the prompt before the first function is being executed.

the code sample:

import csv

def read_operator_rate_file(filename = 'test_scenarios.csv'):
   
    try:
    with open(filename, newline='') as f:
        # read each row as a list and store it in a list
        operator_data = list(csv.reader(f))
        operator_data.sort(key = lambda x:int(x[0]))
        return operator_data
except FileNotFoundError as errorcode:
    print(errorcode)
except IOError:
    print("Could not read file:"), filename


def get_input(phone_number_to_dial = input("enter phone number>> ")):
   
    try:
       
        assert (phone_number_to_dial.startswith('+') and     phone_number_to_dial[
                                                         1:].isdigit())     or phone_number_to_dial[
                                                                           :].isdigit(), 'Invalid phone number'
        assert len(phone_number_to_dial) > 2, 'Phone number too short'
        # had this at 9 but changed it to 2 for calling 112
        assert len(phone_number_to_dial) < 16, 'Phone number too long'
    except Exception as e:
        print(e)
        exit()
    else:
        return (phone_number_to_dial)

if __name__ == '__main__':
    operator_list = read_operator_rate_file()
    get_input()
Sabby
  • 3
  • 3
  • 4
    Please show a [mcve] of the code. `main` isn't at all special though, so if you're calling `input` before you call `main`, then `input` will run first. – Carcigenicate Apr 08 '21 at 14:02
  • When I run this part the prompt "Enter phone number" appears first then the functions are being called in the order as called in main. I don't know why. – Sabby Apr 08 '21 at 14:17
  • Arguments are evaluated with the function head (the ``def get_input``). When *else* would you expect ``input`` to be called? – MisterMiyagi Apr 08 '21 at 14:19
  • 1
    Does this answer your question? [“Least Astonishment” and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – MisterMiyagi Apr 08 '21 at 14:19
  • @MisterMiyagi This default argument isn't mutable – wjandrea Apr 08 '21 at 14:21
  • 1
    @wjandrea It's not, but the Q&A has extensive information on when (and why) default values are evaluated. FWIW, I can dupe-hammer – my comment does indeed *ask* whether it answers the question. – MisterMiyagi Apr 08 '21 at 14:23
  • @MisterMiyagi What i was trying to do is to run the read_operator_rate_file function first, check if the file is loaded without error and then proceed to read user input. But now the prompt from the get_input function appears and then the code is checking for file existence. – Sabby Apr 08 '21 at 14:26
  • Does this answer your question? [Python function default argument random value](https://stackoverflow.com/questions/62412902/python-function-default-argument-random-value) – wjandrea Apr 08 '21 at 14:26
  • @MisterMiyagi There, that's a better duplicate target – wjandrea Apr 08 '21 at 14:26

1 Answers1

0

I'm not sure why exactly it happens like that, but I would imagine that the default argument is evaluated when the function is defined, which is before you call your code.

Rather than

def get_input(phone_number_to_dial = input("enter phone number>> ")):

I would suggest that you use something like:

def get_input(phone_number_to_dial=None):
    if phone_number_to_dial is None:
        phone_number_to_dial = input("enter phone number>> ")
Rory Browne
  • 627
  • 1
  • 5
  • 11
  • Actually - it seems like everything in this answer is more clearly explained in the link shared by @wjandrea in a comment. I'm not sure what the correct idiomatic thing is to do about that. – Rory Browne Apr 08 '21 at 15:09