zingy
zingy

Reputation: 811

confusion over sequence of function calling

I have a silly confusion here. I have the following main() in my code and I pass transcriptionFile and phonemeFile as inputs. Is it the time() function that is called first and then the coart() function? But I input the files together that are passed in these functions in a single line, so don't they have to be called at the same time?

I am just a beginner in python. I was working on the time() function and coart() function separately but now I have to pass a list generated in coart() function to time() function. So, if time() function is called first then it wouldn't recognize the list even if I pass it. I hope I made my question clear. Thank you.

if __name__ == "__main__" :

if len(sys.argv) != 3 :   
    Usage()

else :
    transcriptionFile = sys.argv[1]

    time("transcriptions.txt")

    phonemeFile = sys.argv[2]

    coart("syllabifiedPhonemes.txt")

Upvotes: 0

Views: 101

Answers (3)

mctylr
mctylr

Reputation: 5169

Python like many (most) programming languages execute commands in the order that follows the program's control flow (decision if/then/else, loops while, for, foreach) with only a single task being executed at any point in the program. This both for simplicity of programming language design and implementation, as well as historically a limitation due to hardware design and performance. So it is an artifact of the development of computer hardware implementations, and software (languages).

Until only recently, the majority of computers had a single CPU, which can only execute a single task at a given time. Several techniques were utilized to simulate the co-handling of multiple tasks (processes, threads, user interfaces) by techniques known as multi-tasking (co-operating or preemptive), multi-user (aka "time-sharing"), multi-threaded (with threads being more lightweight than processes as viewed by the Operating System) by a single processor by dividing the CPU's usage in varying manners to give the appearance of multiple tasks running simultaneously, but technically they are merely stopping and starting so fast that the (human) user cannot detect the pauses. You can think of it as being similar to the viewing of multiple image frames in a cinema's film, when shown at "normal" speed, the human viewer sees it as continuous motion.

Now there are techniques to execute multiple tasks "simultaneously" (whether only appearing to be simultaneous as with a single CPU and a multi-tasking operating system, or truly simultaneously being executed by multiple CPUs or CPU cores.

So my default your Python program will execute instructions in sequence, unless you use a number of more advanced techniques to tell the computer when to try to execute functions simultaneously. The language and the computer will not make a sequential program execute portions in parallel, without input by the programmer.

(AFAIK no research regarding automated detection of parallelizable code has ever reached general success)

Upvotes: 1

Vaughn Cato
Vaughn Cato

Reputation: 64308

Statements are always executed in the order listed. So in your case, sys.argv[1] will first be assigned to the global variable transcriptionFile. Then, the time function will be called. Then the value of sys.argv[2] will be assigned to the phonemeFile variable, and finally the coart function will be called. If the time function uses something that the coart function produces, then you will need to call the coart function first. It doesn't matter than your command line parameters in sys.argv occur in a different order.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363507

Is it the time() function that is called first and then the coart() function?

Yes. Statements are in the order they are listed in the file, unless interrupted by control flow constructs such as if-else, while, raise, etc.

But I input the files together that are passed in these functions in a single line, so don't they have to be called at the same time?

In the common Python interpreter CPython, you can't execute two functions at the same time in any easy way. You have to make a choice. If the functions are completely independent, then both choices are valid.

Upvotes: 1

Related Questions