-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathstruct.PyArray.html
1062 lines (972 loc) · 206 KB
/
struct.PyArray.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A safe, statically-typed wrapper for NumPy’s `ndarray` class."><title>PyArray in numpy::array - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="numpy" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../numpy/index.html">numpy</a><span class="version">0.20.0</span></h2></div><h2 class="location"><a href="#">PyArray</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.arange">arange</a></li><li><a href="#method.as_array">as_array</a></li><li><a href="#method.as_array_mut">as_array_mut</a></li><li><a href="#method.as_raw_array">as_raw_array</a></li><li><a href="#method.as_raw_array_mut">as_raw_array_mut</a></li><li><a href="#method.as_slice">as_slice</a></li><li><a href="#method.as_slice_mut">as_slice_mut</a></li><li><a href="#method.as_untyped">as_untyped</a></li><li><a href="#method.borrow_from_array">borrow_from_array</a></li><li><a href="#method.cast">cast</a></li><li><a href="#method.copy_to">copy_to</a></li><li><a href="#method.data">data</a></li><li><a href="#method.dims">dims</a></li><li><a href="#method.from_array">from_array</a></li><li><a href="#method.from_borrowed_ptr">from_borrowed_ptr</a></li><li><a href="#method.from_iter">from_iter</a></li><li><a href="#method.from_owned_array">from_owned_array</a></li><li><a href="#method.from_owned_object_array">from_owned_object_array</a></li><li><a href="#method.from_owned_ptr">from_owned_ptr</a></li><li><a href="#method.from_slice">from_slice</a></li><li><a href="#method.from_vec">from_vec</a></li><li><a href="#method.from_vec2">from_vec2</a></li><li><a href="#method.from_vec3">from_vec3</a></li><li><a href="#method.get">get</a></li><li><a href="#method.get_mut">get_mut</a></li><li><a href="#method.get_owned">get_owned</a></li><li><a href="#method.item">item</a></li><li><a href="#method.new">new</a></li><li><a href="#method.readonly">readonly</a></li><li><a href="#method.readwrite">readwrite</a></li><li><a href="#method.reshape">reshape</a></li><li><a href="#method.reshape_with_order">reshape_with_order</a></li><li><a href="#method.resize">resize</a></li><li><a href="#method.to_dyn">to_dyn</a></li><li><a href="#method.to_owned">to_owned</a></li><li><a href="#method.to_owned_array">to_owned_array</a></li><li><a href="#method.to_vec">to_vec</a></li><li><a href="#method.try_as_matrix">try_as_matrix</a></li><li><a href="#method.try_as_matrix_mut">try_as_matrix_mut</a></li><li><a href="#method.try_readonly">try_readonly</a></li><li><a href="#method.try_readwrite">try_readwrite</a></li><li><a href="#method.uget">uget</a></li><li><a href="#method.uget_mut">uget_mut</a></li><li><a href="#method.uget_raw">uget_raw</a></li><li><a href="#method.zeros">zeros</a></li></ul><h3><a href="#deref-methods-PyUntypedArray">Methods from Deref<Target=PyUntypedArray></a></h3><ul class="block deref-methods"><li><a href="#method.as_array_ptr">as_array_ptr</a></li><li><a href="#method.dtype">dtype</a></li><li><a href="#method.is_c_contiguous">is_c_contiguous</a></li><li><a href="#method.is_contiguous">is_contiguous</a></li><li><a href="#method.is_empty">is_empty</a></li><li><a href="#method.is_fortran_contiguous">is_fortran_contiguous</a></li><li><a href="#method.len">len</a></li><li><a href="#method.ndim">ndim</a></li><li><a href="#method.shape">shape</a></li><li><a href="#method.strides">strides</a></li></ul><h3><a href="#deref-methods-PyAny">Methods from Deref<Target=PyAny></a></h3><ul class="block deref-methods"><li><a href="#method.as_ptr">as_ptr</a></li><li><a href="#method.call">call</a></li><li><a href="#method.call0">call0</a></li><li><a href="#method.call1">call1</a></li><li><a href="#method.call_method">call_method</a></li><li><a href="#method.call_method0">call_method0</a></li><li><a href="#method.call_method1">call_method1</a></li><li><a href="#method.compare">compare</a></li><li><a href="#method.contains">contains</a></li><li><a href="#method.del_item">del_item</a></li><li><a href="#method.delattr">delattr</a></li><li><a href="#method.dir">dir</a></li><li><a href="#method.downcast">downcast</a></li><li><a href="#method.downcast_exact">downcast_exact</a></li><li><a href="#method.downcast_unchecked">downcast_unchecked</a></li><li><a href="#method.eq">eq</a></li><li><a href="#method.extract">extract</a></li><li><a href="#method.ge">ge</a></li><li><a href="#method.get_item">get_item</a></li><li><a href="#method.get_refcnt">get_refcnt</a></li><li><a href="#method.get_type">get_type</a></li><li><a href="#method.get_type_ptr">get_type_ptr</a></li><li><a href="#method.getattr">getattr</a></li><li><a href="#method.gt">gt</a></li><li><a href="#method.hasattr">hasattr</a></li><li><a href="#method.hash">hash</a></li><li><a href="#method.into_ptr">into_ptr</a></li><li><a href="#method.is">is</a></li><li><a href="#method.is_callable">is_callable</a></li><li><a href="#method.is_ellipsis">is_ellipsis</a></li><li><a href="#method.is_empty-1">is_empty</a></li><li><a href="#method.is_exact_instance">is_exact_instance</a></li><li><a href="#method.is_exact_instance_of">is_exact_instance_of</a></li><li><a href="#method.is_instance">is_instance</a></li><li><a href="#method.is_instance_of">is_instance_of</a></li><li><a href="#method.is_none">is_none</a></li><li><a href="#method.is_true">is_true</a></li><li><a href="#method.is_truthy">is_truthy</a></li><li><a href="#method.iter">iter</a></li><li><a href="#method.le">le</a></li><li><a href="#method.len-1">len</a></li><li><a href="#method.lt">lt</a></li><li><a href="#method.ne">ne</a></li><li><a href="#method.py">py</a></li><li><a href="#method.py_super">py_super</a></li><li><a href="#method.repr">repr</a></li><li><a href="#method.rich_compare">rich_compare</a></li><li><a href="#method.set_item">set_item</a></li><li><a href="#method.setattr">setattr</a></li><li><a href="#method.str">str</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block trait-implementation"><li><a href="#impl-AsPyPointer-for-PyArray%3CT,+D%3E">AsPyPointer</a></li><li><a href="#impl-AsRef%3CPyAny%3E-for-PyArray%3CT,+D%3E">AsRef<PyAny></a></li><li><a href="#impl-Debug-for-PyArray%3CT,+D%3E">Debug</a></li><li><a href="#impl-Deref-for-PyArray%3CT,+D%3E">Deref</a></li><li><a href="#impl-DerefToPyAny-for-PyArray%3CT,+D%3E">DerefToPyAny</a></li><li><a href="#impl-Display-for-PyArray%3CT,+D%3E">Display</a></li><li><a href="#impl-From%3C%26PyArray%3CT,+D%3E%3E-for-%26PyAny">From<&'a PyArray<T, D>></a></li><li><a href="#impl-From%3C%26PyArray%3CT,+D%3E%3E-for-Py%3CPyArray%3CT,+D%3E%3E">From<&PyArray<T, D>></a></li><li><a href="#impl-FromPyObject%3C'py%3E-for-%26PyArray%3CT,+D%3E">FromPyObject<'py></a></li><li><a href="#impl-IntoPy%3CPy%3CPyAny%3E%3E-for-PyArray%3CT,+D%3E">IntoPy<Py<PyAny>></a></li><li><a href="#impl-IntoPy%3CPy%3CPyArray%3CT,+D%3E%3E%3E-for-%26PyArray%3CT,+D%3E">IntoPy<Py<PyArray<T, D>>></a></li><li><a href="#impl-PyNativeType-for-PyArray%3CT,+D%3E">PyNativeType</a></li><li><a href="#impl-PyTypeInfo-for-PyArray%3CT,+D%3E">PyTypeInfo</a></li><li><a href="#impl-ToPyObject-for-PyArray%3CT,+D%3E">ToPyObject</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-RefUnwindSafe-for-PyArray%3CT,+D%3E">!RefUnwindSafe</a></li><li><a href="#impl-Send-for-PyArray%3CT,+D%3E">!Send</a></li><li><a href="#impl-Sync-for-PyArray%3CT,+D%3E">!Sync</a></li><li><a href="#impl-Unpin-for-PyArray%3CT,+D%3E">Unpin</a></li><li><a href="#impl-UnwindSafe-for-PyArray%3CT,+D%3E">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block blanket-implementation"><li><a href="#impl-Any-for-T">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-T">From<T></a></li><li><a href="#impl-FromPyPointer%3C'p%3E-for-T">FromPyPointer<'p></a></li><li><a href="#impl-HasPyGilRef-for-T">HasPyGilRef</a></li><li><a href="#impl-Into%3CU%3E-for-T">Into<U></a></li><li><a href="#impl-PyTryFrom%3C'v%3E-for-T">PyTryFrom<'v></a></li><li><a href="#impl-PyTypeCheck-for-T">PyTypeCheck</a></li><li><a href="#impl-Same-for-T">Same</a></li><li><a href="#impl-SupersetOf%3CSS%3E-for-SP">SupersetOf<SS></a></li><li><a href="#impl-ToString-for-T">ToString</a></li><li><a href="#impl-TryFrom%3CU%3E-for-T">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-T">TryInto<U></a></li></ul></section><h2><a href="index.html">In numpy::array</a></h2></div></nav><div class="sidebar-resizer"></div>
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../../numpy/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Struct <a href="../index.html">numpy</a>::<wbr><a href="index.html">array</a>::<wbr><a class="struct" href="#">PyArray</a><button id="copy-path" title="Copy item path to clipboard"><img src="../../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../../src/numpy/array.rs.html#102">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><pre class="rust item-decl"><code>pub struct PyArray<T, D>(<span class="comment">/* private fields */</span>);</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A safe, statically-typed wrapper for NumPy’s <a href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html"><code>ndarray</code></a> class.</p>
<h2 id="memory-location"><a href="#memory-location">Memory location</a></h2>
<ul>
<li>Allocated by Rust: Constructed via <a href="../convert/trait.IntoPyArray.html" title="trait numpy::convert::IntoPyArray"><code>IntoPyArray</code></a> or
<a href="struct.PyArray.html#method.from_vec" title="associated function numpy::array::PyArray::from_vec"><code>from_vec</code></a> or <a href="struct.PyArray.html#method.from_owned_array" title="associated function numpy::array::PyArray::from_owned_array"><code>from_owned_array</code></a>.</li>
</ul>
<p>These methods transfers ownership of the Rust allocation into a suitable Python object
and uses the memory as the internal buffer backing the NumPy array.</p>
<p>Please note that some destructive methods like <a href="struct.PyArray.html#method.resize" title="method numpy::array::PyArray::resize"><code>resize</code></a> will fail
when used with this kind of array as NumPy cannot reallocate the internal buffer.</p>
<ul>
<li>Allocated by NumPy: Constructed via other methods, like <a href="../convert/trait.ToPyArray.html" title="trait numpy::convert::ToPyArray"><code>ToPyArray</code></a> or
<a href="struct.PyArray.html#method.from_slice" title="associated function numpy::array::PyArray::from_slice"><code>from_slice</code></a> or <a href="struct.PyArray.html#method.from_array" title="associated function numpy::array::PyArray::from_array"><code>from_array</code></a>.</li>
</ul>
<p>These methods allocate memory in Python’s private heap via NumPy’s API.</p>
<p>In both cases, <code>PyArray</code> is managed by Python so it can neither be moved from
nor deallocated manually.</p>
<h2 id="references"><a href="#references">References</a></h2>
<p>Like <a href="struct.PyArray.html#method.new" title="associated function numpy::array::PyArray::new"><code>new</code></a>, all constructor methods of <code>PyArray</code> return a shared reference <code>&PyArray</code>
instead of an owned value. This design follows <a href="https://pyo3.rs/main/memory.html">PyO3’s ownership concept</a>,
i.e. the return value is GIL-bound owning reference into Python’s heap.</p>
<h2 id="element-type-and-dimensionality"><a href="#element-type-and-dimensionality">Element type and dimensionality</a></h2>
<p><code>PyArray</code> has two type parametes <code>T</code> and <code>D</code>.
<code>T</code> represents the type of its elements, e.g. <a href="https://doc.rust-lang.org/1.76.0/std/primitive.f32.html" title="primitive f32"><code>f32</code></a> or [<code>PyObject</code>].
<code>D</code> represents its dimensionality, e.g <a href="../type.Ix2.html" title="type numpy::Ix2"><code>Ix2</code></a> or <a href="../type.IxDyn.html" title="type numpy::IxDyn"><code>IxDyn</code></a>.</p>
<p>Element types are Rust types which implement the <a href="../trait.Element.html" title="trait numpy::Element"><code>Element</code></a> trait.
Dimensions are represented by the <a href="https://docs.rs/ndarray/0.15/ndarray/dimension/dimension_trait/trait.Dimension.html" title="trait ndarray::dimension::dimension_trait::Dimension"><code>ndarray::Dimension</code></a> trait.</p>
<p>Typically, <code>Ix1, Ix2, ...</code> are used for fixed dimensionality arrays,
and <code>IxDyn</code> is used for dynamic dimensionality arrays. Type aliases
for combining <code>PyArray</code> with these types are provided, e.g. <a href="type.PyArray1.html" title="type numpy::array::PyArray1"><code>PyArray1</code></a> or <a href="type.PyArrayDyn.html" title="type numpy::array::PyArrayDyn"><code>PyArrayDyn</code></a>.</p>
<p>To specify concrete dimension like <code>3×4×5</code>, types which implement the <a href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html" title="trait ndarray::dimension::conversion::IntoDimension"><code>ndarray::IntoDimension</code></a>
trait are used. Typically, this means arrays like <code>[3, 4, 5]</code> or tuples like <code>(3, 4, 5)</code>.</p>
<h2 id="example"><a href="#example">Example</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>ndarray::{array, Array};
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::arange(py, <span class="number">0.</span>, <span class="number">4.</span>, <span class="number">1.</span>).reshape([<span class="number">2</span>, <span class="number">2</span>]).unwrap();
<span class="kw">let </span>array = <span class="macro">array!</span>[[<span class="number">3.</span>, <span class="number">4.</span>], [<span class="number">5.</span>, <span class="number">6.</span>]];
<span class="macro">assert_eq!</span>(
array.dot(<span class="kw-2">&</span>pyarray.readonly().as_array()),
<span class="macro">array!</span>[[<span class="number">8.</span>, <span class="number">15.</span>], [<span class="number">12.</span>, <span class="number">23.</span>]]
);
});</code></pre></div>
</div></details><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+D%3E" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#201-255">source</a><a href="#impl-PyArray%3CT,+D%3E" class="anchor">§</a><h3 class="code-header">impl<T, D> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, D></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.as_untyped" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#204-206">source</a><h4 class="code-header">pub fn <a href="#method.as_untyped" class="fn">as_untyped</a>(&self) -> &<a class="struct" href="../struct.PyUntypedArray.html" title="struct numpy::PyUntypedArray">PyUntypedArray</a></h4></section></summary><div class="docblock"><p>Access an untyped representation of this array.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.to_owned" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#228-230">source</a><h4 class="code-header">pub fn <a href="#method.to_owned" class="fn">to_owned</a>(&self) -> Py<Self></h4></section></summary><div class="docblock"><p>Turn <code>&PyArray<T,D></code> into <code>Py<PyArray<T,D>></code>,
i.e. a pointer into Python’s heap which is independent of the GIL lifetime.</p>
<p>This method can be used to avoid lifetime annotations of function arguments
or return values.</p>
<h5 id="example-1"><a href="#example-1">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray1;
<span class="kw">use </span>pyo3::{Py, Python};
<span class="kw">let </span>array: Py<PyArray1<f64>> = Python::with_gil(|py| {
PyArray1::zeros(py, <span class="number">5</span>, <span class="bool-val">false</span>).to_owned()
});
Python::with_gil(|py| {
<span class="macro">assert_eq!</span>(array.as_ref(py).readonly().as_slice().unwrap(), [<span class="number">0.0</span>; <span class="number">5</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_owned_ptr" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#237-239">source</a><h4 class="code-header">pub unsafe fn <a href="#method.from_owned_ptr" class="fn">from_owned_ptr</a><'py>(
py: Python<'py>,
ptr: <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.pointer.html">*mut </a>PyObject
) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a></h4></section></summary><div class="docblock"><p>Constructs a reference to a <code>PyArray</code> from a raw pointer to a Python object.</p>
<h5 id="safety"><a href="#safety">Safety</a></h5>
<p>This is a wrapper around [<code>pyo3::FromPyPointer::from_owned_ptr_or_opt</code>] and inherits its safety contract.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_borrowed_ptr" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#246-248">source</a><h4 class="code-header">pub unsafe fn <a href="#method.from_borrowed_ptr" class="fn">from_borrowed_ptr</a><'py>(
py: Python<'py>,
ptr: <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.pointer.html">*mut </a>PyObject
) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a></h4></section></summary><div class="docblock"><p>Constructs a reference to a <code>PyArray</code> from a raw point to a Python object.</p>
<h5 id="safety-1"><a href="#safety-1">Safety</a></h5>
<p>This is a wrapper around [<code>pyo3::FromPyPointer::from_borrowed_ptr_or_opt</code>] and inherits its safety contract.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.data" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#252-254">source</a><h4 class="code-header">pub fn <a href="#method.data" class="fn">data</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.pointer.html">*mut T</a></h4></section></summary><div class="docblock"><p>Returns a pointer to the first element of the array.</p>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+D%3E-1" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#257-927">source</a><a href="#impl-PyArray%3CT,+D%3E-1" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>, D: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/dimension_trait/trait.Dimension.html" title="trait ndarray::dimension::dimension_trait::Dimension">Dimension</a>> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, D></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.dims" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#292-294">source</a><h4 class="code-header">pub fn <a href="#method.dims" class="fn">dims</a>(&self) -> D</h4></section></summary><div class="docblock"><p>Same as <a href="../struct.PyUntypedArray.html#method.shape" title="method numpy::PyUntypedArray::shape"><code>shape</code></a>, but returns <code>D</code> instead of <code>&[usize]</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.new" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#336-342">source</a><h4 class="code-header">pub unsafe fn <a href="#method.new" class="fn">new</a><'py, ID>(py: Python<'py>, dims: ID, is_fortran: <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&Self</a><div class="where">where
ID: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html" title="trait ndarray::dimension::conversion::IntoDimension">IntoDimension</a><Dim = D>,</div></h4></section></summary><div class="docblock"><p>Creates a new uninitialized NumPy array.</p>
<p>If <code>is_fortran</code> is true, then it has Fortran/column-major order,
otherwise it has C/row-major order.</p>
<h5 id="safety-2"><a href="#safety-2">Safety</a></h5>
<p>The returned array will always be safe to be dropped as the elements must either
be trivially copyable (as indicated by <code><T as Element>::IS_COPY</code>) or be pointers
into Python’s heap, which NumPy will automatically zero-initialize.</p>
<p>However, the elements themselves will not be valid and should be initialized manually
using raw pointers obtained via <a href="struct.PyArray.html#method.uget_raw" title="method numpy::array::PyArray::uget_raw"><code>uget_raw</code></a>. Before that, all methods
which produce references to the elements invoke undefined behaviour. In particular,
zero-initialized pointers are <em>not</em> valid instances of <code>PyObject</code>.</p>
<h5 id="example-2"><a href="#example-2">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray3;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>arr = <span class="kw">unsafe </span>{
<span class="kw">let </span>arr = PyArray3::<i32>::new(py, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], <span class="bool-val">false</span>);
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">4 </span>{
<span class="kw">for </span>j <span class="kw">in </span><span class="number">0</span>..<span class="number">5 </span>{
<span class="kw">for </span>k <span class="kw">in </span><span class="number">0</span>..<span class="number">6 </span>{
arr.uget_raw([i, j, k]).write((i * j * k) <span class="kw">as </span>i32);
}
}
}
arr
};
<span class="macro">assert_eq!</span>(arr.shape(), <span class="kw-2">&</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.borrow_from_array" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#445-466">source</a><h4 class="code-header">pub unsafe fn <a href="#method.borrow_from_array" class="fn">borrow_from_array</a><'py, S>(
array: &<a class="struct" href="https://docs.rs/ndarray/0.15/ndarray/struct.ArrayBase.html" title="struct ndarray::ArrayBase">ArrayBase</a><S, D>,
container: &'py PyAny
) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a><div class="where">where
S: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/data_traits/trait.Data.html" title="trait ndarray::data_traits::Data">Data</a><Elem = T>,</div></h4></section></summary><div class="docblock"><p>Creates a NumPy array backed by <code>array</code> and ties its ownership to the Python object <code>container</code>.</p>
<h5 id="safety-3"><a href="#safety-3">Safety</a></h5>
<p><code>container</code> is set as a base object of the returned array which must not be dropped until <code>container</code> is dropped.
Furthermore, <code>array</code> must not be reallocated from the time this method is called and until <code>container</code> is dropped.</p>
<h5 id="example-3"><a href="#example-3">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[pyclass]
</span><span class="kw">struct </span>Owner {
array: Array1<f64>,
}
<span class="attr">#[pymethods]
</span><span class="kw">impl </span>Owner {
<span class="attr">#[getter]
</span><span class="kw">fn </span>array<<span class="lifetime">'py</span>>(this: <span class="kw-2">&</span><span class="lifetime">'py </span>PyCell<<span class="self">Self</span>>) -> <span class="kw-2">&</span><span class="lifetime">'py </span>PyArray1<f64> {
<span class="kw">let </span>array = <span class="kw-2">&</span>this.borrow().array;
<span class="comment">// SAFETY: The memory backing `array` will stay valid as long as this object is alive
// as we do not modify `array` in any way which would cause it to be reallocated.
</span><span class="kw">unsafe </span>{ PyArray1::borrow_from_array(array, this) }
}
}</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.zeros" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#493-508">source</a><h4 class="code-header">pub fn <a href="#method.zeros" class="fn">zeros</a><'py, ID>(py: Python<'py>, dims: ID, is_fortran: <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&Self</a><div class="where">where
ID: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html" title="trait ndarray::dimension::conversion::IntoDimension">IntoDimension</a><Dim = D>,</div></h4></section></summary><div class="docblock"><p>Construct a new NumPy array filled with zeros.</p>
<p>If <code>is_fortran</code> is true, then it has Fortran/column-major order,
otherwise it has C/row-major order.</p>
<p>For arrays of Python objects, this will fill the array
with valid pointers to zero-valued Python integer objects.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/generated/numpy.zeros.html"><code>numpy.zeros</code></a> and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Zeros"><code>PyArray_Zeros</code></a>.</p>
<h5 id="example-4"><a href="#example-4">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray2;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray: <span class="kw-2">&</span>PyArray2<usize> = PyArray2::zeros(py, [<span class="number">2</span>, <span class="number">2</span>], <span class="bool-val">true</span>);
<span class="macro">assert_eq!</span>(pyarray.readonly().as_slice().unwrap(), [<span class="number">0</span>; <span class="number">4</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_slice" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#519-525">source</a><h4 class="code-header">pub unsafe fn <a href="#method.as_slice" class="fn">as_slice</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.slice.html">[T]</a>, <a class="struct" href="../struct.NotContiguousError.html" title="struct numpy::NotContiguousError">NotContiguousError</a>></h4></section></summary><div class="docblock"><p>Returns an immutable view of the internal data as a slice.</p>
<h5 id="safety-4"><a href="#safety-4">Safety</a></h5>
<p>Calling this method is undefined behaviour if the underlying array
is aliased mutably by other instances of <code>PyArray</code>
or concurrently modified by Python or other native code.</p>
<p>Please consider the safe alternative <a href="../borrow/struct.PyReadonlyArray.html#method.as_slice" title="method numpy::borrow::PyReadonlyArray::as_slice"><code>PyReadonlyArray::as_slice</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_slice_mut" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#536-542">source</a><h4 class="code-header">pub unsafe fn <a href="#method.as_slice_mut" class="fn">as_slice_mut</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&mut <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.slice.html">[T]</a>, <a class="struct" href="../struct.NotContiguousError.html" title="struct numpy::NotContiguousError">NotContiguousError</a>></h4></section></summary><div class="docblock"><p>Returns a mutable view of the internal data as a slice.</p>
<h5 id="safety-5"><a href="#safety-5">Safety</a></h5>
<p>Calling this method is undefined behaviour if the underlying array
is aliased immutably or mutably by other instances of <a href="struct.PyArray.html" title="struct numpy::array::PyArray"><code>PyArray</code></a>
or concurrently modified by Python or other native code.</p>
<p>Please consider the safe alternative <a href="../borrow/struct.PyReadwriteArray.html#method.as_slice_mut" title="method numpy::borrow::PyReadwriteArray::as_slice_mut"><code>PyReadwriteArray::as_slice_mut</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_owned_array" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#561-573">source</a><h4 class="code-header">pub fn <a href="#method.from_owned_array" class="fn">from_owned_array</a><'py>(py: Python<'py>, arr: <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array">Array</a><T, D>) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a></h4></section></summary><div class="docblock"><p>Constructs a NumPy from an <a href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array"><code>ndarray::Array</code></a></p>
<p>This method uses the internal <a href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec</code></a> of the <a href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array"><code>ndarray::Array</code></a> as the base object of the NumPy array.</p>
<h5 id="example-5"><a href="#example-5">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>ndarray::array;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::from_owned_array(py, <span class="macro">array!</span>[[<span class="number">1</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">4</span>]]);
<span class="macro">assert_eq!</span>(pyarray.readonly().as_array(), <span class="macro">array!</span>[[<span class="number">1</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">4</span>]]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#598-601">source</a><h4 class="code-header">pub unsafe fn <a href="#method.get" class="fn">get</a>(&self, index: impl <a class="trait" href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex">NpyIndex</a><Dim = D>) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&T</a>></h4></section></summary><div class="docblock"><p>Get a reference of the specified element if the given index is valid.</p>
<h5 id="safety-6"><a href="#safety-6">Safety</a></h5>
<p>Calling this method is undefined behaviour if the underlying array
is aliased mutably by other instances of <code>PyArray</code>
or concurrently modified by Python or other native code.</p>
<p>Consider using safe alternatives like <a href="../borrow/struct.PyReadonlyArray.html#method.get" title="method numpy::borrow::PyReadonlyArray::get"><code>PyReadonlyArray::get</code></a>.</p>
<h5 id="example-6"><a href="#example-6">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::arange(py, <span class="number">0</span>, <span class="number">16</span>, <span class="number">1</span>).reshape([<span class="number">2</span>, <span class="number">2</span>, <span class="number">4</span>]).unwrap();
<span class="macro">assert_eq!</span>(<span class="kw">unsafe </span>{ <span class="kw-2">*</span>pyarray.get([<span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>]).unwrap() }, <span class="number">11</span>);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_mut" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#630-633">source</a><h4 class="code-header">pub unsafe fn <a href="#method.get_mut" class="fn">get_mut</a>(&self, index: impl <a class="trait" href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex">NpyIndex</a><Dim = D>) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&mut T</a>></h4></section></summary><div class="docblock"><p>Same as <a href="struct.PyArray.html#method.get" title="method numpy::array::PyArray::get"><code>get</code></a>, but returns <code>Option<&mut T></code>.</p>
<h5 id="safety-7"><a href="#safety-7">Safety</a></h5>
<p>Calling this method is undefined behaviour if the underlying array
is aliased immutably or mutably by other instances of <a href="struct.PyArray.html" title="struct numpy::array::PyArray"><code>PyArray</code></a>
or concurrently modified by Python or other native code.</p>
<p>Consider using safe alternatives like <a href="../borrow/struct.PyReadwriteArray.html#method.get_mut" title="method numpy::borrow::PyReadwriteArray::get_mut"><code>PyReadwriteArray::get_mut</code></a>.</p>
<h5 id="example-7"><a href="#example-7">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::arange(py, <span class="number">0</span>, <span class="number">16</span>, <span class="number">1</span>).reshape([<span class="number">2</span>, <span class="number">2</span>, <span class="number">4</span>]).unwrap();
<span class="kw">unsafe </span>{
<span class="kw-2">*</span>pyarray.get_mut([<span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>]).unwrap() = <span class="number">42</span>;
}
<span class="macro">assert_eq!</span>(<span class="kw">unsafe </span>{ <span class="kw-2">*</span>pyarray.get([<span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>]).unwrap() }, <span class="number">42</span>);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.uget" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#670-675">source</a><h4 class="code-header">pub unsafe fn <a href="#method.uget" class="fn">uget</a><Idx>(&self, index: Idx) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&T</a><div class="where">where
Idx: <a class="trait" href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex">NpyIndex</a><Dim = D>,</div></h4></section></summary><div class="docblock"><p>Get an immutable reference of the specified element,
without checking the given index.</p>
<p>See <a href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex"><code>NpyIndex</code></a> for what types can be used as the index.</p>
<h5 id="safety-8"><a href="#safety-8">Safety</a></h5>
<p>Passing an invalid index is undefined behavior.
The element must also have been initialized and
all other references to it is must also be shared.</p>
<p>See <a href="../borrow/struct.PyReadonlyArray.html#method.get" title="method numpy::borrow::PyReadonlyArray::get"><code>PyReadonlyArray::get</code></a> for a safe alternative.</p>
<h5 id="example-8"><a href="#example-8">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::arange(py, <span class="number">0</span>, <span class="number">16</span>, <span class="number">1</span>).reshape([<span class="number">2</span>, <span class="number">2</span>, <span class="number">4</span>]).unwrap();
<span class="macro">assert_eq!</span>(<span class="kw">unsafe </span>{ <span class="kw-2">*</span>pyarray.uget([<span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>]) }, <span class="number">11</span>);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.uget_mut" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#688-693">source</a><h4 class="code-header">pub unsafe fn <a href="#method.uget_mut" class="fn">uget_mut</a><Idx>(&self, index: Idx) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&mut T</a><div class="where">where
Idx: <a class="trait" href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex">NpyIndex</a><Dim = D>,</div></h4></section></summary><div class="docblock"><p>Same as <a href="struct.PyArray.html#method.uget" title="method numpy::array::PyArray::uget"><code>uget</code></a>, but returns <code>&mut T</code>.</p>
<h5 id="safety-9"><a href="#safety-9">Safety</a></h5>
<p>Passing an invalid index is undefined behavior.
The element must also have been initialized and
other references to it must not exist.</p>
<p>See <a href="../borrow/struct.PyReadwriteArray.html#method.get_mut" title="method numpy::borrow::PyReadwriteArray::get_mut"><code>PyReadwriteArray::get_mut</code></a> for a safe alternative.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.uget_raw" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#701-707">source</a><h4 class="code-header">pub unsafe fn <a href="#method.uget_raw" class="fn">uget_raw</a><Idx>(&self, index: Idx) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.pointer.html">*mut T</a><div class="where">where
Idx: <a class="trait" href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex">NpyIndex</a><Dim = D>,</div></h4></section></summary><div class="docblock"><p>Same as <a href="struct.PyArray.html#method.uget" title="method numpy::array::PyArray::uget"><code>uget</code></a>, but returns <code>*mut T</code>.</p>
<h5 id="safety-10"><a href="#safety-10">Safety</a></h5>
<p>Passing an invalid index is undefined behavior.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_owned" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#724-729">source</a><h4 class="code-header">pub fn <a href="#method.get_owned" class="fn">get_owned</a><Idx>(&self, index: Idx) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><T><div class="where">where
Idx: <a class="trait" href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex">NpyIndex</a><Dim = D>,</div></h4></section></summary><div class="docblock"><p>Get a copy of the specified element in the array.</p>
<p>See <a href="../convert/trait.NpyIndex.html" title="trait numpy::convert::NpyIndex"><code>NpyIndex</code></a> for what types can be used as the index.</p>
<h5 id="example-9"><a href="#example-9">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::arange(py, <span class="number">0</span>, <span class="number">16</span>, <span class="number">1</span>).reshape([<span class="number">2</span>, <span class="number">2</span>, <span class="number">4</span>]).unwrap();
<span class="macro">assert_eq!</span>(pyarray.get_owned([<span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>]), <span class="prelude-val">Some</span>(<span class="number">11</span>));
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.to_dyn" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#732-734">source</a><h4 class="code-header">pub fn <a href="#method.to_dyn" class="fn">to_dyn</a>(&self) -> &<a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, <a class="type" href="../type.IxDyn.html" title="type numpy::IxDyn">IxDyn</a>></h4></section></summary><div class="docblock"><p>Turn an array with fixed dimensionality into one with dynamic dimensionality.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.to_vec" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#756-758">source</a><h4 class="code-header">pub fn <a href="#method.to_vec" class="fn">to_vec</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><T>, <a class="struct" href="../struct.NotContiguousError.html" title="struct numpy::NotContiguousError">NotContiguousError</a>></h4></section></summary><div class="docblock"><p>Returns a copy of the internal data of the array as a <a href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec</code></a>.</p>
<p>Fails if the internal array is not contiguous. See also <a href="struct.PyArray.html#method.as_slice" title="method numpy::array::PyArray::as_slice"><code>as_slice</code></a>.</p>
<h5 id="example-10"><a href="#example-10">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray2;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray= py
.eval(<span class="string">"__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')"</span>, <span class="prelude-val">None</span>, <span class="prelude-val">None</span>)
.unwrap()
.downcast::<PyArray2<i64>>()
.unwrap();
<span class="macro">assert_eq!</span>(pyarray.to_vec().unwrap(), <span class="macro">vec!</span>[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_array" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#778-783">source</a><h4 class="code-header">pub fn <a href="#method.from_array" class="fn">from_array</a><'py, S>(py: Python<'py>, arr: &<a class="struct" href="https://docs.rs/ndarray/0.15/ndarray/struct.ArrayBase.html" title="struct ndarray::ArrayBase">ArrayBase</a><S, D>) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a><div class="where">where
S: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/data_traits/trait.Data.html" title="trait ndarray::data_traits::Data">Data</a><Elem = T>,</div></h4></section></summary><div class="docblock"><p>Construct a NumPy array from a <a href="https://docs.rs/ndarray/0.15/ndarray/struct.ArrayBase.html" title="struct ndarray::ArrayBase"><code>ndarray::ArrayBase</code></a>.</p>
<p>This method allocates memory in Python’s heap via the NumPy API,
and then copies all elements of the array there.</p>
<h5 id="example-11"><a href="#example-11">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>ndarray::array;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::from_array(py, <span class="kw-2">&</span><span class="macro">array!</span>[[<span class="number">1</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">4</span>]]);
<span class="macro">assert_eq!</span>(pyarray.readonly().as_array(), <span class="macro">array!</span>[[<span class="number">1</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">4</span>]]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_readonly" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#786-788">source</a><h4 class="code-header">pub fn <a href="#method.try_readonly" class="fn">try_readonly</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="struct" href="../borrow/struct.PyReadonlyArray.html" title="struct numpy::borrow::PyReadonlyArray">PyReadonlyArray</a><'_, T, D>, <a class="enum" href="../enum.BorrowError.html" title="enum numpy::BorrowError">BorrowError</a>></h4></section></summary><div class="docblock"><p>Get an immutable borrow of the NumPy array</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.readonly" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#797-799">source</a><h4 class="code-header">pub fn <a href="#method.readonly" class="fn">readonly</a>(&self) -> <a class="struct" href="../borrow/struct.PyReadonlyArray.html" title="struct numpy::borrow::PyReadonlyArray">PyReadonlyArray</a><'_, T, D></h4></section></summary><div class="docblock"><p>Get an immutable borrow of the NumPy array</p>
<h5 id="panics"><a href="#panics">Panics</a></h5>
<p>Panics if the allocation backing the array is currently mutably borrowed.</p>
<p>For a non-panicking variant, use <a href="struct.PyArray.html#method.try_readonly" title="method numpy::array::PyArray::try_readonly"><code>try_readonly</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_readwrite" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#802-804">source</a><h4 class="code-header">pub fn <a href="#method.try_readwrite" class="fn">try_readwrite</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="struct" href="../borrow/struct.PyReadwriteArray.html" title="struct numpy::borrow::PyReadwriteArray">PyReadwriteArray</a><'_, T, D>, <a class="enum" href="../enum.BorrowError.html" title="enum numpy::BorrowError">BorrowError</a>></h4></section></summary><div class="docblock"><p>Get a mutable borrow of the NumPy array</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.readwrite" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#816-818">source</a><h4 class="code-header">pub fn <a href="#method.readwrite" class="fn">readwrite</a>(&self) -> <a class="struct" href="../borrow/struct.PyReadwriteArray.html" title="struct numpy::borrow::PyReadwriteArray">PyReadwriteArray</a><'_, T, D></h4></section></summary><div class="docblock"><p>Get a mutable borrow of the NumPy array</p>
<h5 id="panics-1"><a href="#panics-1">Panics</a></h5>
<p>Panics if the allocation backing the array is currently borrowed or
if the array is <a href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flags.html">flagged as</a> not writeable.</p>
<p>For a non-panicking variant, use <a href="struct.PyArray.html#method.try_readwrite" title="method numpy::array::PyArray::try_readwrite"><code>try_readwrite</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_array" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#881-883">source</a><h4 class="code-header">pub unsafe fn <a href="#method.as_array" class="fn">as_array</a>(&self) -> <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/type.ArrayView.html" title="type ndarray::ArrayView">ArrayView</a><'_, T, D></h4></section></summary><div class="docblock"><p>Returns an <a href="https://docs.rs/ndarray/0.15/ndarray/type.ArrayView.html" title="type ndarray::ArrayView"><code>ArrayView</code></a> of the internal array.</p>
<p>See also <a href="../borrow/struct.PyReadonlyArray.html#method.as_array" title="method numpy::borrow::PyReadonlyArray::as_array"><code>PyReadonlyArray::as_array</code></a>.</p>
<h5 id="safety-11"><a href="#safety-11">Safety</a></h5>
<p>Calling this method invalidates all exclusive references to the internal data, e.g. <code>&mut [T]</code> or <code>ArrayViewMut</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_array_mut" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#892-894">source</a><h4 class="code-header">pub unsafe fn <a href="#method.as_array_mut" class="fn">as_array_mut</a>(&self) -> <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/type.ArrayViewMut.html" title="type ndarray::ArrayViewMut">ArrayViewMut</a><'_, T, D></h4></section></summary><div class="docblock"><p>Returns an <a href="https://docs.rs/ndarray/0.15/ndarray/type.ArrayViewMut.html" title="type ndarray::ArrayViewMut"><code>ArrayViewMut</code></a> of the internal array.</p>
<p>See also <a href="../borrow/struct.PyReadwriteArray.html#method.as_array_mut" title="method numpy::borrow::PyReadwriteArray::as_array_mut"><code>PyReadwriteArray::as_array_mut</code></a>.</p>
<h5 id="safety-12"><a href="#safety-12">Safety</a></h5>
<p>Calling this method invalidates all other references to the internal data, e.g. <code>ArrayView</code> or <code>ArrayViewMut</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_raw_array" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#897-899">source</a><h4 class="code-header">pub fn <a href="#method.as_raw_array" class="fn">as_raw_array</a>(&self) -> <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/type.RawArrayView.html" title="type ndarray::RawArrayView">RawArrayView</a><T, D></h4></section></summary><div class="docblock"><p>Returns the internal array as <a href="https://docs.rs/ndarray/0.15/ndarray/type.RawArrayView.html" title="type ndarray::RawArrayView"><code>RawArrayView</code></a> enabling element access via raw pointers</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.as_raw_array_mut" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#902-904">source</a><h4 class="code-header">pub fn <a href="#method.as_raw_array_mut" class="fn">as_raw_array_mut</a>(&self) -> <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/type.RawArrayViewMut.html" title="type ndarray::RawArrayViewMut">RawArrayViewMut</a><T, D></h4></section></summary><div class="docblock"><p>Returns the internal array as <a href="https://docs.rs/ndarray/0.15/ndarray/type.RawArrayViewMut.html" title="type ndarray::RawArrayViewMut"><code>RawArrayViewMut</code></a> enabling element access via raw pointers</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.to_owned_array" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#924-926">source</a><h4 class="code-header">pub fn <a href="#method.to_owned_array" class="fn">to_owned_array</a>(&self) -> <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array">Array</a><T, D></h4></section></summary><div class="docblock"><p>Get a copy of the array as an <a href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array"><code>ndarray::Array</code></a>.</p>
<h5 id="example-12"><a href="#example-12">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>ndarray::array;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::arange(py, <span class="number">0</span>, <span class="number">4</span>, <span class="number">1</span>).reshape([<span class="number">2</span>, <span class="number">2</span>]).unwrap();
<span class="macro">assert_eq!</span>(
pyarray.to_owned_array(),
<span class="macro">array!</span>[[<span class="number">0</span>, <span class="number">1</span>], [<span class="number">2</span>, <span class="number">3</span>]]
)
});</code></pre></div>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CN,+D%3E" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#930-1033">source</a><a href="#impl-PyArray%3CN,+D%3E" class="anchor">§</a><h3 class="code-header">impl<N, D> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><N, D><div class="where">where
N: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/scalar/trait.Scalar.html" title="trait nalgebra::base::scalar::Scalar">Scalar</a> + <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>,
D: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/dimension_trait/trait.Dimension.html" title="trait ndarray::dimension::dimension_trait::Dimension">Dimension</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.try_as_matrix" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#996-1010">source</a><h4 class="code-header">pub unsafe fn <a href="#method.try_as_matrix" class="fn">try_as_matrix</a><R, C, RStride, CStride>(
&self
) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="type" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/matrix_view/type.MatrixView.html" title="type nalgebra::base::matrix_view::MatrixView">MatrixView</a><'_, N, R, C, RStride, CStride>><div class="where">where
R: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,
C: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,
RStride: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,
CStride: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,</div></h4></section></summary><div class="docblock"><p>Try to convert this array into a <a href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/matrix_view/type.MatrixView.html" title="type nalgebra::base::matrix_view::MatrixView"><code>nalgebra::MatrixView</code></a> using the given shape and strides.</p>
<h5 id="safety-13"><a href="#safety-13">Safety</a></h5>
<p>Calling this method invalidates all exclusive references to the internal data, e.g. <code>ArrayViewMut</code> or <code>MatrixSliceMut</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_as_matrix_mut" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1018-1032">source</a><h4 class="code-header">pub unsafe fn <a href="#method.try_as_matrix_mut" class="fn">try_as_matrix_mut</a><R, C, RStride, CStride>(
&self
) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="type" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/matrix_view/type.MatrixViewMut.html" title="type nalgebra::base::matrix_view::MatrixViewMut">MatrixViewMut</a><'_, N, R, C, RStride, CStride>><div class="where">where
R: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,
C: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,
RStride: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,
CStride: <a class="trait" href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/dimension/trait.Dim.html" title="trait nalgebra::base::dimension::Dim">Dim</a>,</div></h4></section></summary><div class="docblock"><p>Try to convert this array into a <a href="https://docs.rs/nalgebra/0.25.0/nalgebra/base/matrix_view/type.MatrixViewMut.html" title="type nalgebra::base::matrix_view::MatrixViewMut"><code>nalgebra::MatrixViewMut</code></a> using the given shape and strides.</p>
<h5 id="safety-14"><a href="#safety-14">Safety</a></h5>
<p>Calling this method invalidates all other references to the internal data, e.g. <code>ArrayView</code>, <code>MatrixSlice</code>, <code>ArrayViewMut</code> or <code>MatrixSliceMut</code>.</p>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CPy%3CPyAny%3E,+D%3E" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#1035-1083">source</a><a href="#impl-PyArray%3CPy%3CPyAny%3E,+D%3E" class="anchor">§</a><h3 class="code-header">impl<D: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/dimension_trait/trait.Dimension.html" title="trait ndarray::dimension::dimension_trait::Dimension">Dimension</a>> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><PyObject, D></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from_owned_object_array" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1070-1082">source</a><h4 class="code-header">pub fn <a href="#method.from_owned_object_array" class="fn">from_owned_object_array</a><'py, T>(
py: Python<'py>,
arr: <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array">Array</a><Py<T>, D>
) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a></h4></section></summary><div class="docblock"><p>Construct a NumPy array containing objects stored in a <a href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array"><code>ndarray::Array</code></a></p>
<p>This method uses the internal <a href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec</code></a> of the <a href="https://docs.rs/ndarray/0.15/ndarray/type.Array.html" title="type ndarray::Array"><code>ndarray::Array</code></a> as the base object of the NumPy array.</p>
<h5 id="example-13"><a href="#example-13">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ndarray::array;
<span class="kw">use </span>pyo3::{pyclass, Py, Python};
<span class="kw">use </span>numpy::PyArray;
<span class="attr">#[pyclass]
</span><span class="kw">struct </span>CustomElement {
foo: i32,
bar: f64,
}
Python::with_gil(|py| {
<span class="kw">let </span>array = <span class="macro">array!</span>[
Py::new(py, CustomElement {
foo: <span class="number">1</span>,
bar: <span class="number">2.0</span>,
}).unwrap(),
Py::new(py, CustomElement {
foo: <span class="number">3</span>,
bar: <span class="number">4.0</span>,
}).unwrap(),
];
<span class="kw">let </span>pyarray = PyArray::from_owned_object_array(py, array);
<span class="macro">assert!</span>(pyarray.readonly().as_array().get(<span class="number">0</span>).unwrap().as_ref(py).is_instance_of::<CustomElement>());
});</code></pre></div>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+Dim%3C%5Busize;+0%5D%3E%3E" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#1085-1092">source</a><a href="#impl-PyArray%3CT,+Dim%3C%5Busize;+0%5D%3E%3E" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="https://doc.rust-lang.org/1.76.0/core/marker/trait.Copy.html" title="trait core::marker::Copy">Copy</a> + <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, <a class="type" href="https://docs.rs/ndarray/0.15/ndarray/aliases/type.Ix0.html" title="type ndarray::aliases::Ix0">Ix0</a>></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.item" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1089-1091">source</a><h4 class="code-header">pub fn <a href="#method.item" class="fn">item</a>(&self) -> T</h4></section></summary><div class="docblock"><p>Get the single element of a zero-dimensional array.</p>
<p>See <a href="../fn.inner.html" title="fn numpy::inner"><code>inner</code></a> for an example.</p>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+Dim%3C%5Busize;+1%5D%3E%3E" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#1094-1160">source</a><a href="#impl-PyArray%3CT,+Dim%3C%5Busize;+1%5D%3E%3E" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, <a class="type" href="../type.Ix1.html" title="type numpy::Ix1">Ix1</a>></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from_slice" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1109-1116">source</a><h4 class="code-header">pub fn <a href="#method.from_slice" class="fn">from_slice</a><'py>(py: Python<'py>, slice: &<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.slice.html">[T]</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a></h4></section></summary><div class="docblock"><p>Construct a one-dimensional array from a <a href="https://doc.rust-lang.org/1.76.0/alloc/slice/index.html" title="mod alloc::slice">slice</a>.</p>
<h5 id="example-14"><a href="#example-14">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>slice = <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];
<span class="kw">let </span>pyarray = PyArray::from_slice(py, slice);
<span class="macro">assert_eq!</span>(pyarray.readonly().as_slice().unwrap(), <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_vec" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1133-1135">source</a><h4 class="code-header">pub fn <a href="#method.from_vec" class="fn">from_vec</a><'py>(py: Python<'py>, vec: <a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><T>) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a></h4></section></summary><div class="docblock"><p>Construct a one-dimensional array from a <a href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec<T></code></a>.</p>
<h5 id="example-15"><a href="#example-15">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>vec = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];
<span class="kw">let </span>pyarray = PyArray::from_vec(py, vec);
<span class="macro">assert_eq!</span>(pyarray.readonly().as_slice().unwrap(), <span class="kw-2">&</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.from_iter" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1153-1159">source</a><h4 class="code-header">pub fn <a href="#method.from_iter" class="fn">from_iter</a><'py, I>(py: Python<'py>, iter: I) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a><div class="where">where
I: <a class="trait" href="https://doc.rust-lang.org/1.76.0/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a><Item = T>,</div></h4></section></summary><div class="docblock"><p>Construct a one-dimensional array from an <a href="https://doc.rust-lang.org/1.76.0/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator"><code>Iterator</code></a>.</p>
<p>If no reliable <a href="https://doc.rust-lang.org/1.76.0/core/iter/traits/iterator/trait.Iterator.html#method.size_hint" title="method core::iter::traits::iterator::Iterator::size_hint"><code>size_hint</code></a> is available,
this method can allocate memory multiple times, which can hurt performance.</p>
<h5 id="example-16"><a href="#example-16">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::from_iter(py, <span class="string">"abcde"</span>.chars().map(u32::from));
<span class="macro">assert_eq!</span>(pyarray.readonly().as_slice().unwrap(), <span class="kw-2">&</span>[<span class="number">97</span>, <span class="number">98</span>, <span class="number">99</span>, <span class="number">100</span>, <span class="number">101</span>]);
});</code></pre></div>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+Dim%3C%5Busize;+2%5D%3E%3E" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#1162-1201">source</a><a href="#impl-PyArray%3CT,+Dim%3C%5Busize;+2%5D%3E%3E" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, <a class="type" href="../type.Ix2.html" title="type numpy::Ix2">Ix2</a>></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from_vec2" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1184-1200">source</a><h4 class="code-header">pub fn <a href="#method.from_vec2" class="fn">from_vec2</a><'py>(
py: Python<'py>,
v: &[<a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><T>]
) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a>, <a class="struct" href="../struct.FromVecError.html" title="struct numpy::FromVecError">FromVecError</a>></h4></section></summary><div class="docblock"><p>Construct a two-dimension array from a <a href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec<Vec<T>></code></a>.</p>
<p>This function checks all dimensions of the inner vectors and returns
an error if they are not all equal.</p>
<h5 id="example-17"><a href="#example-17">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
<span class="kw">use </span>ndarray::array;
Python::with_gil(|py| {
<span class="kw">let </span>vec2 = <span class="macro">vec!</span>[<span class="macro">vec!</span>[<span class="number">11</span>, <span class="number">12</span>], <span class="macro">vec!</span>[<span class="number">21</span>, <span class="number">22</span>]];
<span class="kw">let </span>pyarray = PyArray::from_vec2(py, <span class="kw-2">&</span>vec2).unwrap();
<span class="macro">assert_eq!</span>(pyarray.readonly().as_array(), <span class="macro">array!</span>[[<span class="number">11</span>, <span class="number">12</span>], [<span class="number">21</span>, <span class="number">22</span>]]);
<span class="kw">let </span>ragged_vec2 = <span class="macro">vec!</span>[<span class="macro">vec!</span>[<span class="number">11</span>, <span class="number">12</span>], <span class="macro">vec!</span>[<span class="number">21</span>]];
<span class="macro">assert!</span>(PyArray::from_vec2(py, <span class="kw-2">&</span>ragged_vec2).is_err());
});</code></pre></div>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+Dim%3C%5Busize;+3%5D%3E%3E" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#1203-1258">source</a><a href="#impl-PyArray%3CT,+Dim%3C%5Busize;+3%5D%3E%3E" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, <a class="type" href="../type.Ix3.html" title="type numpy::Ix3">Ix3</a>></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from_vec3" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1234-1257">source</a><h4 class="code-header">pub fn <a href="#method.from_vec3" class="fn">from_vec3</a><'py>(
py: Python<'py>,
v: &[<a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><<a class="struct" href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><T>>]
) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&'py Self</a>, <a class="struct" href="../struct.FromVecError.html" title="struct numpy::FromVecError">FromVecError</a>></h4></section></summary><div class="docblock"><p>Construct a three-dimensional array from a <a href="https://doc.rust-lang.org/1.76.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec<Vec<Vec<T>>></code></a>.</p>
<p>This function checks all dimensions of the inner vectors and returns
an error if they are not all equal.</p>
<h5 id="example-18"><a href="#example-18">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
<span class="kw">use </span>ndarray::array;
Python::with_gil(|py| {
<span class="kw">let </span>vec3 = <span class="macro">vec!</span>[
<span class="macro">vec!</span>[<span class="macro">vec!</span>[<span class="number">111</span>, <span class="number">112</span>], <span class="macro">vec!</span>[<span class="number">121</span>, <span class="number">122</span>]],
<span class="macro">vec!</span>[<span class="macro">vec!</span>[<span class="number">211</span>, <span class="number">212</span>], <span class="macro">vec!</span>[<span class="number">221</span>, <span class="number">222</span>]],
];
<span class="kw">let </span>pyarray = PyArray::from_vec3(py, <span class="kw-2">&</span>vec3).unwrap();
<span class="macro">assert_eq!</span>(
pyarray.readonly().as_array(),
<span class="macro">array!</span>[[[<span class="number">111</span>, <span class="number">112</span>], [<span class="number">121</span>, <span class="number">122</span>]], [[<span class="number">211</span>, <span class="number">212</span>], [<span class="number">221</span>, <span class="number">222</span>]]]
);
<span class="kw">let </span>ragged_vec3 = <span class="macro">vec!</span>[
<span class="macro">vec!</span>[<span class="macro">vec!</span>[<span class="number">111</span>, <span class="number">112</span>], <span class="macro">vec!</span>[<span class="number">121</span>, <span class="number">122</span>]],
<span class="macro">vec!</span>[<span class="macro">vec!</span>[<span class="number">211</span>], <span class="macro">vec!</span>[<span class="number">221</span>, <span class="number">222</span>]],
];
<span class="macro">assert!</span>(PyArray::from_vec3(py, <span class="kw-2">&</span>ragged_vec3).is_err());
});</code></pre></div>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+D%3E-2" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#1260-1433">source</a><a href="#impl-PyArray%3CT,+D%3E-2" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>, D> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, D></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.copy_to" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1282-1291">source</a><h4 class="code-header">pub fn <a href="#method.copy_to" class="fn">copy_to</a><U: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>>(&self, other: &<a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><U, D>) -> PyResult<<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Copies <code>self</code> into <code>other</code>, performing a data type conversion if necessary.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_CopyInto"><code>PyArray_CopyInto</code></a>.</p>
<h5 id="example-19"><a href="#example-19">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray_f = PyArray::arange(py, <span class="number">2.0</span>, <span class="number">5.0</span>, <span class="number">1.0</span>);
<span class="kw">let </span>pyarray_i = <span class="kw">unsafe </span>{ PyArray::<i64, <span class="kw">_</span>>::new(py, [<span class="number">3</span>], <span class="bool-val">false</span>) };
<span class="macro">assert!</span>(pyarray_f.copy_to(pyarray_i).is_ok());
<span class="macro">assert_eq!</span>(pyarray_i.readonly().as_slice().unwrap(), <span class="kw-2">&</span>[<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.cast" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1313-1327">source</a><h4 class="code-header">pub fn <a href="#method.cast" class="fn">cast</a><'py, U: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a>>(
&'py self,
is_fortran: <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>
) -> PyResult<&'py <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><U, D>></h4></section></summary><div class="docblock"><p>Cast the <code>PyArray<T></code> to <code>PyArray<U></code>, by allocating a new array.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_CastToType"><code>PyArray_CastToType</code></a>.</p>
<h5 id="example-20"><a href="#example-20">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray_f = PyArray::arange(py, <span class="number">2.0</span>, <span class="number">5.0</span>, <span class="number">1.0</span>);
<span class="kw">let </span>pyarray_i = pyarray_f.cast::<i32>(<span class="bool-val">false</span>).unwrap();
<span class="macro">assert_eq!</span>(pyarray_i.readonly().as_slice().unwrap(), <span class="kw-2">&</span>[<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.reshape_with_order" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1355-1375">source</a><h4 class="code-header">pub fn <a href="#method.reshape_with_order" class="fn">reshape_with_order</a><'py, ID: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html" title="trait ndarray::dimension::conversion::IntoDimension">IntoDimension</a>>(
&'py self,
dims: ID,
order: <a class="enum" href="../npyffi/types/enum.NPY_ORDER.html" title="enum numpy::npyffi::types::NPY_ORDER">NPY_ORDER</a>
) -> PyResult<&'py <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, ID::<a class="associatedtype" href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html#associatedtype.Dim" title="type ndarray::dimension::conversion::IntoDimension::Dim">Dim</a>>></h4></section></summary><div class="docblock"><p>Construct a new array which has same values as self,
but has different dimensions specified by <code>dims</code>
and a possibly different memory order specified by <code>order</code>.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/generated/numpy.reshape.html"><code>numpy.reshape</code></a> and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Newshape"><code>PyArray_Newshape</code></a>.</p>
<h5 id="example-21"><a href="#example-21">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::{npyffi::NPY_ORDER, PyArray};
<span class="kw">use </span>pyo3::Python;
<span class="kw">use </span>ndarray::array;
Python::with_gil(|py| {
<span class="kw">let </span>array =
PyArray::from_iter(py, <span class="number">0</span>..<span class="number">9</span>).reshape_with_order([<span class="number">3</span>, <span class="number">3</span>], NPY_ORDER::NPY_FORTRANORDER).unwrap();
<span class="macro">assert_eq!</span>(array.readonly().as_array(), <span class="macro">array!</span>[[<span class="number">0</span>, <span class="number">3</span>, <span class="number">6</span>], [<span class="number">1</span>, <span class="number">4</span>, <span class="number">7</span>], [<span class="number">2</span>, <span class="number">5</span>, <span class="number">8</span>]]);
<span class="macro">assert!</span>(array.is_fortran_contiguous());
<span class="macro">assert!</span>(array.reshape([<span class="number">5</span>]).is_err());
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.reshape" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1379-1384">source</a><h4 class="code-header">pub fn <a href="#method.reshape" class="fn">reshape</a><'py, ID: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html" title="trait ndarray::dimension::conversion::IntoDimension">IntoDimension</a>>(
&'py self,
dims: ID
) -> PyResult<&'py <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, ID::<a class="associatedtype" href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html#associatedtype.Dim" title="type ndarray::dimension::conversion::IntoDimension::Dim">Dim</a>>></h4></section></summary><div class="docblock"><p>Special case of <a href="struct.PyArray.html#method.reshape_with_order" title="method numpy::array::PyArray::reshape_with_order"><code>reshape_with_order</code></a> which keeps the memory order the same.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.resize" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1417-1432">source</a><h4 class="code-header">pub unsafe fn <a href="#method.resize" class="fn">resize</a><ID: <a class="trait" href="https://docs.rs/ndarray/0.15/ndarray/dimension/conversion/trait.IntoDimension.html" title="trait ndarray::dimension::conversion::IntoDimension">IntoDimension</a>>(&self, dims: ID) -> PyResult<<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>></h4></section></summary><div class="docblock"><p>Extends or truncates the dimensions of an array.</p>
<p>This method works only on <a href="../struct.PyUntypedArray.html#method.is_contiguous" title="method numpy::PyUntypedArray::is_contiguous">contiguous</a> arrays.
Missing elements will be initialized as if calling <a href="struct.PyArray.html#method.zeros" title="associated function numpy::array::PyArray::zeros"><code>zeros</code></a>.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.resize.html"><code>ndarray.resize</code></a> and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Resize"><code>PyArray_Resize</code></a>.</p>
<h5 id="safety-15"><a href="#safety-15">Safety</a></h5>
<p>There should be no outstanding references (shared or exclusive) into the array
as this method might re-allocate it and thereby invalidate all pointers into it.</p>
<h5 id="example-22"><a href="#example-22">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::<f64, <span class="kw">_</span>>::zeros(py, (<span class="number">10</span>, <span class="number">10</span>), <span class="bool-val">false</span>);
<span class="macro">assert_eq!</span>(pyarray.shape(), [<span class="number">10</span>, <span class="number">10</span>]);
<span class="kw">unsafe </span>{
pyarray.resize((<span class="number">100</span>, <span class="number">100</span>)).unwrap();
}
<span class="macro">assert_eq!</span>(pyarray.shape(), [<span class="number">100</span>, <span class="number">100</span>]);
});</code></pre></div>
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-PyArray%3CT,+Dim%3C%5Busize;+1%5D%3E%3E-1" class="impl"><a class="src rightside" href="../../src/numpy/array.rs.html#1435-1469">source</a><a href="#impl-PyArray%3CT,+Dim%3C%5Busize;+1%5D%3E%3E-1" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="../trait.Element.html" title="trait numpy::Element">Element</a> + <a class="trait" href="https://docs.rs/num-traits/0.2/num_traits/cast/trait.AsPrimitive.html" title="trait num_traits::cast::AsPrimitive">AsPrimitive</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.f64.html">f64</a>>> <a class="struct" href="struct.PyArray.html" title="struct numpy::array::PyArray">PyArray</a><T, <a class="type" href="../type.Ix1.html" title="type numpy::Ix1">Ix1</a>></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.arange" class="method"><a class="src rightside" href="../../src/numpy/array.rs.html#1457-1468">source</a><h4 class="code-header">pub fn <a href="#method.arange" class="fn">arange</a><'py>(py: Python<'py>, start: T, stop: T, step: T) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&Self</a></h4></section></summary><div class="docblock"><p>Return evenly spaced values within a given interval.</p>
<p>See <a href="https://numpy.org/doc/stable/reference/generated/numpy.arange.html">numpy.arange</a> for the Python API and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Arange">PyArray_Arange</a> for the C API.</p>
<h5 id="example-23"><a href="#example-23">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>pyarray = PyArray::arange(py, <span class="number">2.0</span>, <span class="number">4.0</span>, <span class="number">0.5</span>);
<span class="macro">assert_eq!</span>(pyarray.readonly().as_slice().unwrap(), <span class="kw-2">&</span>[<span class="number">2.0</span>, <span class="number">2.5</span>, <span class="number">3.0</span>, <span class="number">3.5</span>]);
<span class="kw">let </span>pyarray = PyArray::arange(py, -<span class="number">2</span>, <span class="number">4</span>, <span class="number">3</span>);
<span class="macro">assert_eq!</span>(pyarray.readonly().as_slice().unwrap(), <span class="kw-2">&</span>[-<span class="number">2</span>, <span class="number">1</span>]);
});</code></pre></div>
</div></details></div></details></div><h2 id="deref-methods-PyUntypedArray" class="section-header"><span>Methods from <a class="trait" href="https://doc.rust-lang.org/1.76.0/core/ops/deref/trait.Deref.html" title="trait core::ops::deref::Deref">Deref</a><Target = <a class="struct" href="../struct.PyUntypedArray.html" title="struct numpy::PyUntypedArray">PyUntypedArray</a>></span><a href="#deref-methods-PyUntypedArray" class="anchor">§</a></h2><div id="deref-methods-PyUntypedArray-1" class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.as_array_ptr" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#90-92">source</a><h4 class="code-header">pub fn <a href="#method.as_array_ptr" class="fn">as_array_ptr</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.pointer.html">*mut </a><a class="struct" href="../npyffi/objects/struct.PyArrayObject.html" title="struct numpy::npyffi::objects::PyArrayObject">PyArrayObject</a></h4></section></summary><div class="docblock"><p>Returns a raw pointer to the underlying <a href="../npyffi/objects/struct.PyArrayObject.html" title="struct numpy::npyffi::objects::PyArrayObject"><code>PyArrayObject</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.dtype" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#114-116">source</a><h4 class="code-header">pub fn <a href="#method.dtype" class="fn">dtype</a>(&self) -> &<a class="struct" href="../struct.PyArrayDescr.html" title="struct numpy::PyArrayDescr">PyArrayDescr</a></h4></section></summary><div class="docblock"><p>Returns the <code>dtype</code> of the array.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.dtype.html"><code>ndarray.dtype</code></a> and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_DTYPE"><code>PyArray_DTYPE</code></a>.</p>
<h5 id="example-24"><a href="#example-24">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::{dtype_bound, PyArray};
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>array = PyArray::from_vec(py, <span class="macro">vec!</span>[<span class="number">1_i32</span>, <span class="number">2</span>, <span class="number">3</span>]);
<span class="macro">assert!</span>(array.dtype().is_equiv_to(dtype_bound::<i32>(py).as_gil_ref()));
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_contiguous" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#140-142">source</a><h4 class="code-header">pub fn <a href="#method.is_contiguous" class="fn">is_contiguous</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns <code>true</code> if the internal data of the array is contiguous,
indepedently of whether C-style/row-major or Fortran-style/column-major.</p>
<h5 id="example-25"><a href="#example-25">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray1;
<span class="kw">use </span>pyo3::{types::IntoPyDict, Python};
Python::with_gil(|py| {
<span class="kw">let </span>array = PyArray1::arange(py, <span class="number">0</span>, <span class="number">10</span>, <span class="number">1</span>);
<span class="macro">assert!</span>(array.is_contiguous());
<span class="kw">let </span>view = py
.eval(<span class="string">"array[::2]"</span>, <span class="prelude-val">None</span>, <span class="prelude-val">Some</span>([(<span class="string">"array"</span>, array)].into_py_dict(py)))
.unwrap()
.downcast::<PyArray1<i32>>()
.unwrap();
<span class="macro">assert!</span>(!view.is_contiguous());
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_fortran_contiguous" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#146-148">source</a><h4 class="code-header">pub fn <a href="#method.is_fortran_contiguous" class="fn">is_fortran_contiguous</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns <code>true</code> if the internal data of the array is Fortran-style/column-major contiguous.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_c_contiguous" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#152-154">source</a><h4 class="code-header">pub fn <a href="#method.is_c_contiguous" class="fn">is_c_contiguous</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns <code>true</code> if the internal data of the array is C-style/row-major contiguous.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.ndim" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#176-178">source</a><h4 class="code-header">pub fn <a href="#method.ndim" class="fn">ndim</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.usize.html">usize</a></h4></section></summary><div class="docblock"><p>Returns the number of dimensions of the array.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.ndim.html"><code>ndarray.ndim</code></a> and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_NDIM"><code>PyArray_NDIM</code></a>.</p>
<h5 id="example-26"><a href="#example-26">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray3;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>arr = PyArray3::<f64>::zeros(py, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], <span class="bool-val">false</span>);
<span class="macro">assert_eq!</span>(arr.ndim(), <span class="number">3</span>);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.strides" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#199-210">source</a><h4 class="code-header">pub fn <a href="#method.strides" class="fn">strides</a>(&self) -> &[<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.isize.html">isize</a>]</h4></section></summary><div class="docblock"><p>Returns a slice indicating how many bytes to advance when iterating along each axis.</p>
<p>See also <a href="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.strides.html"><code>ndarray.strides</code></a> and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_STRIDES"><code>PyArray_STRIDES</code></a>.</p>
<h5 id="example-27"><a href="#example-27">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray3;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>arr = PyArray3::<f64>::zeros(py, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], <span class="bool-val">false</span>);
<span class="macro">assert_eq!</span>(arr.strides(), <span class="kw-2">&</span>[<span class="number">240</span>, <span class="number">48</span>, <span class="number">8</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.shape" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#232-243">source</a><h4 class="code-header">pub fn <a href="#method.shape" class="fn">shape</a>(&self) -> &[<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.usize.html">usize</a>]</h4></section></summary><div class="docblock"><p>Returns a slice which contains dimmensions of the array.</p>
<p>See also [<code>ndarray.shape</code>][ndaray-shape] and <a href="https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_DIMS"><code>PyArray_DIMS</code></a>.</p>
<h5 id="example-28"><a href="#example-28">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>numpy::PyArray3;
<span class="kw">use </span>pyo3::Python;
Python::with_gil(|py| {
<span class="kw">let </span>arr = PyArray3::<f64>::zeros(py, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], <span class="bool-val">false</span>);
<span class="macro">assert_eq!</span>(arr.shape(), <span class="kw-2">&</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]);
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.len" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#247-249">source</a><h4 class="code-header">pub fn <a href="#method.len" class="fn">len</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.usize.html">usize</a></h4></section></summary><div class="docblock"><p>Calculates the total number of elements in the array.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_empty" class="method"><a class="src rightside" href="../../src/numpy/untyped_array.rs.html#253-255">source</a><h4 class="code-header">pub fn <a href="#method.is_empty" class="fn">is_empty</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns <code>true</code> if the there are no elements in the array.</p>
</div></details></div><h2 id="deref-methods-PyAny" class="section-header"><span>Methods from <a class="trait" href="https://doc.rust-lang.org/1.76.0/core/ops/deref/trait.Deref.html" title="trait core::ops::deref::Deref">Deref</a><Target = PyAny></span><a href="#deref-methods-PyAny" class="anchor">§</a></h2><div id="deref-methods-PyAny-1" class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.is" class="method"><h4 class="code-header">pub fn <a href="#method.is" class="fn">is</a><T>(&self, other: <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&T</a>) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a><div class="where">where
T: AsPyPointer,</div></h4></section></summary><div class="docblock"><p>Returns whether <code>self</code> and <code>other</code> point to the same object. To compare
the equality of two objects (the <code>==</code> operator), use <a href="PyAny::eq"><code>eq</code></a>.</p>
<p>This is equivalent to the Python expression <code>self is other</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.hasattr" class="method"><h4 class="code-header">pub fn <a href="#method.hasattr" class="fn">hasattr</a><N>(&self, attr_name: N) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr><div class="where">where
N: IntoPy<Py<PyString>>,</div></h4></section></summary><div class="docblock"><p>Determines whether this object has the given attribute.</p>
<p>This is equivalent to the Python expression <code>hasattr(self, attr_name)</code>.</p>
<p>To avoid repeated temporary allocations of Python strings, the [<code>intern!</code>] macro can be used
to intern <code>attr_name</code>.</p>
<h5 id="example-interning-the-attribute-name"><a href="#example-interning-the-attribute-name">Example: <code>intern!</code>ing the attribute name</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[pyfunction]
</span><span class="kw">fn </span>has_version(sys: <span class="kw-2">&</span>Bound<<span class="lifetime">'_</span>, PyModule>) -> PyResult<bool> {
sys.hasattr(<span class="macro">intern!</span>(sys.py(), <span class="string">"version"</span>))
}</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.getattr" class="method"><h4 class="code-header">pub fn <a href="#method.getattr" class="fn">getattr</a><N>(&self, attr_name: N) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr><div class="where">where
N: IntoPy<Py<PyString>>,</div></h4></section></summary><div class="docblock"><p>Retrieves an attribute value.</p>
<p>This is equivalent to the Python expression <code>self.attr_name</code>.</p>
<p>To avoid repeated temporary allocations of Python strings, the [<code>intern!</code>] macro can be used
to intern <code>attr_name</code>.</p>
<h5 id="example-interning-the-attribute-name-1"><a href="#example-interning-the-attribute-name-1">Example: <code>intern!</code>ing the attribute name</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[pyfunction]
</span><span class="kw">fn </span>version<<span class="lifetime">'py</span>>(sys: <span class="kw-2">&</span>Bound<<span class="lifetime">'py</span>, PyModule>) -> PyResult<Bound<<span class="lifetime">'py</span>, PyAny>> {
sys.getattr(<span class="macro">intern!</span>(sys.py(), <span class="string">"version"</span>))
}</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.setattr" class="method"><h4 class="code-header">pub fn <a href="#method.setattr" class="fn">setattr</a><N, V>(&self, attr_name: N, value: V) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>, PyErr><div class="where">where
N: IntoPy<Py<PyString>>,
V: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Sets an attribute value.</p>
<p>This is equivalent to the Python expression <code>self.attr_name = value</code>.</p>
<p>To avoid repeated temporary allocations of Python strings, the [<code>intern!</code>] macro can be used
to intern <code>name</code>.</p>
<h5 id="example-interning-the-attribute-name-2"><a href="#example-interning-the-attribute-name-2">Example: <code>intern!</code>ing the attribute name</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[pyfunction]
</span><span class="kw">fn </span>set_answer(ob: <span class="kw-2">&</span>Bound<<span class="lifetime">'_</span>, PyAny>) -> PyResult<()> {
ob.setattr(<span class="macro">intern!</span>(ob.py(), <span class="string">"answer"</span>), <span class="number">42</span>)
}</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.delattr" class="method"><h4 class="code-header">pub fn <a href="#method.delattr" class="fn">delattr</a><N>(&self, attr_name: N) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>, PyErr><div class="where">where
N: IntoPy<Py<PyString>>,</div></h4></section></summary><div class="docblock"><p>Deletes an attribute.</p>
<p>This is equivalent to the Python statement <code>del self.attr_name</code>.</p>
<p>To avoid repeated temporary allocations of Python strings, the [<code>intern!</code>] macro can be used
to intern <code>attr_name</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.compare" class="method"><h4 class="code-header">pub fn <a href="#method.compare" class="fn">compare</a><O>(&self, other: O) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="enum" href="https://doc.rust-lang.org/1.76.0/core/cmp/enum.Ordering.html" title="enum core::cmp::Ordering">Ordering</a>, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Returns an <a href="https://doc.rust-lang.org/1.76.0/core/cmp/enum.Ordering.html" title="enum core::cmp::Ordering"><code>Ordering</code></a> between <code>self</code> and <code>other</code>.</p>
<p>This is equivalent to the following Python code:</p>
<div class="example-wrap"><pre class="language-python"><code>if self == other:
return Equal
elif a < b:
return Less
elif a > b:
return Greater
else:
raise TypeError("PyAny::compare(): All comparisons returned false")
</code></pre></div><h5 id="examples"><a href="#examples">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::PyFloat;
<span class="kw">use </span>std::cmp::Ordering;
Python::with_gil(|py| -> PyResult<()> {
<span class="kw">let </span>a = PyFloat::new_bound(py, <span class="number">0_f64</span>);
<span class="kw">let </span>b = PyFloat::new_bound(py, <span class="number">42_f64</span>);
<span class="macro">assert_eq!</span>(a.compare(b)<span class="question-mark">?</span>, Ordering::Less);
<span class="prelude-val">Ok</span>(())
})<span class="question-mark">?</span>;</code></pre></div>
<p>It will return <code>PyErr</code> for values that cannot be compared:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::{PyFloat, PyString};
Python::with_gil(|py| -> PyResult<()> {
<span class="kw">let </span>a = PyFloat::new_bound(py, <span class="number">0_f64</span>);
<span class="kw">let </span>b = PyString::new_bound(py, <span class="string">"zero"</span>);
<span class="macro">assert!</span>(a.compare(b).is_err());
<span class="prelude-val">Ok</span>(())
})<span class="question-mark">?</span>;</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.rich_compare" class="method"><h4 class="code-header">pub fn <a href="#method.rich_compare" class="fn">rich_compare</a><O>(
&self,
other: O,
compare_op: CompareOp
) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Tests whether two Python objects obey a given [<code>CompareOp</code>].</p>
<p><a href="Self::lt"><code>lt</code></a>, <a href="Self::le"><code>le</code></a>, <a href="Self::eq"><code>eq</code></a>, <a href="Self::ne"><code>ne</code></a>,
<a href="Self::gt"><code>gt</code></a> and <a href="Self::ge"><code>ge</code></a> are the specialized versions
of this function.</p>
<p>Depending on the value of <code>compare_op</code>, this is equivalent to one of the
following Python expressions:</p>
<div><table><thead><tr><th style="text-align: center"><code>compare_op</code></th><th style="text-align: center">Python expression</th></tr></thead><tbody>
<tr><td style="text-align: center">[<code>CompareOp::Eq</code>]</td><td style="text-align: center"><code>self == other</code></td></tr>
<tr><td style="text-align: center">[<code>CompareOp::Ne</code>]</td><td style="text-align: center"><code>self != other</code></td></tr>
<tr><td style="text-align: center">[<code>CompareOp::Lt</code>]</td><td style="text-align: center"><code>self < other</code></td></tr>
<tr><td style="text-align: center">[<code>CompareOp::Le</code>]</td><td style="text-align: center"><code>self <= other</code></td></tr>
<tr><td style="text-align: center">[<code>CompareOp::Gt</code>]</td><td style="text-align: center"><code>self > other</code></td></tr>
<tr><td style="text-align: center">[<code>CompareOp::Ge</code>]</td><td style="text-align: center"><code>self >= other</code></td></tr>
</tbody></table>
</div><h5 id="examples-1"><a href="#examples-1">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::class::basic::CompareOp;
<span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::PyInt;
Python::with_gil(|py| -> PyResult<()> {
<span class="kw">let </span>a: Bound<<span class="lifetime">'_</span>, PyInt> = <span class="number">0_u8</span>.into_py(py).into_bound(py).downcast_into()<span class="question-mark">?</span>;
<span class="kw">let </span>b: Bound<<span class="lifetime">'_</span>, PyInt> = <span class="number">42_u8</span>.into_py(py).into_bound(py).downcast_into()<span class="question-mark">?</span>;
<span class="macro">assert!</span>(a.rich_compare(b, CompareOp::Le)<span class="question-mark">?</span>.is_truthy()<span class="question-mark">?</span>);
<span class="prelude-val">Ok</span>(())
})<span class="question-mark">?</span>;</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.lt" class="method"><h4 class="code-header">pub fn <a href="#method.lt" class="fn">lt</a><O>(&self, other: O) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Tests whether this object is less than another.</p>
<p>This is equivalent to the Python expression <code>self < other</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.le" class="method"><h4 class="code-header">pub fn <a href="#method.le" class="fn">le</a><O>(&self, other: O) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Tests whether this object is less than or equal to another.</p>
<p>This is equivalent to the Python expression <code>self <= other</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.eq" class="method"><h4 class="code-header">pub fn <a href="#method.eq" class="fn">eq</a><O>(&self, other: O) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Tests whether this object is equal to another.</p>
<p>This is equivalent to the Python expression <code>self == other</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.ne" class="method"><h4 class="code-header">pub fn <a href="#method.ne" class="fn">ne</a><O>(&self, other: O) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Tests whether this object is not equal to another.</p>
<p>This is equivalent to the Python expression <code>self != other</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.gt" class="method"><h4 class="code-header">pub fn <a href="#method.gt" class="fn">gt</a><O>(&self, other: O) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Tests whether this object is greater than another.</p>
<p>This is equivalent to the Python expression <code>self > other</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.ge" class="method"><h4 class="code-header">pub fn <a href="#method.ge" class="fn">ge</a><O>(&self, other: O) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr><div class="where">where
O: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Tests whether this object is greater than or equal to another.</p>
<p>This is equivalent to the Python expression <code>self >= other</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_callable" class="method"><h4 class="code-header">pub fn <a href="#method.is_callable" class="fn">is_callable</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Determines whether this object appears callable.</p>
<p>This is equivalent to Python’s <a href="https://docs.python.org/3/library/functions.html#callable"><code>callable()</code></a> function.</p>
<h5 id="examples-2"><a href="#examples-2">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
Python::with_gil(|py| -> PyResult<()> {
<span class="kw">let </span>builtins = PyModule::import_bound(py, <span class="string">"builtins"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>print = builtins.getattr(<span class="string">"print"</span>)<span class="question-mark">?</span>;
<span class="macro">assert!</span>(print.is_callable());
<span class="prelude-val">Ok</span>(())
})<span class="question-mark">?</span>;</code></pre></div>
<p>This is equivalent to the Python statement <code>assert callable(print)</code>.</p>
<p>Note that unless an API needs to distinguish between callable and
non-callable objects, there is no point in checking for callability.
Instead, it is better to just do the call and handle potential
exceptions.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.call" class="method"><h4 class="code-header">pub fn <a href="#method.call" class="fn">call</a>(
&self,
args: impl IntoPy<Py<PyTuple>>,
kwargs: <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&PyDict>
) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr></h4></section></summary><div class="docblock"><p>Calls the object.</p>
<p>This is equivalent to the Python expression <code>self(*args, **kwargs)</code>.</p>
<h5 id="examples-3"><a href="#examples-3">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::PyDict;
<span class="kw">const </span>CODE: <span class="kw-2">&</span>str = <span class="string">r#"
def function(*args, **kwargs):
assert args == ("hello",)
assert kwargs == {"cruel": "world"}
return "called with args and kwargs"
"#</span>;
Python::with_gil(|py| {
<span class="kw">let </span>module = PyModule::from_code_bound(py, CODE, <span class="string">""</span>, <span class="string">""</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>fun = module.getattr(<span class="string">"function"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>args = (<span class="string">"hello"</span>,);
<span class="kw">let </span>kwargs = PyDict::new_bound(py);
kwargs.set_item(<span class="string">"cruel"</span>, <span class="string">"world"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>result = fun.call(args, <span class="prelude-val">Some</span>(<span class="kw-2">&</span>kwargs))<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(result.extract::<String>()<span class="question-mark">?</span>, <span class="string">"called with args and kwargs"</span>);
<span class="prelude-val">Ok</span>(())
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.call0" class="method"><h4 class="code-header">pub fn <a href="#method.call0" class="fn">call0</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr></h4></section></summary><div class="docblock"><p>Calls the object without arguments.</p>
<p>This is equivalent to the Python expression <code>self()</code>.</p>
<h5 id="examples-4"><a href="#examples-4">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
Python::with_gil(|py| -> PyResult<()> {
<span class="kw">let </span>module = PyModule::import_bound(py, <span class="string">"builtins"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>help = module.getattr(<span class="string">"help"</span>)<span class="question-mark">?</span>;
help.call0()<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
})<span class="question-mark">?</span>;</code></pre></div>
<p>This is equivalent to the Python expression <code>help()</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.call1" class="method"><h4 class="code-header">pub fn <a href="#method.call1" class="fn">call1</a>(&self, args: impl IntoPy<Py<PyTuple>>) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr></h4></section></summary><div class="docblock"><p>Calls the object with only positional arguments.</p>
<p>This is equivalent to the Python expression <code>self(*args)</code>.</p>
<h5 id="examples-5"><a href="#examples-5">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">const </span>CODE: <span class="kw-2">&</span>str = <span class="string">r#"
def function(*args, **kwargs):
assert args == ("hello",)
assert kwargs == {}
return "called with args"
"#</span>;
Python::with_gil(|py| {
<span class="kw">let </span>module = PyModule::from_code_bound(py, CODE, <span class="string">""</span>, <span class="string">""</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>fun = module.getattr(<span class="string">"function"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>args = (<span class="string">"hello"</span>,);
<span class="kw">let </span>result = fun.call1(args)<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(result.extract::<String>()<span class="question-mark">?</span>, <span class="string">"called with args"</span>);
<span class="prelude-val">Ok</span>(())
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.call_method" class="method"><h4 class="code-header">pub fn <a href="#method.call_method" class="fn">call_method</a><N, A>(
&self,
name: N,
args: A,
kwargs: <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a><&PyDict>
) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr><div class="where">where
N: IntoPy<Py<PyString>>,
A: IntoPy<Py<PyTuple>>,</div></h4></section></summary><div class="docblock"><p>Calls a method on the object.</p>
<p>This is equivalent to the Python expression <code>self.name(*args, **kwargs)</code>.</p>
<p>To avoid repeated temporary allocations of Python strings, the [<code>intern!</code>] macro can be used
to intern <code>name</code>.</p>
<h5 id="examples-6"><a href="#examples-6">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::PyDict;
<span class="kw">const </span>CODE: <span class="kw-2">&</span>str = <span class="string">r#"
class A:
def method(self, *args, **kwargs):
assert args == ("hello",)
assert kwargs == {"cruel": "world"}
return "called with args and kwargs"
a = A()
"#</span>;
Python::with_gil(|py| {
<span class="kw">let </span>module = PyModule::from_code_bound(py, CODE, <span class="string">""</span>, <span class="string">""</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>instance = module.getattr(<span class="string">"a"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>args = (<span class="string">"hello"</span>,);
<span class="kw">let </span>kwargs = PyDict::new_bound(py);
kwargs.set_item(<span class="string">"cruel"</span>, <span class="string">"world"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>result = instance.call_method(<span class="string">"method"</span>, args, <span class="prelude-val">Some</span>(<span class="kw-2">&</span>kwargs))<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(result.extract::<String>()<span class="question-mark">?</span>, <span class="string">"called with args and kwargs"</span>);
<span class="prelude-val">Ok</span>(())
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.call_method0" class="method"><h4 class="code-header">pub fn <a href="#method.call_method0" class="fn">call_method0</a><N>(&self, name: N) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr><div class="where">where
N: IntoPy<Py<PyString>>,</div></h4></section></summary><div class="docblock"><p>Calls a method on the object without arguments.</p>
<p>This is equivalent to the Python expression <code>self.name()</code>.</p>
<p>To avoid repeated temporary allocations of Python strings, the [<code>intern!</code>] macro can be used
to intern <code>name</code>.</p>
<h5 id="examples-7"><a href="#examples-7">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">const </span>CODE: <span class="kw-2">&</span>str = <span class="string">r#"
class A:
def method(self, *args, **kwargs):
assert args == ()
assert kwargs == {}
return "called with no arguments"
a = A()
"#</span>;
Python::with_gil(|py| {
<span class="kw">let </span>module = PyModule::from_code_bound(py, CODE, <span class="string">""</span>, <span class="string">""</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>instance = module.getattr(<span class="string">"a"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>result = instance.call_method0(<span class="string">"method"</span>)<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(result.extract::<String>()<span class="question-mark">?</span>, <span class="string">"called with no arguments"</span>);
<span class="prelude-val">Ok</span>(())
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.call_method1" class="method"><h4 class="code-header">pub fn <a href="#method.call_method1" class="fn">call_method1</a><N, A>(&self, name: N, args: A) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr><div class="where">where
N: IntoPy<Py<PyString>>,
A: IntoPy<Py<PyTuple>>,</div></h4></section></summary><div class="docblock"><p>Calls a method on the object with only positional arguments.</p>
<p>This is equivalent to the Python expression <code>self.name(*args)</code>.</p>
<p>To avoid repeated temporary allocations of Python strings, the [<code>intern!</code>] macro can be used
to intern <code>name</code>.</p>
<h5 id="examples-8"><a href="#examples-8">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">const </span>CODE: <span class="kw-2">&</span>str = <span class="string">r#"
class A:
def method(self, *args, **kwargs):
assert args == ("hello",)
assert kwargs == {}
return "called with args"
a = A()
"#</span>;
Python::with_gil(|py| {
<span class="kw">let </span>module = PyModule::from_code_bound(py, CODE, <span class="string">""</span>, <span class="string">""</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>instance = module.getattr(<span class="string">"a"</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>args = (<span class="string">"hello"</span>,);
<span class="kw">let </span>result = instance.call_method1(<span class="string">"method"</span>, args)<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(result.extract::<String>()<span class="question-mark">?</span>, <span class="string">"called with args"</span>);
<span class="prelude-val">Ok</span>(())
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_true" class="method"><h4 class="code-header">pub fn <a href="#method.is_true" class="fn">is_true</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.21.0: use <code>.is_truthy()</code> instead</span></div></span></summary><div class="docblock"><p>Returns whether the object is considered to be true.</p>
<p>This is equivalent to the Python expression <code>bool(self)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_truthy" class="method"><h4 class="code-header">pub fn <a href="#method.is_truthy" class="fn">is_truthy</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr></h4></section></summary><div class="docblock"><p>Returns whether the object is considered to be true.</p>
<p>This applies truth value testing equivalent to the Python expression <code>bool(self)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_none" class="method"><h4 class="code-header">pub fn <a href="#method.is_none" class="fn">is_none</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns whether the object is considered to be None.</p>
<p>This is equivalent to the Python expression <code>self is None</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_ellipsis" class="method"><h4 class="code-header">pub fn <a href="#method.is_ellipsis" class="fn">is_ellipsis</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.20.0: use <code>.is(py.Ellipsis())</code> instead</span></div></span></summary><div class="docblock"><p>Returns whether the object is Ellipsis, e.g. <code>...</code>.</p>
<p>This is equivalent to the Python expression <code>self is ...</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_empty-1" class="method"><h4 class="code-header">pub fn <a href="#method.is_empty-1" class="fn">is_empty</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr></h4></section></summary><div class="docblock"><p>Returns true if the sequence or mapping has a length of 0.</p>
<p>This is equivalent to the Python expression <code>len(self) == 0</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_item" class="method"><h4 class="code-header">pub fn <a href="#method.get_item" class="fn">get_item</a><K>(&self, key: K) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyAny, PyErr><div class="where">where
K: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Gets an item from the collection.</p>
<p>This is equivalent to the Python expression <code>self[key]</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.set_item" class="method"><h4 class="code-header">pub fn <a href="#method.set_item" class="fn">set_item</a><K, V>(&self, key: K, value: V) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>, PyErr><div class="where">where
K: ToPyObject,
V: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Sets a collection item value.</p>
<p>This is equivalent to the Python expression <code>self[key] = value</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.del_item" class="method"><h4 class="code-header">pub fn <a href="#method.del_item" class="fn">del_item</a><K>(&self, key: K) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>, PyErr><div class="where">where
K: ToPyObject,</div></h4></section></summary><div class="docblock"><p>Deletes an item from the collection.</p>
<p>This is equivalent to the Python expression <code>del self[key]</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.iter" class="method"><h4 class="code-header">pub fn <a href="#method.iter" class="fn">iter</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyIterator, PyErr></h4></section></summary><div class="docblock"><p>Takes an object and returns an iterator for it.</p>
<p>This is typically a new iterator but if the argument is an iterator,
this returns itself.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_type" class="method"><h4 class="code-header">pub fn <a href="#method.get_type" class="fn">get_type</a>(&self) -> &PyType</h4></section></summary><div class="docblock"><p>Returns the Python type object for this object’s type.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_type_ptr" class="method"><h4 class="code-header">pub fn <a href="#method.get_type_ptr" class="fn">get_type_ptr</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.pointer.html">*mut </a>PyTypeObject</h4></section></summary><div class="docblock"><p>Returns the Python type pointer for this object.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.downcast" class="method"><h4 class="code-header">pub fn <a href="#method.downcast" class="fn">downcast</a><T>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&T</a>, PyDowncastError<'_>><div class="where">where
T: PyTypeCheck<AsRefTarget = T>,</div></h4></section></summary><div class="docblock"><p>Downcast this <code>PyAny</code> to a concrete Python type or pyclass.</p>
<p>Note that you can often avoid downcasting yourself by just specifying
the desired type in function or method signatures.
However, manual downcasting is sometimes necessary.</p>
<p>For extracting a Rust-only type, see <a href="struct.PyAny.html#method.extract"><code>PyAny::extract</code></a>.</p>
<h5 id="example-downcasting-to-a-specific-python-object"><a href="#example-downcasting-to-a-specific-python-object">Example: Downcasting to a specific Python object</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::{PyDict, PyList};
Python::with_gil(|py| {
<span class="kw">let </span>dict = PyDict::new_bound(py);
<span class="macro">assert!</span>(dict.is_instance_of::<PyAny>());
<span class="kw">let </span>any = dict.as_any();
<span class="macro">assert!</span>(any.downcast::<PyDict>().is_ok());
<span class="macro">assert!</span>(any.downcast::<PyList>().is_err());
});</code></pre></div>
<h5 id="example-getting-a-reference-to-a-pyclass"><a href="#example-getting-a-reference-to-a-pyclass">Example: Getting a reference to a pyclass</a></h5>
<p>This is useful if you want to mutate a <code>PyObject</code> that
might actually be a pyclass.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pyclass]
</span><span class="kw">struct </span>Class {
i: i32,
}
Python::with_gil(|py| {
<span class="kw">let </span>class = Py::new(py, Class { i: <span class="number">0 </span>}).unwrap().into_bound(py).into_any();
<span class="kw">let </span>class_bound: <span class="kw-2">&</span>Bound<<span class="lifetime">'_</span>, Class> = class.downcast()<span class="question-mark">?</span>;
class_bound.borrow_mut().i += <span class="number">1</span>;
<span class="comment">// Alternatively you can get a `PyRefMut` directly
</span><span class="kw">let </span>class_ref: PyRefMut<<span class="lifetime">'_</span>, Class> = class.extract()<span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(class_ref.i, <span class="number">1</span>);
<span class="prelude-val">Ok</span>(())
})</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.downcast_exact" class="method"><h4 class="code-header">pub fn <a href="#method.downcast_exact" class="fn">downcast_exact</a><T>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&T</a>, PyDowncastError<'_>><div class="where">where
T: PyTypeInfo<AsRefTarget = T>,</div></h4></section></summary><div class="docblock"><p>Downcast this <code>PyAny</code> to a concrete Python type or pyclass (but not a subclass of it).</p>
<p>It is almost always better to use [<code>PyAny::downcast</code>] because it accounts for Python
subtyping. Use this method only when you do not want to allow subtypes.</p>
<p>The advantage of this method over [<code>PyAny::downcast</code>] is that it is faster. The implementation
of <code>downcast_exact</code> uses the equivalent of the Python expression <code>type(self) is T</code>, whereas
<code>downcast</code> uses <code>isinstance(self, T)</code>.</p>
<p>For extracting a Rust-only type, see <a href="struct.PyAny.html#method.extract"><code>PyAny::extract</code></a>.</p>
<h5 id="example-downcasting-to-a-specific-python-object-but-not-a-subtype"><a href="#example-downcasting-to-a-specific-python-object-but-not-a-subtype">Example: Downcasting to a specific Python object but not a subtype</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>pyo3::types::{PyBool, PyLong};
Python::with_gil(|py| {
<span class="kw">let </span>b = PyBool::new_bound(py, <span class="bool-val">true</span>);
<span class="macro">assert!</span>(b.is_instance_of::<PyBool>());
<span class="kw">let </span>any: <span class="kw-2">&</span>Bound<<span class="lifetime">'_</span>, PyAny> = b.as_any();
<span class="comment">// `bool` is a subtype of `int`, so `downcast` will accept a `bool` as an `int`
// but `downcast_exact` will not.
</span><span class="macro">assert!</span>(any.downcast::<PyLong>().is_ok());
<span class="macro">assert!</span>(any.downcast_exact::<PyLong>().is_err());
<span class="macro">assert!</span>(any.downcast_exact::<PyBool>().is_ok());
});</code></pre></div>
</div></details><details class="toggle method-toggle" open><summary><section id="method.downcast_unchecked" class="method"><h4 class="code-header">pub unsafe fn <a href="#method.downcast_unchecked" class="fn">downcast_unchecked</a><T>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.reference.html">&T</a><div class="where">where
T: HasPyGilRef<AsRefTarget = T>,</div></h4></section></summary><div class="docblock"><p>Converts this <code>PyAny</code> to a concrete Python type without checking validity.</p>
<h5 id="safety-16"><a href="#safety-16">Safety</a></h5>
<p>Callers must ensure that the type is valid or risk type confusion.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.extract" class="method"><h4 class="code-header">pub fn <a href="#method.extract" class="fn">extract</a><'py, D>(&'py self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><D, PyErr><div class="where">where
D: FromPyObject<'py>,</div></h4></section></summary><div class="docblock"><p>Extracts some type from the Python object.</p>
<p>This is a wrapper function around [<code>FromPyObject::extract()</code>].</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.get_refcnt" class="method"><h4 class="code-header">pub fn <a href="#method.get_refcnt" class="fn">get_refcnt</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.isize.html">isize</a></h4></section></summary><div class="docblock"><p>Returns the reference count for the Python object.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.repr" class="method"><h4 class="code-header">pub fn <a href="#method.repr" class="fn">repr</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyString, PyErr></h4></section></summary><div class="docblock"><p>Computes the “repr” representation of self.</p>
<p>This is equivalent to the Python expression <code>repr(self)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.str" class="method"><h4 class="code-header">pub fn <a href="#method.str" class="fn">str</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><&PyString, PyErr></h4></section></summary><div class="docblock"><p>Computes the “str” representation of self.</p>
<p>This is equivalent to the Python expression <code>str(self)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.hash" class="method"><h4 class="code-header">pub fn <a href="#method.hash" class="fn">hash</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.isize.html">isize</a>, PyErr></h4></section></summary><div class="docblock"><p>Retrieves the hash code of self.</p>
<p>This is equivalent to the Python expression <code>hash(self)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.len-1" class="method"><h4 class="code-header">pub fn <a href="#method.len-1" class="fn">len</a>(&self) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.usize.html">usize</a>, PyErr></h4></section></summary><div class="docblock"><p>Returns the length of the sequence or mapping.</p>
<p>This is equivalent to the Python expression <code>len(self)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.dir" class="method"><h4 class="code-header">pub fn <a href="#method.dir" class="fn">dir</a>(&self) -> &PyList</h4></section></summary><div class="docblock"><p>Returns the list of attributes of this object.</p>
<p>This is equivalent to the Python expression <code>dir(self)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_instance" class="method"><h4 class="code-header">pub fn <a href="#method.is_instance" class="fn">is_instance</a>(&self, ty: &PyAny) -> <a class="enum" href="https://doc.rust-lang.org/1.76.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a>, PyErr></h4></section></summary><div class="docblock"><p>Checks whether this object is an instance of type <code>ty</code>.</p>
<p>This is equivalent to the Python expression <code>isinstance(self, ty)</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_exact_instance" class="method"><h4 class="code-header">pub fn <a href="#method.is_exact_instance" class="fn">is_exact_instance</a>(&self, ty: &PyAny) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Checks whether this object is an instance of exactly type <code>ty</code> (not a subclass).</p>
<p>This is equivalent to the Python expression <code>type(self) is ty</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_instance_of" class="method"><h4 class="code-header">pub fn <a href="#method.is_instance_of" class="fn">is_instance_of</a><T>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a><div class="where">where
T: PyTypeInfo,</div></h4></section></summary><div class="docblock"><p>Checks whether this object is an instance of type <code>T</code>.</p>
<p>This is equivalent to the Python expression <code>isinstance(self, T)</code>,
if the type <code>T</code> is known at compile time.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_exact_instance_of" class="method"><h4 class="code-header">pub fn <a href="#method.is_exact_instance_of" class="fn">is_exact_instance_of</a><T>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.bool.html">bool</a><div class="where">where