-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharrays.interface.html
1004 lines (797 loc) · 58.1 KB
/
arrays.interface.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" data-content_root="../" data-theme="light">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The array interface protocol — NumPy v2.3.dev0 Manual</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "light";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "light";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="../_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link href="../_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=8f2a1f02" />
<link rel="stylesheet" type="text/css" href="../_static/graphviz.css?v=eafc0fe6" />
<link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Vibur" />
<link rel="stylesheet" type="text/css" href="../_static/jupyterlite_sphinx.css?v=2c9f8f05" />
<link rel="stylesheet" type="text/css" href="../_static/sphinx-design.min.css?v=95c83b7e" />
<link rel="stylesheet" type="text/css" href="../_static/numpy.css?v=a1b581f7" />
<!-- So that users can add custom icons -->
<script src="../_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" />
<script src="../_static/documentation_options.js?v=9cbc30f1"></script>
<script src="../_static/doctools.js?v=888ff710"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../_static/copybutton.js?v=30646c52"></script>
<script src="../_static/jupyterlite_sphinx.js?v=96e329c5"></script>
<script src="../_static/design-tabs.js?v=f930bc37"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'reference/arrays.interface';</script>
<script>
DOCUMENTATION_OPTIONS.theme_version = '0.16.1';
DOCUMENTATION_OPTIONS.theme_switcher_json_url = 'https://numpy.org/doc/_static/versions.json';
DOCUMENTATION_OPTIONS.theme_switcher_version_match = 'devdocs';
DOCUMENTATION_OPTIONS.show_version_warning_banner =
true;
</script>
<link rel="icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Datetimes and timedeltas" href="arrays.datetime.html" />
<link rel="prev" title="numpy.ma.MaskedArray.__setmask__" href="generated/numpy.ma.MaskedArray.__setmask__.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="2.3.dev0" />
<meta name="docbuild:last-update" content="Apr 19, 2025"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="light">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="../search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
<div class="bd-header__inner bd-page-width">
<button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation">
<span class="fa-solid fa-bars"></span>
</button>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="../index.html">
<img src="../_static/numpylogo.svg" class="logo__image only-light" alt="NumPy v2.3.dev0 Manual - Home"/>
<img src="../_static/numpylogo_dark.svg" class="logo__image only-dark pst-js-only" alt="NumPy v2.3.dev0 Manual - Home"/>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item ">
<a class="nav-link nav-internal" href="../user/index.html">
User Guide
</a>
</li>
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
API reference
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../building/index.html">
Building from source
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../dev/index.html">
Development
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../release.html">
Release notes
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://numpy.org/numpy-tutorials/">
Learn
</a>
</li>
<li class="nav-item dropdown">
<button class="btn dropdown-toggle nav-item" type="button"
data-bs-toggle="dropdown" aria-expanded="false"
aria-controls="pst-nav-more-links">
More
</button>
<ul id="pst-nav-more-links" class="dropdown-menu">
<li class=" ">
<a class="nav-link dropdown-item nav-external" href="https://numpy.org/neps">
NEPs
</a>
</li>
</ul>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm pst-navbar-icon search-button search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button></div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item">
<div class="version-switcher__container dropdown pst-js-only">
<button id="pst-version-switcher-button-2"
type="button"
class="version-switcher__button btn btn-sm dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-2"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-2"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-2">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page">
<span class="fa-solid fa-outdent"></span>
</button>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item ">
<a class="nav-link nav-internal" href="../user/index.html">
User Guide
</a>
</li>
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
API reference
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../building/index.html">
Building from source
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../dev/index.html">
Development
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../release.html">
Release notes
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://numpy.org/numpy-tutorials/">
Learn
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://numpy.org/neps">
NEPs
</a>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm pst-navbar-icon search-button search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button></div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item">
<div class="version-switcher__container dropdown pst-js-only">
<button id="pst-version-switcher-button-3"
type="button"
class="version-switcher__button btn btn-sm dropdown-toggle"
data-bs-toggle="dropdown"
aria-haspopup="listbox"
aria-controls="pst-version-switcher-list-3"
aria-label="Version switcher list"
>
Choose version <!-- this text may get changed later by javascript -->
<span class="caret"></span>
</button>
<div id="pst-version-switcher-list-3"
class="version-switcher__menu dropdown-menu list-group-flush py-0"
role="listbox" aria-labelledby="pst-version-switcher-button-3">
<!-- dropdown will be populated by javascript on page load -->
</div>
</div></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
aria-label="Section Navigation">
<p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
<div class="bd-toc-item navbar-nav"><ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="module_structure.html">NumPy’s module structure</a></li>
</ul>
<ul class="current nav bd-sidenav">
<li class="toctree-l1 current active has-children"><a class="reference internal" href="arrays.html">Array objects</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="arrays.ndarray.html">The N-dimensional array (<code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code>)</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.scalars.html">Scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.dtypes.html">Data type objects (<code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code>)</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.promotion.html">Data type promotion in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.nditer.html">Iterating over arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.classes.html">Standard array subclasses</a></li>
<li class="toctree-l2"><a class="reference internal" href="maskedarray.html">Masked arrays</a></li>
<li class="toctree-l2 current active"><a class="current reference internal" href="#">The array interface protocol</a></li>
<li class="toctree-l2"><a class="reference internal" href="arrays.datetime.html">Datetimes and timedeltas</a></li>
</ul>
</details></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="ufuncs.html">Universal functions (<code class="xref py py-class docutils literal notranslate"><span class="pre">ufunc</span></code>)</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="routines.html">Routines and objects by topic</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="typing.html">Typing (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.typing</span></code>)</a></li>
<li class="toctree-l1"><a class="reference internal" href="distutils.html">Packaging</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="c-api/index.html">NumPy C-API</a></li>
</ul>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="array_api.html">Array API standard compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="simd/index.html">CPU/SIMD optimizations</a></li>
<li class="toctree-l1"><a class="reference internal" href="thread_safety.html">Thread Safety</a></li>
<li class="toctree-l1"><a class="reference internal" href="global_state.html">Global Configuration Options</a></li>
<li class="toctree-l1"><a class="reference internal" href="security.html">NumPy security</a></li>
<li class="toctree-l1"><a class="reference internal" href="distutils_status_migration.html">Status of <code class="docutils literal notranslate"><span class="pre">numpy.distutils</span></code> and migration advice</a></li>
<li class="toctree-l1"><a class="reference internal" href="distutils_guide.html"><code class="docutils literal notranslate"><span class="pre">numpy.distutils</span></code> user guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="swig.html">NumPy and SWIG</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item">
<div id="ethical-ad-placement"
class="flat"
data-ea-publisher="readthedocs"
data-ea-type="readthedocs-sidebar"
data-ea-manual="true">
</div></div>
</div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb" class="d-print-none">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="../index.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item"><a href="index.html" class="nav-link">NumPy reference</a></li>
<li class="breadcrumb-item"><a href="arrays.html" class="nav-link">Array objects</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">The array interface protocol</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="the-array-interface-protocol">
<span id="arrays-interface"></span><span id="index-0"></span><h1>The array interface protocol<a class="headerlink" href="#the-array-interface-protocol" title="Link to this heading">#</a></h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This page describes the NumPy-specific API for accessing the contents of
a NumPy array from other C extensions. <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-3118/"><strong>PEP 3118</strong></a> –
<a class="reference external" href="https://docs.python.org/3/c-api/buffer.html#c.PyObject_GetBuffer" title="(in Python v3.13)"><code class="xref c c-func docutils literal notranslate"><span class="pre">The</span> <span class="pre">Revised</span> <span class="pre">Buffer</span> <span class="pre">Protocol</span></code></a> introduces
similar, standardized API to Python 2.6 and 3.0 for any extension
module to use. <a class="reference external" href="https://cython.org/">Cython</a>’s buffer array support
uses the <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-3118/"><strong>PEP 3118</strong></a> API; see the <a class="reference external" href="https://github.com/cython/cython/wiki/tutorials-numpy">Cython NumPy
tutorial</a>. Cython provides a way to write code that supports the buffer
protocol with Python versions older than 2.6 because it has a
backward-compatible implementation utilizing the array interface
described here.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">version<span class="colon">:</span></dt>
<dd class="field-odd"><p>3</p>
</dd>
</dl>
<p>The array interface (sometimes called array protocol) was created in
2005 as a means for array-like Python objects to reuse each other’s
data buffers intelligently whenever possible. The homogeneous
N-dimensional array interface is a default mechanism for objects to
share N-dimensional array memory and information. The interface
consists of a Python-side and a C-side using two attributes. Objects
wishing to be considered an N-dimensional array in application code
should support at least one of these attributes. Objects wishing to
support an N-dimensional array in application code should look for at
least one of these attributes and use the information provided
appropriately.</p>
<p>This interface describes homogeneous arrays in the sense that each
item of the array has the same “type”. This type can be very simple
or it can be a quite arbitrary and complicated C-like structure.</p>
<p>There are two ways to use the interface: A Python side and a C-side.
Both are separate attributes.</p>
<section id="python-side">
<h2>Python side<a class="headerlink" href="#python-side" title="Link to this heading">#</a></h2>
<p>This approach to the interface consists of the object having an
<a class="reference internal" href="#object.__array_interface__" title="object.__array_interface__"><code class="xref py py-data docutils literal notranslate"><span class="pre">__array_interface__</span></code></a> attribute.</p>
<dl class="py data">
<dt class="sig sig-object py" id="object.__array_interface__">
<span class="sig-prename descclassname"><span class="pre">object.</span></span><span class="sig-name descname"><span class="pre">__array_interface__</span></span><a class="headerlink" href="#object.__array_interface__" title="Link to this definition">#</a></dt>
<dd><p>A dictionary of items (3 required and 5 optional). The optional
keys in the dictionary have implied defaults if they are not
provided.</p>
<p>The keys are:</p>
<dl>
<dt><strong>shape</strong> (required)</dt><dd><p>Tuple whose elements are the array size in each dimension. Each
entry is an integer (a Python <a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>). Note that these
integers could be larger than the platform <code class="docutils literal notranslate"><span class="pre">int</span></code> or <code class="docutils literal notranslate"><span class="pre">long</span></code>
could hold (a Python <a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> is a C <code class="docutils literal notranslate"><span class="pre">long</span></code>). It is up to the code
using this attribute to handle this appropriately; either by
raising an error when overflow is possible, or by using
<code class="docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code> as the C type for the shapes.</p>
</dd>
<dt><strong>typestr</strong> (required)</dt><dd><p>A string providing the basic type of the homogeneous array The
basic string format consists of 3 parts: a character describing
the byteorder of the data (<code class="docutils literal notranslate"><span class="pre"><</span></code>: little-endian, <code class="docutils literal notranslate"><span class="pre">></span></code>:
big-endian, <code class="docutils literal notranslate"><span class="pre">|</span></code>: not-relevant), a character code giving the
basic type of the array, and an integer providing the number of
bytes the type uses.</p>
<p>The basic type character codes are:</p>
<div class="pst-scrollable-table-container"><table class="table">
<tbody>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">t</span></code></p></td>
<td><p>Bit field (following integer gives the number of
bits in the bit field).</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">b</span></code></p></td>
<td><p>Boolean (integer type where all values are only <code class="docutils literal notranslate"><span class="pre">True</span></code> or
<code class="docutils literal notranslate"><span class="pre">False</span></code>)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">i</span></code></p></td>
<td><p>Integer</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">u</span></code></p></td>
<td><p>Unsigned integer</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">f</span></code></p></td>
<td><p>Floating point</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">c</span></code></p></td>
<td><p>Complex floating point</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">m</span></code></p></td>
<td><p>Timedelta</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">M</span></code></p></td>
<td><p>Datetime</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">O</span></code></p></td>
<td><p>Object (i.e. the memory contains a pointer to <a class="reference external" href="https://docs.python.org/3/c-api/structures.html#c.PyObject" title="(in Python v3.13)"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a>)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">S</span></code></p></td>
<td><p>String (fixed-length sequence of char)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">U</span></code></p></td>
<td><p>Unicode (fixed-length sequence of <a class="reference external" href="https://docs.python.org/3/c-api/unicode.html#c.Py_UCS4" title="(in Python v3.13)"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UCS4</span></code></a>)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">V</span></code></p></td>
<td><p>Other (void * – each item is a fixed-size chunk of memory)</p></td>
</tr>
</tbody>
</table>
</div>
</dd>
<dt><strong>descr</strong> (optional)</dt><dd><p>A list of tuples providing a more detailed description of the
memory layout for each item in the homogeneous array. Each
tuple in the list has two or three elements. Normally, this
attribute would be used when <em>typestr</em> is <code class="docutils literal notranslate"><span class="pre">V[0-9]+</span></code>, but this is
not a requirement. The only requirement is that the number of
bytes represented in the <em>typestr</em> key is the same as the total
number of bytes represented here. The idea is to support
descriptions of C-like structs that make up array
elements. The elements of each tuple in the list are</p>
<ol class="arabic simple">
<li><p>A string providing a name associated with this portion of
the datatype. This could also be a tuple of <code class="docutils literal notranslate"><span class="pre">('full</span> <span class="pre">name',</span>
<span class="pre">'basic_name')</span></code> where basic name would be a valid Python
variable name representing the full name of the field.</p></li>
<li><p>Either a basic-type description string as in <em>typestr</em> or
another list (for nested structured types)</p></li>
<li><p>An optional shape tuple providing how many times this part
of the structure should be repeated. No repeats are assumed
if this is not given. Very complicated structures can be
described using this generic interface. Notice, however,
that each element of the array is still of the same
data-type. Some examples of using this interface are given
below.</p></li>
</ol>
<p><strong>Default</strong>: <code class="docutils literal notranslate"><span class="pre">[('',</span> <span class="pre">typestr)]</span></code></p>
</dd>
<dt><strong>data</strong> (optional)</dt><dd><p>A 2-tuple whose first argument is a <a class="reference external" href="https://docs.python.org/3/c-api/long.html" title="(in Python v3.13)"><span class="xref std std-doc">Python integer</span></a>
that points to the data-area storing the array contents.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When converting from C/C++ via <code class="docutils literal notranslate"><span class="pre">PyLong_From*</span></code> or high-level
bindings such as Cython or pybind11, make sure to use an integer
of sufficiently large bitness.</p>
</div>
<p>This pointer must point to the first element of
data (in other words any offset is always ignored in this
case). The second entry in the tuple is a read-only flag (true
means the data area is read-only).</p>
<p>This attribute can also be an object exposing the
<a class="reference external" href="https://docs.python.org/3/c-api/buffer.html#bufferobjects" title="(in Python v3.13)"><span class="xref std std-ref">buffer interface</span></a> which
will be used to share the data. If this key is not present (or
returns None), then memory sharing will be done
through the buffer interface of the object itself. In this
case, the offset key can be used to indicate the start of the
buffer. A reference to the object exposing the array interface
must be stored by the new object if the memory area is to be
secured.</p>
<p><strong>Default</strong>: <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
</dd>
<dt><strong>strides</strong> (optional)</dt><dd><p>Either <code class="docutils literal notranslate"><span class="pre">None</span></code> to indicate a C-style contiguous array or
a tuple of strides which provides the number of bytes needed
to jump to the next array element in the corresponding
dimension. Each entry must be an integer (a Python
<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>). As with shape, the values may
be larger than can be represented by a C <code class="docutils literal notranslate"><span class="pre">int</span></code> or <code class="docutils literal notranslate"><span class="pre">long</span></code>; the
calling code should handle this appropriately, either by
raising an error, or by using <code class="docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code> in C. The
default is <code class="docutils literal notranslate"><span class="pre">None</span></code> which implies a C-style contiguous
memory buffer. In this model, the last dimension of the array
varies the fastest. For example, the default strides tuple
for an object whose array entries are 8 bytes long and whose
shape is <code class="docutils literal notranslate"><span class="pre">(10,</span> <span class="pre">20,</span> <span class="pre">30)</span></code> would be <code class="docutils literal notranslate"><span class="pre">(4800,</span> <span class="pre">240,</span> <span class="pre">8)</span></code>.</p>
<p><strong>Default</strong>: <code class="docutils literal notranslate"><span class="pre">None</span></code> (C-style contiguous)</p>
</dd>
<dt><strong>mask</strong> (optional)</dt><dd><p><code class="docutils literal notranslate"><span class="pre">None</span></code> or an object exposing the array interface. All
elements of the mask array should be interpreted only as true
or not true indicating which elements of this array are valid.
The shape of this object should be <em class="xref py py-obj">“broadcastable”</em> to the shape of the
original array.</p>
<p><strong>Default</strong>: <code class="docutils literal notranslate"><span class="pre">None</span></code> (All array values are valid)</p>
</dd>
<dt><strong>offset</strong> (optional)</dt><dd><p>An integer offset into the array data region. This can only be
used when data is <code class="docutils literal notranslate"><span class="pre">None</span></code> or returns a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#memoryview" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a>
object.</p>
<p><strong>Default</strong>: <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
</dd>
<dt><strong>version</strong> (required)</dt><dd><p>An integer showing the version of the interface (i.e. 3 for
this version). Be careful not to use this to invalidate
objects exposing future versions of the interface.</p>
</dd>
</dl>
</dd></dl>
</section>
<section id="c-struct-access">
<h2>C-struct access<a class="headerlink" href="#c-struct-access" title="Link to this heading">#</a></h2>
<p>This approach to the array interface allows for faster access to an
array using only one attribute lookup and a well-defined C-structure.</p>
<dl class="py data">
<dt class="sig sig-object py" id="object.__array_struct__">
<span class="sig-prename descclassname"><span class="pre">object.</span></span><span class="sig-name descname"><span class="pre">__array_struct__</span></span><a class="headerlink" href="#object.__array_struct__" title="Link to this definition">#</a></dt>
<dd><p>A <a class="reference external" href="https://docs.python.org/3/c-api/capsule.html#c.PyCapsule" title="(in Python v3.13)"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a> whose <code class="docutils literal notranslate"><span class="pre">pointer</span></code> member contains a
pointer to a filled <a class="reference internal" href="c-api/types-and-structures.html#c.PyArrayInterface" title="PyArrayInterface"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyArrayInterface</span></code></a> structure. Memory
for the structure is dynamically created and the <a class="reference external" href="https://docs.python.org/3/c-api/capsule.html#c.PyCapsule" title="(in Python v3.13)"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a>
is also created with an appropriate destructor so the retriever of
this attribute simply has to apply <a class="reference external" href="https://docs.python.org/3/c-api/refcounting.html#c.Py_DECREF" title="(in Python v3.13)"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF</span></code></a> to the
object returned by this attribute when it is finished. Also,
either the data needs to be copied out, or a reference to the
object exposing this attribute must be held to ensure the data is
not freed. Objects exposing the <a class="reference internal" href="#object.__array_struct__" title="object.__array_struct__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">__array_struct__</span></code></a> interface
must also not reallocate their memory if other objects are
referencing them.</p>
</dd></dl>
<p>The <a class="reference internal" href="c-api/types-and-structures.html#c.PyArrayInterface" title="PyArrayInterface"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyArrayInterface</span></code></a> structure is defined in <code class="docutils literal notranslate"><span class="pre">numpy/ndarrayobject.h</span></code>
as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">typedef</span> <span class="n">struct</span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">two</span><span class="p">;</span> <span class="o">/*</span> <span class="n">contains</span> <span class="n">the</span> <span class="n">integer</span> <span class="mi">2</span> <span class="o">--</span> <span class="n">simple</span> <span class="n">sanity</span> <span class="n">check</span> <span class="o">*/</span>
<span class="nb">int</span> <span class="n">nd</span><span class="p">;</span> <span class="o">/*</span> <span class="n">number</span> <span class="n">of</span> <span class="n">dimensions</span> <span class="o">*/</span>
<span class="n">char</span> <span class="n">typekind</span><span class="p">;</span> <span class="o">/*</span> <span class="n">kind</span> <span class="ow">in</span> <span class="n">array</span> <span class="o">---</span> <span class="n">character</span> <span class="n">code</span> <span class="n">of</span> <span class="n">typestr</span> <span class="o">*/</span>
<span class="nb">int</span> <span class="n">itemsize</span><span class="p">;</span> <span class="o">/*</span> <span class="n">size</span> <span class="n">of</span> <span class="n">each</span> <span class="n">element</span> <span class="o">*/</span>
<span class="nb">int</span> <span class="n">flags</span><span class="p">;</span> <span class="o">/*</span> <span class="n">flags</span> <span class="n">indicating</span> <span class="n">how</span> <span class="n">the</span> <span class="n">data</span> <span class="n">should</span> <span class="n">be</span> <span class="n">interpreted</span> <span class="o">*/</span>
<span class="o">/*</span> <span class="n">must</span> <span class="nb">set</span> <span class="n">ARR_HAS_DESCR</span> <span class="n">bit</span> <span class="n">to</span> <span class="n">validate</span> <span class="n">descr</span> <span class="o">*/</span>
<span class="n">Py_ssize_t</span> <span class="o">*</span><span class="n">shape</span><span class="p">;</span> <span class="o">/*</span> <span class="n">A</span> <span class="n">length</span><span class="o">-</span><span class="n">nd</span> <span class="n">array</span> <span class="n">of</span> <span class="n">shape</span> <span class="n">information</span> <span class="o">*/</span>
<span class="n">Py_ssize_t</span> <span class="o">*</span><span class="n">strides</span><span class="p">;</span> <span class="o">/*</span> <span class="n">A</span> <span class="n">length</span><span class="o">-</span><span class="n">nd</span> <span class="n">array</span> <span class="n">of</span> <span class="n">stride</span> <span class="n">information</span> <span class="o">*/</span>
<span class="n">void</span> <span class="o">*</span><span class="n">data</span><span class="p">;</span> <span class="o">/*</span> <span class="n">A</span> <span class="n">pointer</span> <span class="n">to</span> <span class="n">the</span> <span class="n">first</span> <span class="n">element</span> <span class="n">of</span> <span class="n">the</span> <span class="n">array</span> <span class="o">*/</span>
<span class="n">PyObject</span> <span class="o">*</span><span class="n">descr</span><span class="p">;</span> <span class="o">/*</span> <span class="n">NULL</span> <span class="ow">or</span> <span class="n">data</span><span class="o">-</span><span class="n">description</span> <span class="p">(</span><span class="n">same</span> <span class="k">as</span> <span class="n">descr</span> <span class="n">key</span>
<span class="n">of</span> <span class="n">__array_interface__</span><span class="p">)</span> <span class="o">--</span> <span class="n">must</span> <span class="nb">set</span> <span class="n">ARR_HAS_DESCR</span>
<span class="n">flag</span> <span class="ow">or</span> <span class="n">this</span> <span class="n">will</span> <span class="n">be</span> <span class="n">ignored</span><span class="o">.</span> <span class="o">*/</span>
<span class="p">}</span> <span class="n">PyArrayInterface</span><span class="p">;</span>
</pre></div>
</div>
<p>The flags member may consist of 5 bits showing how the data should be
interpreted and one bit showing how the Interface should be
interpreted. The data-bits are <a class="reference internal" href="c-api/array.html#c.NPY_ARRAY_C_CONTIGUOUS" title="NPY_ARRAY_C_CONTIGUOUS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">NPY_ARRAY_C_CONTIGUOUS</span></code></a> (0x1),
<a class="reference internal" href="c-api/array.html#c.NPY_ARRAY_F_CONTIGUOUS" title="NPY_ARRAY_F_CONTIGUOUS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">NPY_ARRAY_F_CONTIGUOUS</span></code></a> (0x2), <a class="reference internal" href="c-api/array.html#c.NPY_ARRAY_ALIGNED" title="NPY_ARRAY_ALIGNED"><code class="xref c c-macro docutils literal notranslate"><span class="pre">NPY_ARRAY_ALIGNED</span></code></a> (0x100),
<a class="reference internal" href="c-api/array.html#c.NPY_ARRAY_NOTSWAPPED" title="NPY_ARRAY_NOTSWAPPED"><code class="xref c c-macro docutils literal notranslate"><span class="pre">NPY_ARRAY_NOTSWAPPED</span></code></a> (0x200), and <a class="reference internal" href="c-api/array.html#c.NPY_ARRAY_WRITEABLE" title="NPY_ARRAY_WRITEABLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">NPY_ARRAY_WRITEABLE</span></code></a> (0x400). A final flag
<a class="reference internal" href="#c.NPY_ARR_HAS_DESCR" title="NPY_ARR_HAS_DESCR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">NPY_ARR_HAS_DESCR</span></code></a> (0x800) indicates whether or not this structure
has the arrdescr field. The field should not be accessed unless this
flag is present.</p>
<dl class="c macro">
<dt class="sig sig-object c" id="c.NPY_ARR_HAS_DESCR">
<span class="sig-name descname"><span class="n"><span class="pre">NPY_ARR_HAS_DESCR</span></span></span><a class="headerlink" href="#c.NPY_ARR_HAS_DESCR" title="Link to this definition">#</a><br /></dt>
<dd></dd></dl>
<div class="admonition-new-since-june-16-2006 admonition">
<p class="admonition-title">New since June 16, 2006:</p>
<p>In the past most implementations used the <code class="docutils literal notranslate"><span class="pre">desc</span></code> member of the <code class="docutils literal notranslate"><span class="pre">PyCObject</span></code>
(now <a class="reference external" href="https://docs.python.org/3/c-api/capsule.html#c.PyCapsule" title="(in Python v3.13)"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a>) itself (do not confuse this with the “descr” member of
the <a class="reference internal" href="c-api/types-and-structures.html#c.PyArrayInterface" title="PyArrayInterface"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyArrayInterface</span></code></a> structure above — they are two separate
things) to hold the pointer to the object exposing the interface.
This is now an explicit part of the interface. Be sure to take a
reference to the object and call <a class="reference external" href="https://docs.python.org/3/c-api/capsule.html#c.PyCapsule_SetContext" title="(in Python v3.13)"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_SetContext</span></code></a> before
returning the <a class="reference external" href="https://docs.python.org/3/c-api/capsule.html#c.PyCapsule" title="(in Python v3.13)"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a>, and configure a destructor to decref this
reference.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><a class="reference internal" href="#object.__array_struct__" title="object.__array_struct__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">__array_struct__</span></code></a> is considered legacy and should not be used for new
code. Use the <a class="reference external" href="https://docs.python.org/3/c-api/buffer.html" title="(in Python v3.13)"><span class="xref std std-doc">buffer protocol</span></a> or the DLPack protocol
<a class="reference internal" href="generated/numpy.from_dlpack.html#numpy.from_dlpack" title="numpy.from_dlpack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.from_dlpack</span></code></a> instead.</p>
</div>
</section>
<section id="type-description-examples">
<h2>Type description examples<a class="headerlink" href="#type-description-examples" title="Link to this heading">#</a></h2>
<p>For clarity it is useful to provide some examples of the type
description and corresponding <a class="reference internal" href="#object.__array_interface__" title="object.__array_interface__"><code class="xref py py-data docutils literal notranslate"><span class="pre">__array_interface__</span></code></a> ‘descr’
entries. Thanks to Scott Gilbert for these examples:</p>
<p>In every case, the ‘descr’ key is optional, but of course provides
more information which may be important for various applications:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">*</span> <span class="n">Float</span> <span class="n">data</span>
<span class="n">typestr</span> <span class="o">==</span> <span class="s1">'>f4'</span>
<span class="n">descr</span> <span class="o">==</span> <span class="p">[(</span><span class="s1">''</span><span class="p">,</span><span class="s1">'>f4'</span><span class="p">)]</span>
<span class="o">*</span> <span class="n">Complex</span> <span class="n">double</span>
<span class="n">typestr</span> <span class="o">==</span> <span class="s1">'>c8'</span>
<span class="n">descr</span> <span class="o">==</span> <span class="p">[(</span><span class="s1">'real'</span><span class="p">,</span><span class="s1">'>f4'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'imag'</span><span class="p">,</span><span class="s1">'>f4'</span><span class="p">)]</span>
<span class="o">*</span> <span class="n">RGB</span> <span class="n">Pixel</span> <span class="n">data</span>
<span class="n">typestr</span> <span class="o">==</span> <span class="s1">'|V3'</span>
<span class="n">descr</span> <span class="o">==</span> <span class="p">[(</span><span class="s1">'r'</span><span class="p">,</span><span class="s1">'|u1'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'g'</span><span class="p">,</span><span class="s1">'|u1'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'b'</span><span class="p">,</span><span class="s1">'|u1'</span><span class="p">)]</span>
<span class="o">*</span> <span class="n">Mixed</span> <span class="n">endian</span> <span class="p">(</span><span class="n">weird</span> <span class="n">but</span> <span class="n">could</span> <span class="n">happen</span><span class="p">)</span><span class="o">.</span>
<span class="n">typestr</span> <span class="o">==</span> <span class="s1">'|V8'</span> <span class="p">(</span><span class="ow">or</span> <span class="s1">'>u8'</span><span class="p">)</span>
<span class="n">descr</span> <span class="o">==</span> <span class="p">[(</span><span class="s1">'big'</span><span class="p">,</span><span class="s1">'>i4'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'little'</span><span class="p">,</span><span class="s1">'<i4'</span><span class="p">)]</span>
<span class="o">*</span> <span class="n">Nested</span> <span class="n">structure</span>
<span class="n">struct</span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">ival</span><span class="p">;</span>
<span class="n">struct</span> <span class="p">{</span>
<span class="n">unsigned</span> <span class="n">short</span> <span class="n">sval</span><span class="p">;</span>
<span class="n">unsigned</span> <span class="n">char</span> <span class="n">bval</span><span class="p">;</span>
<span class="n">unsigned</span> <span class="n">char</span> <span class="n">cval</span><span class="p">;</span>
<span class="p">}</span> <span class="n">sub</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">typestr</span> <span class="o">==</span> <span class="s1">'|V8'</span> <span class="p">(</span><span class="ow">or</span> <span class="s1">'<u8'</span> <span class="k">if</span> <span class="n">you</span> <span class="n">want</span><span class="p">)</span>
<span class="n">descr</span> <span class="o">==</span> <span class="p">[(</span><span class="s1">'ival'</span><span class="p">,</span><span class="s1">'<i4'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'sub'</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'sval'</span><span class="p">,</span><span class="s1">'<u2'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'bval'</span><span class="p">,</span><span class="s1">'|u1'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'cval'</span><span class="p">,</span><span class="s1">'|u1'</span><span class="p">)</span> <span class="p">])</span> <span class="p">]</span>
<span class="o">*</span> <span class="n">Nested</span> <span class="n">array</span>
<span class="n">struct</span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">ival</span><span class="p">;</span>
<span class="n">double</span> <span class="n">data</span><span class="p">[</span><span class="mi">16</span><span class="o">*</span><span class="mi">4</span><span class="p">];</span>
<span class="p">}</span>
<span class="n">typestr</span> <span class="o">==</span> <span class="s1">'|V516'</span>
<span class="n">descr</span> <span class="o">==</span> <span class="p">[(</span><span class="s1">'ival'</span><span class="p">,</span><span class="s1">'>i4'</span><span class="p">),</span> <span class="p">(</span><span class="s1">'data'</span><span class="p">,</span><span class="s1">'>f8'</span><span class="p">,(</span><span class="mi">16</span><span class="p">,</span><span class="mi">4</span><span class="p">))]</span>
<span class="o">*</span> <span class="n">Padded</span> <span class="n">structure</span>
<span class="n">struct</span> <span class="p">{</span>
<span class="nb">int</span> <span class="n">ival</span><span class="p">;</span>
<span class="n">double</span> <span class="n">dval</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">typestr</span> <span class="o">==</span> <span class="s1">'|V16'</span>
<span class="n">descr</span> <span class="o">==</span> <span class="p">[(</span><span class="s1">'ival'</span><span class="p">,</span><span class="s1">'>i4'</span><span class="p">),(</span><span class="s1">''</span><span class="p">,</span><span class="s1">'|V4'</span><span class="p">),(</span><span class="s1">'dval'</span><span class="p">,</span><span class="s1">'>f8'</span><span class="p">)]</span>
</pre></div>
</div>
<p>It should be clear that any structured type could be described using this
interface.</p>
</section>
<section id="differences-with-array-interface-version-2">
<h2>Differences with array interface (version 2)<a class="headerlink" href="#differences-with-array-interface-version-2" title="Link to this heading">#</a></h2>
<p>The version 2 interface was very similar. The differences were
largely aesthetic. In particular:</p>
<ol class="arabic">
<li><p>The PyArrayInterface structure had no descr member at the end
(and therefore no flag ARR_HAS_DESCR)</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">context</span></code> member of the <a class="reference external" href="https://docs.python.org/3/c-api/capsule.html#c.PyCapsule" title="(in Python v3.13)"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a> (formally the <code class="docutils literal notranslate"><span class="pre">desc</span></code>
member of the <code class="docutils literal notranslate"><span class="pre">PyCObject</span></code>) returned from <code class="docutils literal notranslate"><span class="pre">__array_struct__</span></code> was
not specified. Usually, it was the object exposing the array (so
that a reference to it could be kept and destroyed when the
C-object was destroyed). It is now an explicit requirement that this field
be used in some way to hold a reference to the owning object.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Until August 2020, this said:</p>
<blockquote>
<div><p>Now it must be a tuple whose first element is a string with
“PyArrayInterface Version #” and whose second element is the object
exposing the array.</p>
</div></blockquote>
<p>This design was retracted almost immediately after it was proposed, in
<<a class="reference external" href="https://mail.python.org/pipermail/numpy-discussion/2006-June/020995.html">https://mail.python.org/pipermail/numpy-discussion/2006-June/020995.html</a>>.
Despite 14 years of documentation to the contrary, at no point was it
valid to assume that <code class="docutils literal notranslate"><span class="pre">__array_interface__</span></code> capsules held this tuple
content.</p>
</div>
</li>
<li><p>The tuple returned from <code class="docutils literal notranslate"><span class="pre">__array_interface__['data']</span></code> used to be a
hex-string (now it is an integer or a long integer).</p></li>
<li><p>There was no <code class="docutils literal notranslate"><span class="pre">__array_interface__</span></code> attribute instead all of the keys
(except for version) in the <code class="docutils literal notranslate"><span class="pre">__array_interface__</span></code> dictionary were
their own attribute: Thus to obtain the Python-side information you
had to access separately the attributes:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">__array_data__</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__array_shape__</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__array_strides__</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__array_typestr__</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__array_descr__</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__array_offset__</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__array_mask__</span></code></p></li>
</ul>
</li>
</ol>
</section>
</section>
</article>
<footer class="prev-next-footer d-print-none">
<div class="prev-next-area">
<a class="left-prev"
href="generated/numpy.ma.MaskedArray.__setmask__.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">numpy.ma.MaskedArray.__setmask__</p>
</div>
</a>
<a class="right-next"
href="arrays.datetime.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Datetimes and timedeltas</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>
</div>
</footer>
</div>
<dialog id="pst-secondary-sidebar-modal"></dialog>
<div id="pst-secondary-sidebar" class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div
id="pst-page-navigation-heading-2"
class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> On this page
</div>
<nav class="bd-toc-nav page-toc" aria-labelledby="pst-page-navigation-heading-2">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#python-side">Python side</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#object.__array_interface__"><code class="docutils literal notranslate"><span class="pre">object.__array_interface__</span></code></a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#c-struct-access">C-struct access</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#object.__array_struct__"><code class="docutils literal notranslate"><span class="pre">object.__array_struct__</span></code></a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#type-description-examples">Type description examples</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#differences-with-array-interface-version-2">Differences with array interface (version 2)</a></li>
</ul>
</nav></div>
</div></div>
</div>
<footer class="bd-footer-content">
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script defer src="../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf"></script>
<script defer src="../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf"></script>
<footer class="bd-footer">
<div class="bd-footer__inner bd-page-width">
<div class="footer-items__start">
<div class="footer-item">
<p class="copyright">
© Copyright 2008-2025, NumPy Developers.
<br/>
</p>
</div>
<div class="footer-item">
<p class="sphinx-version">
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
<br/>
</p>
</div>
</div>
<div class="footer-items__end">
<div class="footer-item">
<p class="theme-version">
<!-- # L10n: Setting the PST URL as an argument as this does not need to be localized -->
Built with the <a href="https://pydata-sphinx-theme.readthedocs.io/en/stable/index.html">PyData Sphinx Theme</a> 0.16.1.
</p></div>
</div>
</div>