Skip to content

Commit b44718c

Browse files
committed
updated with input/output syntax
1 parent 4bf5fdf commit b44718c

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

docs/src/pythoncall.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,40 +97,45 @@ A common use case is calling multiple blocks of Python code from Julia interacti
9797

9898
```julia-repl
9999
julia> @pyexec """
100-
global re
101-
import re
102-
103-
def my_sentence(s):
104-
words = re.findall("[a-zA-Z]+", s)
105-
sentence = " ".join(words)
106-
return sentence
107-
""" => my_sentence
100+
global re
101+
import re
102+
103+
def my_sentence(s):
104+
words = re.findall("[a-zA-Z]+", s)
105+
sentence = " ".join(words)
106+
return sentence
107+
""" => my_sentence
108108
Python: <function my_sentence at 0x7d83bb1b01a0>
109109
110110
julia> sentence = my_sentence("PythonCall.jl is very useful!")
111111
Python: 'PythonCall jl is very useful'
112112
```
113113

114-
Note the use of the `global` keyword to make the `re` package accessible in global scope, and the `=> my_sentence` syntax to create a Julia function named `my_sentence` that calls to the Python function of the same name. This syntax also supports calling to multiple functions:
114+
Note the use of the `global` keyword to make the `re` package accessible in global scope, and the `=> my_sentence` syntax to create a Julia function named `my_sentence` that calls to the Python function of the same name. This syntax also supports calling to multiple functions and passing data back-and-forth:
115115

116116
```julia-repl
117-
julia> @pyexec """
118-
def add(a, b):
119-
return a + b
117+
julia> @pyexec (num=10) => """
118+
def add(a, b):
119+
return a + b
120+
121+
def subtract(a, b):
122+
return a - b
120123
121-
def subtract(a, b):
122-
return a - b
123-
""" => (add, subtract)
124-
(add = <py function add at 0x7e6cbe398d50>, subtract = <py function subtract at 0x7e6cbdfa7b60>)
124+
plusone = num + 1
125+
""" => (add, subtract, plusone::Float64)
126+
(add = <py function add at 0x721b001a7cc0>, subtract = <py function subtract at 0x721b001a7d70>, plusone = 11.0)
125127
126128
julia> add(4, 3)
127129
Python: 7
128130
129131
julia> subtract(4, 3)
130132
Python: 1
133+
134+
julia> plusone
135+
11.0
131136
```
132137

133-
See [@pyexec](@ref), [@pyeval](@ref), and their functional forms [pyexec](@ref) and [pyeval](@ref), for more.
138+
Here we demonstrate passing a named variable, `num`, via the use of the `=>` syntax again, and returning named output, with the last element, `plusone`, being cast to a Julia object via the `::` syntax. See [@pyexec](@ref), [@pyeval](@ref), and their functional forms [pyexec](@ref) and [pyeval](@ref), for more.
134139

135140
## Conversion between Julia and Python
136141

0 commit comments

Comments
 (0)