You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type conversions are automatically performed for numeric, boolean,
110
113
string, IO stream, date/period, and function types, along with tuples,
@@ -122,10 +125,13 @@ Keyword arguments can also be passed. For example, matplotlib's
122
125
[pyplot](https://matplotlib.org/) uses keyword arguments to specify plot
123
126
options, and this functionality is accessed from Julia by:
124
127
125
-
plt = pyimport("matplotlib.pyplot")
126
-
x = range(0;stop=2*pi,length=1000); y = sin.(3*x + 4*cos.(2*x));
127
-
plt.plot(x, y, color="red", linewidth=2.0, linestyle="--")
128
-
plt.show()
128
+
```julia
129
+
plt =pyimport("matplotlib.pyplot")
130
+
x =range(0;stop=2*pi,length=1000)
131
+
y =sin.(3*x +4*cos.(2*x))
132
+
plt.plot(x, y, color="red", linewidth=2.0, linestyle="--")
133
+
plt.show()
134
+
```
129
135
130
136
However, for better integration with Julia graphics backends and to
131
137
avoid the need for the `show` function, we recommend using matplotlib
@@ -135,14 +141,18 @@ Arbitrary Julia functions can be passed to Python routines taking
135
141
function arguments. For example, to find the root of cos(x) - x,
136
142
we could call the Newton solver in scipy.optimize via:
137
143
138
-
so = pyimport("scipy.optimize")
139
-
so.newton(x -> cos(x) - x, 1)
144
+
```julia
145
+
so =pyimport("scipy.optimize")
146
+
so.newton(x ->cos(x) - x, 1)
147
+
```
140
148
141
149
A macro exists for mimicking Python's "with statement". For example:
142
150
143
-
@pywith pybuiltin("open")("file.txt","w") as f begin
144
-
f.write("hello")
145
-
end
151
+
```julia
152
+
@pywithpybuiltin("open")("file.txt","w") as f begin
153
+
f.write("hello")
154
+
end
155
+
```
146
156
147
157
The type of `f` can be specified with `f::T` (for example, to override automatic
148
158
conversion, use `f::PyObject`). Similarly, if the context manager returns a type
@@ -430,66 +440,76 @@ documentation `?pyfunction` and `?pyfunctionret` for more information.
430
440
`@pydef` creates a Python class whose methods are implemented in Julia.
431
441
For instance,
432
442
433
-
P = pyimport("numpy.polynomial")
434
-
@pydef mutable struct Doubler <: P.Polynomial
435
-
function __init__(self, x=10)
436
-
self.x = x
437
-
end
438
-
my_method(self, arg1::Number) = arg1 + 20
439
-
x2.get(self) = self.x * 2
440
-
function x2.set!(self, new_val)
441
-
self.x = new_val / 2
442
-
end
443
+
```julia
444
+
P =pyimport("numpy.polynomial")
445
+
@pydefmutable struct Doubler <:P.Polynomial
446
+
function__init__(self, x=10)
447
+
self.x = x
448
+
end
449
+
my_method(self, arg1::Number) =arg1 +20
450
+
x2.get(self) = self.x *2
451
+
function x2.set!(self, new_val)
452
+
self.x = new_val /2
443
453
end
444
-
Doubler().x2
454
+
end
455
+
Doubler().x2
456
+
```
445
457
446
458
is essentially equivalent to the following Python code:
447
459
448
-
import numpy.polynomial
449
-
class Doubler(numpy.polynomial.Polynomial):
450
-
def __init__(self, x=10):
451
-
self.x = x
452
-
def my_method(self, arg1): return arg1 + 20
453
-
@property
454
-
def x2(self): return self.x * 2
455
-
@x2.setter
456
-
def x2(self, new_val):
457
-
self.x = new_val / 2
458
-
Doubler().x2
460
+
```python
461
+
import numpy.polynomial
462
+
classDoubler(numpy.polynomial.Polynomial):
463
+
def__init__(self, x=10):
464
+
self.x = x
465
+
defmy_method(self, arg1): return arg1 +20
466
+
@property
467
+
defx2(self): returnself.x *2
468
+
@x2.setter
469
+
defx2(self, new_val):
470
+
self.x = new_val /2
471
+
Doubler().x2
472
+
```
459
473
460
474
The method arguments and return values are automatically converted between Julia and Python. All Python special methods are supported (`__len__`, `__add__`, etc.).
461
475
462
476
`@pydef` allows for multiple inheritance of Python classes:
0 commit comments