2

I couldn't find any question related to this subject. But does python execute a function after the previous called function is finished or is there in any way parallel execution?

For example:

def a():
    print('a')

def b():
    print('b')

a()
b()

So in this example I would like to know if I can always be sure that function b is called after function a is finished, even if function a is a very long script? And what is the defenition of this, so I can look up documentation regarding this matter.

Thanks!!

4
  • 2
    yes, python is an interpreted language, it works with a repl - read, evaluate, print, loop, means, as long as you are not using a multi-threaded code a() will happen before b() Commented Sep 9, 2019 at 13:04
  • stackoverflow.com/a/45025059/9153298 Commented Sep 9, 2019 at 13:05
  • 2
    There is no by default parallel execution language. At least among popular one. Program executes instructions one by one in defined order. And you are running them in one thread. Even if you create a separate thread in a() for caller thread it still sequential operation and a() return first and then b() being executed. Commented Sep 9, 2019 at 13:08
  • depends on complexity of how many functions needs to be called in order/sequence you may create a hierarchical state machine Commented Sep 9, 2019 at 13:10

4 Answers 4

4

TLDR: b will only ever run after a is exited.

Each Python thread will only ever execute one thing at a time and respect ordering of expressions and statements. For the most part, this means executing "top-to-bottom", though function definitions, control flow and other elements can affect execution order. Ordering is preserved in any case, however.


Strictly speaking, the Python language only defines the execution order of expressions.

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

Neither simple statements nor compound statements define an evaluation order.

However, Python is defined based on a byte code interpreting virtual machine, and the reference implementation is based on a stackbased bytecode evaluation loop. All major implementations of Python preserve the observable behaviour of executing one statement after the other.

Sign up to request clarification or add additional context in comments.

Comments

2

Defining the function doesn't mean its execution. Since you defined a first, the function object for a will be created first, so as for there calls.

You can take it as execution timeline starting from top to bottom.

Comments

1

In python functions are by default executed in the order they appear. However if you call them in a different order they will execute as such. So in your example

def a():
    print('a')
def b():
    print('b')

b()
a()

then b() will execute before a()

Comments

0

There is no parallel execution of functions in python. The above functions will be executed in the same sequence that they were called in regardless of the amount of computation workload of either of the functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.