Define Python function in Julia file with PythonCall?

Whenever you use pyexec you’re invoking the Python parser, which makes for an unfair comparison. It’s better to use it to create a Python function then call that:

@pyexec """
def fib(n):
   a, b = 0, 1
   while a < n:
       print(a, end=' ')
       a, b = b, a+b
   print()
""" => fib

fib(10)

This example uses @pyexec’s handy => notation for getting variables into and out of Python.

9 Likes