Skip to content

Commit 5bb084d

Browse files
author
Christopher Doris
committed
update docs
1 parent 2adad5c commit 5bb084d

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

docs/src/pythoncall.md

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ Now you can interact with Python as follows:
2424

2525
```julia-repl
2626
julia> re = pyimport("re")
27-
Python module: <module 're' from '[...]/lib/re.py'>
27+
Python: <module 're' from '[...]/lib/re.py'>
2828
2929
julia> words = re.findall("[a-zA-Z]+", "PythonCall.jl is very useful!")
30-
Python list: ['PythonCall', 'jl', 'is', 'very', 'useful']
30+
Python: ['PythonCall', 'jl', 'is', 'very', 'useful']
3131
3232
julia> sentence = Py(" ").join(words)
33-
Python str: 'PythonCall jl is very useful'
33+
Python: 'PythonCall jl is very useful'
3434
3535
julia> pyconvert(String, sentence)
3636
"PythonCall jl is very useful"
@@ -52,13 +52,13 @@ comparison and arithmetic:
5252

5353
```julia-repl
5454
julia> x = pylist([3, 4, 5])
55-
Python list: [3, 4, 5]
55+
Python: [3, 4, 5]
5656
5757
julia> x[2] == 5
58-
Python bool: True
58+
Python: True
5959
6060
julia> x[pyslice(0,2)] + pylist([1,2])
61-
Python list: [3, 4, 1, 2]
61+
Python: [3, 4, 1, 2]
6262
```
6363

6464
We have just seen the functions [`pylist`](@ref) (for constructing a Python list) and
@@ -79,13 +79,13 @@ Notable exceptions are:
7979
To access the Python builtins directly, you can access the fields of [`pybuiltins`](@ref):
8080
```julia-repl
8181
julia> pybuiltins.None
82-
Python None
82+
Python: None
8383
8484
julia> pybuiltins.True
85-
Python bool: True
85+
Python: True
8686
8787
julia> pybuiltins.ValueError("some error")
88-
Python ValueError: ValueError('some error')
88+
Python: ValueError('some error')
8989
```
9090

9191
With the functions introduced so far, you have access to the vast majority of Python's
@@ -103,7 +103,7 @@ converted to another Julia type. Instead, you can explicitly convert using
103103

104104
```julia-repl
105105
julia> x = pylist([3.4, 5.6])
106-
Python list: [3.4, 5.6]
106+
Python: [3.4, 5.6]
107107
108108
julia> pyconvert(Vector, x)
109109
2-element Vector{Float64}:
@@ -116,7 +116,7 @@ julia> pyconvert(Vector{Float32}, x)
116116
5.6
117117
118118
julia> pyconvert(Any, x)
119-
2-element PyList{Py}:
119+
2-element PyList{Any}:
120120
3.4
121121
5.6
122122
```
@@ -151,7 +151,7 @@ a Julia vector:
151151

152152
```julia-repl
153153
julia> x = pylist([3,4,5])
154-
Python list: [3, 4, 5]
154+
Python: [3, 4, 5]
155155
156156
julia> y = PyList{Union{Int,Nothing}}(x)
157157
3-element PyList{Union{Nothing, Int64}}:
@@ -176,7 +176,7 @@ julia> append!(y, 1:2)
176176
2
177177
178178
julia> x
179-
Python list: [3, 4, 5, None, 1, 2]
179+
Python: [3, 4, 5, None, 1, 2]
180180
```
181181

182182
There are wrappers for other container types, such as [`PyDict`](@ref) and [`PySet`](@ref).
@@ -187,7 +187,7 @@ like `bytes`, `bytearray`, `array.array` and `numpy.ndarray`:
187187

188188
```julia-repl
189189
julia> x = pyimport("array").array("i", [3, 4, 5])
190-
Python array: array('i', [3, 4, 5])
190+
Python: array('i', [3, 4, 5])
191191
192192
julia> y = PyArray(x)
193193
3-element PyArray{Int32, 1, true, true, Int32}:
@@ -202,7 +202,7 @@ julia> y[1] = 0
202202
0
203203
204204
julia> x
205-
Python array: array('i', [0, 4, 5])
205+
Python: array('i', [0, 4, 5])
206206
```
207207

208208
It directly wraps the underlying data buffer, so array operations such as indexing are about
@@ -212,7 +212,7 @@ The [`PyIO`](@ref) wrapper type views a Python file object as a Julia IO object:
212212

213213
```julia-repl
214214
julia> x = pyimport("io").StringIO()
215-
Python StringIO: <_io.StringIO object at 0x000000006579BC70>
215+
Python: <_io.StringIO object at 0x000000006579BC70>
216216
217217
julia> y = PyIO(x)
218218
PyIO(<py _io.StringIO object at 0x000000006579BC70>, false, true, false, 4096, UInt8[], 4096, UInt8[])
@@ -222,10 +222,10 @@ julia> println(y, "Hello, world!")
222222
julia> flush(y)
223223
224224
julia> x.seek(0)
225-
Python int: 0
225+
Python: 0
226226
227227
julia> x.read()
228-
Python str: 'Hello, world!\n'
228+
Python: 'Hello, world!\n'
229229
```
230230

231231
## [Configuration](@id pythoncall-config)
@@ -317,23 +317,44 @@ example package which wraps the Python FAISS package.
317317

318318
### Precompilation
319319

320-
You may not interact with Python during module precompilation. Therefore, instead of
320+
You must not interact with Python during module precompilation. Therefore, instead of
321321
```julia
322322
module MyModule
323323
using PythonCall
324324
const foo = pyimport("foo")
325325
bar() = foo.bar() # will crash when called
326326
end
327327
```
328-
you must do
328+
you can do the import when the module is loaded, saving the result in a `Ref`
329329
```julia
330330
module MyModule
331331
using PythonCall
332-
const foo = PythonCall.pynew() # initially NULL
332+
const foo = Ref{Py}()
333333
function __init__()
334-
PythonCall.pycopy!(foo, pyimport("foo"))
334+
foo[] = pyimport("foo")
335335
end
336-
bar() = foo.bar() # now ok
336+
bar() = foo[].bar()
337+
end
338+
```
339+
or you can perform any imports dynamically
340+
```julia
341+
module MyModule
342+
using PythonCall
343+
bar() = pyimport("foo").bar()
344+
end
345+
```
346+
or if that is too slow, you can cache the import
347+
```julia
348+
module MyModule
349+
using PythonCall
350+
bar() = @pyconst(pyimport("foo")).bar()
351+
end
352+
```
353+
or even cache the imported function
354+
```julia
355+
module MyModule
356+
using PythonCall
357+
bar() = @pyconst(pyimport("foo").bar)()
337358
end
338359
```
339360

0 commit comments

Comments
 (0)