forked from python/python-docs-tr
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodules.html
More file actions
722 lines (680 loc) · 70.7 KB
/
modules.html
File metadata and controls
722 lines (680 loc) · 70.7 KB
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
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />
<meta property="og:title" content="6. Modüller" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/tutorial/modules.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="Python yorumlayıcısından çıkıp tekrar girerseniz, yaptığınız tanımlar (fonksiyonlar ve değişkenler) kaybolur. Bu nedenle, daha uzun bir program yazmak istiyorsanız, girdiyi yorumlayıcıya hazırlarke..." />
<meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="Python yorumlayıcısından çıkıp tekrar girerseniz, yaptığınız tanımlar (fonksiyonlar ve değişkenler) kaybolur. Bu nedenle, daha uzun bir program yazmak istiyorsanız, girdiyi yorumlayıcıya hazırlarke..." />
<meta property="og:image:width" content="200" />
<meta property="og:image:height" content="200" />
<meta name="theme-color" content="#3776ab" />
<title>6. Modüller — Python 3.11.5 belgelendirmesi</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Python 3.11.5 belgelendirmesi içinde ara"
href="../_static/opensearch.xml"/>
<link rel="author" title="Bu belgeler hakkında" href="../about.html" />
<link rel="index" title="Dizin" href="../genindex.html" />
<link rel="search" title="Ara" href="../search.html" />
<link rel="copyright" title="Telif Hakkı" href="../copyright.html" />
<link rel="next" title="7. Girdi ve Çıktı" href="inputoutput.html" />
<link rel="prev" title="5. Veri Yapıları" href="datastructures.html" />
<link rel="canonical" href="https://docs.python.org/3/tutorial/modules.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
<input placeholder="Hızlı Arama" aria-label="Hızlı Arama" type="search" name="q" />
<input type="submit" value="Git"/>
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">İçindekiler</a></h3>
<ul>
<li><a class="reference internal" href="#">6. Modüller</a><ul>
<li><a class="reference internal" href="#more-on-modules">6.1. Modüller hakkında daha fazla</a><ul>
<li><a class="reference internal" href="#executing-modules-as-scripts">6.1.1. Modülleri komut dosyası olarak yürütme</a></li>
<li><a class="reference internal" href="#the-module-search-path">6.1.2. Modül Arama Yolu</a></li>
<li><a class="reference internal" href="#compiled-python-files">6.1.3. “Derlenmiş” Python dosyaları</a></li>
</ul>
</li>
<li><a class="reference internal" href="#standard-modules">6.2. Standart modüller</a></li>
<li><a class="reference internal" href="#the-dir-function">6.3. <code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code> Fonksiyonu</a></li>
<li><a class="reference internal" href="#packages">6.4. Paketler</a><ul>
<li><a class="reference internal" href="#importing-from-a-package">6.4.1. Bir Paketten * İçe Aktarma</a></li>
<li><a class="reference internal" href="#intra-package-references">6.4.2. Paket İçi Referanslar</a></li>
<li><a class="reference internal" href="#packages-in-multiple-directories">6.4.3. Birden Çok Dizindeki Paketler</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Önceki konu</h4>
<p class="topless"><a href="datastructures.html"
title="önceki bölüm"><span class="section-number">5. </span>Veri Yapıları</a></p>
</div>
<div>
<h4>Sonraki konu</h4>
<p class="topless"><a href="inputoutput.html"
title="sonraki bölüm"><span class="section-number">7. </span>Girdi ve Çıktı</a></p>
</div>
<div role="note" aria-label="source link">
<h3>Bu Sayfa</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Hata Bildir</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.11/Doc/tutorial/modules.rst"
rel="nofollow">Kaynağı Göster
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Gezinti</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="Genel Endeks"
accesskey="I">dizin</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Modül Dizini"
>modülleri</a> |</li>
<li class="right" >
<a href="inputoutput.html" title="7. Girdi ve Çıktı"
accesskey="N">sonraki</a> |</li>
<li class="right" >
<a href="datastructures.html" title="5. Veri Yapıları"
accesskey="P">önceki</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.11.5 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python Öğreticisi</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">6. </span>Modüller</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Hızlı Arama" aria-label="Hızlı Arama" type="search" name="q" id="search-box" />
<input type="submit" value="Git" />
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="modules">
<span id="tut-modules"></span><h1><span class="section-number">6. </span>Modüller<a class="headerlink" href="#modules" title="Permalink to this heading">¶</a></h1>
<p>Python yorumlayıcısından çıkıp tekrar girerseniz, yaptığınız tanımlar (fonksiyonlar ve değişkenler) kaybolur. Bu nedenle, daha uzun bir program yazmak istiyorsanız, girdiyi yorumlayıcıya hazırlarken bir metin düzenleyicisi kullanmak ve o dosyayla girdi olarak çalıştırmak daha iyidir. Bu bir <em>script</em> oluşturma olarak bilinir. Programınız uzadıkça, daha kolay bakım için birkaç dosyaya bölmek isteyebilirsiniz. Ayrıca, tanımını her programa kopyalamadan, birkaç programda yazdığınız kullanışlı bir fonksiyonu kullanmak isteyebilirsiniz.</p>
<p>Bunu desteklemek için Python, tanımları bir dosyaya koymanın ve bunları bir komut dosyasında veya yorumlayıcının etkileşimli bir örneğinde kullanmanın bir yolunu sağlar. Böyle bir dosyaya <em>module</em> denir; bir modülden alınan tanımlar diğer modüllere veya <em>main</em> modülüne (en üst düzeyde ve hesap makinesi modunda yürütülen bir komut dosyasında erişiminiz olan değişkenlerin derlenmesi) aktarılabilir.</p>
<p>Modül, Python tanımlarını ve ifadelerini içeren bir dosyadır. Dosya adı, <code class="file docutils literal notranslate"><span class="pre">.py</span></code> son ekini içeren modül adıdır. Bir modül içinde, modülün adı (dize olarak) <code class="docutils literal notranslate"><span class="pre">__name__</span></code> genel değişkeninin değeri olarak kullanılabilir. Örneğin, geçerli dizinde aşağıdaki içeriklerle <code class="file docutils literal notranslate"><span class="pre">fibo.py</span></code> adlı bir dosya oluşturmak için en sevdiğiniz metin düzenleyicisini kullanın:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Fibonacci numbers module</span>
<span class="k">def</span> <span class="nf">fib</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="c1"># write Fibonacci series up to n</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">while</span> <span class="n">a</span> <span class="o"><</span> <span class="n">n</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="nb">print</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">fib2</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <span class="c1"># return Fibonacci series up to n</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="k">while</span> <span class="n">a</span> <span class="o"><</span> <span class="n">n</span><span class="p">:</span>
<span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="k">return</span> <span class="n">result</span>
</pre></div>
</div>
<p>Şimdi Python yorumlayıcısına girin ve bu modülü aşağıdaki komutla içe aktarın:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fibo</span>
</pre></div>
</div>
<p>Bu, <code class="docutils literal notranslate"><span class="pre">fibo</span></code> ‘da tanımlanan işlevlerin adlarını doğrudan geçerli <a class="reference internal" href="../glossary.html#term-namespace"><span class="xref std std-term">namespace</span></a> ‘e eklemez (daha fazla ayrıntı için bkz. <a class="reference internal" href="classes.html#tut-scopes"><span class="std std-ref">Python Etki Alanları ve Ad Alanları</span></a>); oraya sadece <code class="docutils literal notranslate"><span class="pre">fibo</span></code> modül adını ekler. Modül adını kullanarak şu işlevlere erişebilirsiniz:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">fibo</span><span class="o">.</span><span class="n">fib</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="go">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</span>
<span class="gp">>>> </span><span class="n">fibo</span><span class="o">.</span><span class="n">fib2</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="go">[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]</span>
<span class="gp">>>> </span><span class="n">fibo</span><span class="o">.</span><span class="vm">__name__</span>
<span class="go">'fibo'</span>
</pre></div>
</div>
<p>Bir işlevi sık sık kullanmayı düşünüyorsanız, işlevi yerel bir ada atayabilirsiniz:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">fib</span> <span class="o">=</span> <span class="n">fibo</span><span class="o">.</span><span class="n">fib</span>
<span class="gp">>>> </span><span class="n">fib</span><span class="p">(</span><span class="mi">500</span><span class="p">)</span>
<span class="go">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377</span>
</pre></div>
</div>
<section id="more-on-modules">
<span id="tut-moremodules"></span><h2><span class="section-number">6.1. </span>Modüller hakkında daha fazla<a class="headerlink" href="#more-on-modules" title="Permalink to this heading">¶</a></h2>
<p>Bir modül, işlev tanımlarının yanı sıra çalıştırılabilir ifadeler de içerebilir. Bu ifadeler modülü başlatmayı amaçlamaktadır. Yalnızca bir import ifadesinde modül adıyla karşılaşıldığında <em>ilk</em> kez yürütülürler. <a class="footnote-reference brackets" href="#id3" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> (Dosya komut dosyası olarak yürütülürse de çalıştırılırlar.)</p>
<p>Her modülün, modülde tanımlanan tüm işlevler tarafından genel ad alanı olarak kullanılan kendi özel ad alanı vardır. Böylece, bir modülün yazarı, bir kullanıcının genel değişkenleriyle yanlışlıkla çakışma endişesi duymadan, modüldeki genel değişkenleri kullanabilir. Öte yandan, ne yaptığınızı biliyorsanız, bir modülün global değişkenlerine, işlevlerine atıfta bulunmak için kullanılan <code class="docutils literal notranslate"><span class="pre">modname.itemname</span></code> notasyonuyla dokunabilirsiniz.</p>
<p>Modüller diğer modülleri içe aktarabilir. Tüm <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> ifadelerinin bir modülün (veya bu konuda betiğin) başına yerleştirilmesi alışılmış bir durumdur ancak gerekli değildir. İçe aktarılan modül adları, bir modülün en üst düzeyine yerleştirilirse (herhangi bir işlev veya sınıfın dışında), modülün genel ad alanına eklenir.</p>
<p><a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> ifadesinin, bir modülden adları doğrudan içe aktaran modülün ad alanına aktaran bir çeşidi vardır. Örneğin:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">fibo</span> <span class="kn">import</span> <span class="n">fib</span><span class="p">,</span> <span class="n">fib2</span>
<span class="gp">>>> </span><span class="n">fib</span><span class="p">(</span><span class="mi">500</span><span class="p">)</span>
<span class="go">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377</span>
</pre></div>
</div>
<p>Bu, içe aktarmaların yerel ad alanında alındığı modül adını tanıtmaz(bu nedenle örnekte <code class="docutils literal notranslate"><span class="pre">fibo</span></code> tanımlanmamıştır).</p>
<p>Bir modülün tanımladığı tüm adları içe aktarmak için bir varyant bile vardır:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">fibo</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">>>> </span><span class="n">fib</span><span class="p">(</span><span class="mi">500</span><span class="p">)</span>
<span class="go">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377</span>
</pre></div>
</div>
<p>Bu, alt çizgiyle başlayanlar (<code class="docutils literal notranslate"><span class="pre">_</span></code>) dışındaki tüm isimleri alır. Çoğu durumda Python programcıları bu özelliği kullanmaz, çünkü yorumlayıcıya bilinmeyen bir ad kümesi ekler ve muhtemelen önceden tanımladığınız bazı şeyleri gizler.</p>
<p>Genel olarak, bir modülden veya paketten <code class="docutils literal notranslate"><span class="pre">*</span></code> içeri aktarma uygulamasının, genellikle okunamayan koda neden olduğundan hoş karşılanmadığına dikkat edin. Ancak, etkileşimli oturumlarda yazmayı kaydetmek için kullanmak sorun değildir.</p>
<p>Modül adının ardından <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> geliyorsa, <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> ‘den sonraki ad doğrudan içe aktarılan modüle bağlanır.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fibo</span> <span class="k">as</span> <span class="nn">fib</span>
<span class="gp">>>> </span><span class="n">fib</span><span class="o">.</span><span class="n">fib</span><span class="p">(</span><span class="mi">500</span><span class="p">)</span>
<span class="go">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377</span>
</pre></div>
</div>
<p>Bu, modülün <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">fibo</span></code> ‘nun yapacağı şekilde etkin bir şekilde içe aktarılmasıdır, tek farkı <code class="docutils literal notranslate"><span class="pre">fib</span></code> olarak mevcut olmasıdır.</p>
<p>Benzer efektlere sahip <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> kullanılırken de kullanılabilir:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">fibo</span> <span class="kn">import</span> <span class="n">fib</span> <span class="k">as</span> <span class="n">fibonacci</span>
<span class="gp">>>> </span><span class="n">fibonacci</span><span class="p">(</span><span class="mi">500</span><span class="p">)</span>
<span class="go">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Not</p>
<p>Verimlilik nedeniyle, her modül yorumlayıcı oturumu başına yalnızca bir kez içe aktarılır. Bu nedenle, modüllerinizi değiştirirseniz, yorumlayıcıyı yeniden başlatmanız gerekir - veya etkileşimli olarak test etmek istediğiniz tek bir modülse, <a class="reference internal" href="../library/importlib.html#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.reload()</span></code></a>, örneğin <code class="docutils literal notranslate"><span class="pre">importlib;</span> <span class="pre">importlib.reload(modulename)</span></code>.</p>
</div>
<section id="executing-modules-as-scripts">
<span id="tut-modulesasscripts"></span><h3><span class="section-number">6.1.1. </span>Modülleri komut dosyası olarak yürütme<a class="headerlink" href="#executing-modules-as-scripts" title="Permalink to this heading">¶</a></h3>
<p>Bir Python modülünü :: ile çalıştırdığınızda:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">fibo</span><span class="o">.</span><span class="n">py</span> <span class="o"><</span><span class="n">arguments</span><span class="o">></span>
</pre></div>
</div>
<p>modüldeki kod, içe aktardığınız gibi yürütülür, ancak <code class="docutils literal notranslate"><span class="pre">__name__</span></code> <code class="docutils literal notranslate"><span class="pre">"__main__"</span></code> olarak ayarlanır. Bu, modülünüzün sonuna bu kodu ekleyerek:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">fib</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
</pre></div>
</div>
<p>dosyayı bir komut dosyası ve içe aktarılabilir bir modül olarak kullanılabilir hale getirebilirsiniz, çünkü komut satırını ayrıştıran kod yalnızca modül “main” dosya olarak yürütülürse çalışır:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>fibo.py<span class="w"> </span><span class="m">50</span>
<span class="go">0 1 1 2 3 5 8 13 21 34</span>
</pre></div>
</div>
<p>Modül içe aktarılırsa kod çalıştırılmaz:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fibo</span>
<span class="gp">>>></span>
</pre></div>
</div>
<p>Bu genellikle bir modüle uygun bir kullanıcı arabirimi sağlamak veya test amacıyla kullanılır (modülü komut dosyası olarak çalıştırmak bir test paketi yürütür).</p>
</section>
<section id="the-module-search-path">
<span id="tut-searchpath"></span><h3><span class="section-number">6.1.2. </span>Modül Arama Yolu<a class="headerlink" href="#the-module-search-path" title="Permalink to this heading">¶</a></h3>
<p id="index-0">When a module named <code class="xref py py-mod docutils literal notranslate"><span class="pre">spam</span></code> is imported, the interpreter first searches for
a built-in module with that name. These module names are listed in
<a class="reference internal" href="../library/sys.html#sys.builtin_module_names" title="sys.builtin_module_names"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.builtin_module_names</span></code></a>. If not found, it then searches for a file
named <code class="file docutils literal notranslate"><span class="pre">spam.py</span></code> in a list of directories given by the variable
<a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>. <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> is initialized from these locations:</p>
<ul class="simple">
<li><p>Girdi komut dosyasını içeren dizin (veya dosya belirtilmediğinde geçerli dizin).</p></li>
<li><p><span class="target" id="index-14"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> (kabuk değişkeni <span class="target" id="index-15"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> ile aynı sözdizimine sahip dizin adlarının listesi).</p></li>
<li><p>Kuruluma bağlı varsayılan (kural olarak, <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> modülü tarafından işlenen bir “site-packages” dizini dahil).</p></li>
</ul>
<p>Daha fazla ayrıntı <a class="reference internal" href="../library/sys_path_init.html#sys-path-init"><span class="std std-ref">The initialization of the sys.path module search path</span></a> adresindedir.</p>
<div class="admonition note">
<p class="admonition-title">Not</p>
<p>Symlink’leri destekleyen dosya sistemlerinde, symlink izlendikten sonra girdi komut dosyasını içeren dizin hesaplanır. Başka bir deyişle, symlink içeren dizin modül arama yoluna <strong>not</strong> eklenir.</p>
</div>
<p>Başlatıldıktan sonra Python programları <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> değiştirebilir. Çalıştırılmakta olan komut dosyasını içeren dizin, arama yolunun başına, standart kitaplık yolunun önüne yerleştirilir. Bu, kitaplık dizininde aynı ada sahip modüller yerine bu dizindeki komut dosyalarının yüklanacağı anlamına gelir. Değiştirme amaçlanmadığı sürece bu bir hatadır. Daha fazla bilgi için <a class="reference internal" href="#tut-standardmodules"><span class="std std-ref">Standart modüller</span></a> bölümüne bakın.</p>
</section>
<section id="compiled-python-files">
<span id="tut-pycache"></span><h3><span class="section-number">6.1.3. </span>“Derlenmiş” Python dosyaları<a class="headerlink" href="#compiled-python-files" title="Permalink to this heading">¶</a></h3>
<p>Modüllerin yüklenmesini hızlandırmak için Python, her modülün derlenmiş sürümünü <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code> dizininde <code class="file docutils literal notranslate"><span class="pre">module.</span><em><span class="pre">version</span></em><span class="pre">.pyc</span></code> adı altında önbelleğe alır; burada sürüm, derlenen dosyanın biçimini kodlar; genellikle Python sürüm numarasını içerir. Örneğin, CPython 3.3 sürümünde spam.py’nin derlenmiş sürümü <code class="docutils literal notranslate"><span class="pre">__pycache__/spam.cpython-33.pyc</span></code> olarak önbelleğe alınacaktır. Bu adlandırma kuralı, farklı sürümlerden ve Python’un farklı sürümlerinden derlenmiş modüllerin bir arada var olmasına izin verir.</p>
<p>Python, eski olup olmadığını ve yeniden derlenmesi gerekip gerekmediğini görmek için kaynağın değişiklik tarihini derlenmiş sürümle karşılaştırır. Bu tamamen otomatik bir işlemdir. Ayrıca, derlenen modüller platformdan bağımsızdır, bu nedenle aynı kitaplık farklı mimarilere sahip sistemler arasında paylaşılabilir.</p>
<p>Python iki durumda önbelleği kontrol etmez. İlk olarak, doğrudan komut satırından yüklenen modülün sonucunu her zaman yeniden derler ve saklamaz. İkincisi, kaynak modül yoksa önbelleği kontrol etmez. Kaynak olmayan (yalnızca derlenmiş) bir dağıtımı desteklemek için, derlenen modül kaynak dizinde olmalı ve bir kaynak modül olmamalıdır.</p>
<p>Uzmanlar için bazı ipuçları:</p>
<ul class="simple">
<li><p>Derlenmiş bir modülün boyutunu küçültmek için Python komutundaki <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> veya <a class="reference internal" href="../using/cmdline.html#cmdoption-OO"><code class="xref std std-option docutils literal notranslate"><span class="pre">-OO</span></code></a> anahtarlarını kullanabilirsiniz. <code class="docutils literal notranslate"><span class="pre">-O</span></code> anahtarı, onaylama ifadelerini kaldırır, <code class="docutils literal notranslate"><span class="pre">-OO</span></code> anahtarı, hem assert ifadelerini hem de __doc__ dizelerini kaldırır. Bazı programlar bunların kullanılabilir olmasına güvenebileceğinden, bu seçeneği yalnızca ne yaptığınızı biliyorsanız kullanmalısınız. “Optimize edilmiş” modüller bir “opt-” etiketine sahiptir ve genellikle daha küçüktür. Gelecekteki sürümler, optimizasyonun etkilerini değiştirebilir.</p></li>
<li><p>Bir program <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> dosyasından okunduğunda, <code class="docutils literal notranslate"><span class="pre">.py</span></code> dosyasından okunduğundan daha hızlı çalışmaz; <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> dosyaları hakkında daha hızlı olan tek şey, yüklenme hızlarıdır.</p></li>
<li><p><a class="reference internal" href="../library/compileall.html#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a> modülü, bir dizindeki tüm modüller için .pyc dosyaları oluşturabilir.</p></li>
<li><p><span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a> ‘de, kararların bir akış şeması da dahil olmak üzere, bu süreç hakkında daha fazla ayrıntı bulunmaktadır.</p></li>
</ul>
</section>
</section>
<section id="standard-modules">
<span id="tut-standardmodules"></span><h2><span class="section-number">6.2. </span>Standart modüller<a class="headerlink" href="#standard-modules" title="Permalink to this heading">¶</a></h2>
<p id="index-4">Python, ayrı bir belge olan Python Kütüphanesi Referansında (bundan sonra “Kütüphane Referansı”) açıklanan standart modüllerden oluşan bir kütüphane ile birlikte gelir. Bazı modüller yorumlayıcıya yerleştirilmiştir; bunlar, dilin çekirdeğinin bir parçası olmayan, ancak yine de verimlilik için veya sistem çağrıları gibi işletim sistemi ilkellerine erişim sağlamak için yerleşik olan işlemlere erişim sağlar. Bu tür modüller seti, aynı zamanda temel platforma da bağlı olan bir yapılandırma seçeneğidir. Örneğin, <a class="reference internal" href="../library/winreg.html#module-winreg" title="winreg: Routines and objects for manipulating the Windows registry. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">winreg</span></code></a> modülü yalnızca Windows sistemlerinde sağlanır. Belirli bir modül biraz ilgiyi hak ediyor: Her Python yorumlayıcısında yerleşik olan <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a>. <code class="docutils literal notranslate"><span class="pre">sys.ps1</span></code> ve <code class="docutils literal notranslate"><span class="pre">sys.ps2</span></code> değişkenleri, birincil ve ikincil bilgi istemleri olarak kullanılan dizeleri tanımlar:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">ps1</span>
<span class="go">'>>> '</span>
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">ps2</span>
<span class="go">'... '</span>
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">ps1</span> <span class="o">=</span> <span class="s1">'C> '</span>
<span class="go">C> print('Yuck!')</span>
<span class="go">Yuck!</span>
<span class="go">C></span>
</pre></div>
</div>
<p>Bu iki değişken yalnızca yorumlayıcı etkileşimli moddaysa tanımlanır.</p>
<p><code class="docutils literal notranslate"><span class="pre">sys.path</span></code> değişkeni, yorumlayıcının modüller için arama yolunu belirleyen bir dizeler listesidir. <span class="target" id="index-17"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> ortam değişkeninden veya <span class="target" id="index-18"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> ayarlanmamışsa yerleşik bir varsayılan değerden alınan varsayılan bir yola başlatılır. Standart liste işlemlerini kullanarak değiştirebilirsiniz:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">'/ufs/guido/lib/python'</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="the-dir-function">
<span id="tut-dir"></span><h2><span class="section-number">6.3. </span><a class="reference internal" href="../library/functions.html#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a> Fonksiyonu<a class="headerlink" href="#the-dir-function" title="Permalink to this heading">¶</a></h2>
<p>Yerleşik fonksiyon <a class="reference internal" href="../library/functions.html#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a>, bir modülün hangi adları tanımladığını bulmak için kullanılır. Sıralanmış bir dize listesi döndürür:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fibo</span><span class="o">,</span> <span class="nn">sys</span>
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">fibo</span><span class="p">)</span>
<span class="go">['__name__', 'fib', 'fib2']</span>
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">sys</span><span class="p">)</span>
<span class="go">['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__',</span>
<span class="go"> '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__',</span>
<span class="go"> '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__',</span>
<span class="go"> '_clear_type_cache', '_current_frames', '_debugmallocstats', '_framework',</span>
<span class="go"> '_getframe', '_git', '_home', '_xoptions', 'abiflags', 'addaudithook',</span>
<span class="go"> 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix',</span>
<span class="go"> 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing',</span>
<span class="go"> 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info',</span>
<span class="go"> 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info',</span>
<span class="go"> 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth',</span>
<span class="go"> 'getallocatedblocks', 'getdefaultencoding', 'getdlopenflags',</span>
<span class="go"> 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile',</span>
<span class="go"> 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval',</span>
<span class="go"> 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',</span>
<span class="go"> 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value',</span>
<span class="go"> 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks',</span>
<span class="go"> 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'pycache_prefix',</span>
<span class="go"> 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setdlopenflags',</span>
<span class="go"> 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr',</span>
<span class="go"> 'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info',</span>
<span class="go"> 'warnoptions']</span>
</pre></div>
</div>
<p>Argümanlar olmadan, <a class="reference internal" href="../library/functions.html#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a>, şu anda tanımladığınız adları listeler:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fibo</span>
<span class="gp">>>> </span><span class="n">fib</span> <span class="o">=</span> <span class="n">fibo</span><span class="o">.</span><span class="n">fib</span>
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">()</span>
<span class="go">['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']</span>
</pre></div>
</div>
<p>Her tür adın listelendiğini unutmayın: değişkenler, modüller, fonksiyonlar vb.</p>
<p id="index-7"><a class="reference internal" href="../library/functions.html#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a> yerleşik fonksiyonlarun ve değişkenlerin adlarını listelemez. Bunların bir listesini istiyorsanız, standart modül <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> ‘de tanımlanırlar:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">builtins</span>
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">builtins</span><span class="p">)</span>
<span class="go">['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',</span>
<span class="go"> 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',</span>
<span class="go"> 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',</span>
<span class="go"> 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',</span>
<span class="go"> 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',</span>
<span class="go"> 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',</span>
<span class="go"> 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',</span>
<span class="go"> 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',</span>
<span class="go"> 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',</span>
<span class="go"> 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',</span>
<span class="go"> 'NotImplementedError', 'OSError', 'OverflowError',</span>
<span class="go"> 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',</span>
<span class="go"> 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning',</span>
<span class="go"> 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',</span>
<span class="go"> 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError',</span>
<span class="go"> 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',</span>
<span class="go"> 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',</span>
<span class="go"> 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__',</span>
<span class="go"> '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs',</span>
<span class="go"> 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable',</span>
<span class="go"> 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',</span>
<span class="go"> 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',</span>
<span class="go"> 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',</span>
<span class="go"> 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',</span>
<span class="go"> 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview',</span>
<span class="go"> 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',</span>
<span class="go"> 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',</span>
<span class="go"> 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',</span>
<span class="go"> 'zip']</span>
</pre></div>
</div>
</section>
<section id="packages">
<span id="tut-packages"></span><h2><span class="section-number">6.4. </span>Paketler<a class="headerlink" href="#packages" title="Permalink to this heading">¶</a></h2>
<p>Packages are a way of structuring Python’s module namespace by using “dotted
module names”. For example, the module name <code class="xref py py-mod docutils literal notranslate"><span class="pre">A.B</span></code> designates a submodule
named <code class="docutils literal notranslate"><span class="pre">B</span></code> in a package named <code class="docutils literal notranslate"><span class="pre">A</span></code>. Just like the use of modules saves the
authors of different modules from having to worry about each other’s global
variable names, the use of dotted module names saves the authors of multi-module
packages like NumPy or Pillow from having to worry about
each other’s module names.</p>
<p>Ses dosyalarının ve ses verilerinin tek tip işlenmesi için bir modül koleksiyonu (“paket”) tasarlamak istediğinizi varsayalım. Birçok farklı ses dosyası biçimi vardır (genellikle uzantılarıyla tanınır, örneğin: <code class="file docutils literal notranslate"><span class="pre">.wav</span></code>, <code class="file docutils literal notranslate"><span class="pre">.aiff</span></code>, <code class="file docutils literal notranslate"><span class="pre">.au</span></code>) bu nedenle, çeşitli dosya biçimleri arasında dönüşüm için büyüyen bir modül koleksiyonu oluşturmanız ve sürdürmeniz gerekebilir. Ses verileri üzerinde gerçekleştirmek isteyebileceğiniz birçok farklı işlem de vardır (karıştırma, eko ekleme, ekolayzır işlevi uygulama, yapay bir stereo efekti oluşturma gibi) bu nedenle ek olarak, bu işlemleri gerçekleştirmek için hiç bitmeyen bir modül akışı yazıyor olacaksınız. İşte paketiniz için olası bir yapı (hiyerarşik bir dosya sistemi cinsinden ifade edilir):</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
filters/ Subpackage for filters
__init__.py
equalizer.py
vocoder.py
karaoke.py
...
</pre></div>
</div>
<p>Paketi içe aktarırken Python, paket alt dizinini arayan <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> üzerindeki dizinleri arar.</p>
<p>The <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code> files are required to make Python treat directories
containing the file as packages. This prevents directories with a common name,
such as <code class="docutils literal notranslate"><span class="pre">string</span></code>, from unintentionally hiding valid modules that occur later
on the module search path. In the simplest case, <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code> can just be
an empty file, but it can also execute initialization code for the package or
set the <code class="docutils literal notranslate"><span class="pre">__all__</span></code> variable, described later.</p>
<p>Paketin kullanıcıları, paketin içindeki ayrı modülleri içe aktarabilir, örneğin:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sound.effects.echo</span>
</pre></div>
</div>
<p>This loads the submodule <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound.effects.echo</span></code>. It must be referenced with
its full name.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sound</span><span class="o">.</span><span class="n">effects</span><span class="o">.</span><span class="n">echo</span><span class="o">.</span><span class="n">echofilter</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">atten</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
<p>Alt modülü içe aktarmanın alternatif bir yolu:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sound.effects</span> <span class="kn">import</span> <span class="n">echo</span>
</pre></div>
</div>
<p>This also loads the submodule <code class="xref py py-mod docutils literal notranslate"><span class="pre">echo</span></code>, and makes it available without its
package prefix, so it can be used as follows:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">echo</span><span class="o">.</span><span class="n">echofilter</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">atten</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
<p>Yine başka bir varyasyon, istenen işlevi veya değişkeni doğrudan içe aktarmaktır:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sound.effects.echo</span> <span class="kn">import</span> <span class="n">echofilter</span>
</pre></div>
</div>
<p>Again, this loads the submodule <code class="xref py py-mod docutils literal notranslate"><span class="pre">echo</span></code>, but this makes its function
<code class="xref py py-func docutils literal notranslate"><span class="pre">echofilter()</span></code> directly available:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">echofilter</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">output</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">atten</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">package</span> <span class="pre">import</span> <span class="pre">item</span></code> kullanırken, öğenin paketin bir alt modülü (veya alt paketi) veya pakette tanımlanmış bir fonksiyon, sınıf veya değişken gibi başka bir ad olabileceğini unutmayın. <code class="docutils literal notranslate"><span class="pre">import</span></code> ifadesi önce öğenin pakette tanımlanıp tanımlanmadığını test eder; değilse modül olduğunu varsayar ve yüklemeye çalışır. Onu bulamazsa, bir <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> istisnası ortaya çıkar.</p>
<p>Aksine, <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">item.subitem.subsubitem</span></code> gibi bir sözdizimi kullanırken, sonuncusu hariç her öğe bir paket olmalıdır; son öğe bir modül veya paket olabilir, ancak önceki öğede tanımlanan bir sınıf, fonksiyon veya değişken olamaz.</p>
<section id="importing-from-a-package">
<span id="tut-pkg-import-star"></span><h3><span class="section-number">6.4.1. </span>Bir Paketten * İçe Aktarma<a class="headerlink" href="#importing-from-a-package" title="Permalink to this heading">¶</a></h3>
<p id="index-8">Şimdi, kullanıcı <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">sound.effects</span> <span class="pre">import</span> <span class="pre">*</span></code> yazdığında ne olur? İdeal olarak, bunun bir şekilde dosya sistemine gitmesi, pakette hangi alt modüllerin bulunduğunu bulması ve hepsini içe aktarması umulur. Bu uzun zaman alabilir ve alt modüllerin içe aktarılması, yalnızca alt modül açıkça içe aktarıldığında gerçekleşmesi gereken istenmeyen yan etkilere neden olabilir.</p>
<p>Tek çözüm, paket yazarının paketin açık bir dizinini sağlamasıdır. <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> ifadesi aşağıdaki kuralı kullanır: eğer bir paketin <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code> kodu <code class="docutils literal notranslate"><span class="pre">__all__</span></code> adlı bir liste tanımlarsa, <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">package</span> <span class="pre">import</span> <span class="pre">*</span></code> ile karşılaşıldığında alınması gereken modül adlarının listesi olarak alınır. Paketin yeni bir sürümü yayınlandığında bu listeyi güncel tutmak paket yazarının sorumluluğundadır. Paket yazarları, paketlerinden * içe aktarmak için bir kullanım görmezlerse, onu desteklememeye de karar verebilirler. Örneğin, <code class="file docutils literal notranslate"><span class="pre">sound/effects/__init__.py</span></code> dosyası şu kodu içerebilir:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"echo"</span><span class="p">,</span> <span class="s2">"surround"</span><span class="p">,</span> <span class="s2">"reverse"</span><span class="p">]</span>
</pre></div>
</div>
<p>This would mean that <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">sound.effects</span> <span class="pre">import</span> <span class="pre">*</span></code> would import the three
named submodules of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound.effects</span></code> package.</p>
<p>Be aware that submodules might become shadowed by locally defined names. For
example, if you added a <code class="docutils literal notranslate"><span class="pre">reverse</span></code> function to the
<code class="file docutils literal notranslate"><span class="pre">sound/effects/__init__.py</span></code> file, the <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">sound.effects</span> <span class="pre">import</span> <span class="pre">*</span></code>
would only import the two submodules <code class="docutils literal notranslate"><span class="pre">echo</span></code> and <code class="docutils literal notranslate"><span class="pre">surround</span></code>, but <em>not</em> the
<code class="docutils literal notranslate"><span class="pre">reverse</span></code> submodule, because it is shadowed by the locally defined
<code class="docutils literal notranslate"><span class="pre">reverse</span></code> function:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
<span class="s2">"echo"</span><span class="p">,</span> <span class="c1"># refers to the 'echo.py' file</span>
<span class="s2">"surround"</span><span class="p">,</span> <span class="c1"># refers to the 'surround.py' file</span>
<span class="s2">"reverse"</span><span class="p">,</span> <span class="c1"># !!! refers to the 'reverse' function now !!!</span>
<span class="p">]</span>
<span class="k">def</span> <span class="nf">reverse</span><span class="p">(</span><span class="n">msg</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span> <span class="c1"># <-- this name shadows the 'reverse.py' submodule</span>
<span class="k">return</span> <span class="n">msg</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># in the case of a 'from sound.effects import *'</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">__all__</span></code> is not defined, the statement <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">sound.effects</span> <span class="pre">import</span> <span class="pre">*</span></code>
does <em>not</em> import all submodules from the package <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound.effects</span></code> into the
current namespace; it only ensures that the package <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound.effects</span></code> has
been imported (possibly running any initialization code in <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code>)
and then imports whatever names are defined in the package. This includes any
names defined (and submodules explicitly loaded) by <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code>. It
also includes any submodules of the package that were explicitly loaded by
previous <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statements. Consider this code:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sound.effects.echo</span>
<span class="kn">import</span> <span class="nn">sound.effects.surround</span>
<span class="kn">from</span> <span class="nn">sound.effects</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>In this example, the <code class="xref py py-mod docutils literal notranslate"><span class="pre">echo</span></code> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">surround</span></code> modules are imported in the
current namespace because they are defined in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound.effects</span></code> package
when the <code class="docutils literal notranslate"><span class="pre">from...import</span></code> statement is executed. (This also works when
<code class="docutils literal notranslate"><span class="pre">__all__</span></code> is defined.)</p>
<p>Bazı modüller, <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">*</span></code> kullandığınızda yalnızca belirli kalıpları takip eden adları dışa aktarmak üzere tasarlanmış olsa da, üretim kodunda yine de kötü uygulama olarak kabul edilir.</p>
<p>Unutmayın, <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">package</span> <span class="pre">import</span> <span class="pre">specific_submodule</span></code> kullanmanın yanlış bir tarafı yok! Aslında, içe aktarma modülünün farklı paketlerden aynı ada sahip alt modülleri kullanması gerekmedikçe, önerilen gösterim budur.</p>
</section>
<section id="intra-package-references">
<span id="id2"></span><h3><span class="section-number">6.4.2. </span>Paket İçi Referanslar<a class="headerlink" href="#intra-package-references" title="Permalink to this heading">¶</a></h3>
<p>When packages are structured into subpackages (as with the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound</span></code> package
in the example), you can use absolute imports to refer to submodules of siblings
packages. For example, if the module <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound.filters.vocoder</span></code> needs to use
the <code class="xref py py-mod docutils literal notranslate"><span class="pre">echo</span></code> module in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sound.effects</span></code> package, it can use <code class="docutils literal notranslate"><span class="pre">from</span>
<span class="pre">sound.effects</span> <span class="pre">import</span> <span class="pre">echo</span></code>.</p>
<p>You can also write relative imports, with the <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">name</span></code> form
of import statement. These imports use leading dots to indicate the current and
parent packages involved in the relative import. From the <code class="xref py py-mod docutils literal notranslate"><span class="pre">surround</span></code>
module for example, you might use:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">echo</span>
<span class="kn">from</span> <span class="nn">..</span> <span class="kn">import</span> <span class="n">formats</span>
<span class="kn">from</span> <span class="nn">..filters</span> <span class="kn">import</span> <span class="n">equalizer</span>
</pre></div>
</div>
<p>Göreceli içe aktarmaların geçerli modülün adını temel aldığını unutmayın. Ana modülün adı her zaman <code class="docutils literal notranslate"><span class="pre">"__main__"</span></code> olduğundan, Python uygulamasının ana modülü olarak kullanılması amaçlanan modüller her zaman mutlak içe aktarma kullanmalıdır.</p>
</section>
<section id="packages-in-multiple-directories">
<h3><span class="section-number">6.4.3. </span>Birden Çok Dizindeki Paketler<a class="headerlink" href="#packages-in-multiple-directories" title="Permalink to this heading">¶</a></h3>
<p>Paketler bir özel özelliği daha destekler, <a class="reference internal" href="../reference/import.html#path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>. Bu, o dosyadaki kod yürütülmeden önce paketin <code class="file docutils literal notranslate"><span class="pre">__init__.py</span></code> dosyasını tutan dizinin adını içeren bir liste olacak şekilde başlatılır. Bu değişken değiştirilebilir; bunu yapmak, pakette bulunan modüller ve alt paketler için gelecekteki aramaları etkiler.</p>
<p>Bu özelliğe sıklıkla ihtiyaç duyulmasa da, bir pakette bulunan modül dizisini genişletmek için kullanılabilir.</p>
<p class="rubric">Dipnotlar</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id3" role="note">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
<p>Aslında işlev tanımları aynı zamanda ‘çalıştırılan’ ‘ifadelerdir’; modül düzeyinde bir işlev tanımının çalıştırılması, işlev adını modülün genel ad alanına ekler.</p>
</aside>
</aside>
</section>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="../contents.html">İçindekiler</a></h3>
<ul>
<li><a class="reference internal" href="#">6. Modüller</a><ul>
<li><a class="reference internal" href="#more-on-modules">6.1. Modüller hakkında daha fazla</a><ul>
<li><a class="reference internal" href="#executing-modules-as-scripts">6.1.1. Modülleri komut dosyası olarak yürütme</a></li>
<li><a class="reference internal" href="#the-module-search-path">6.1.2. Modül Arama Yolu</a></li>
<li><a class="reference internal" href="#compiled-python-files">6.1.3. “Derlenmiş” Python dosyaları</a></li>
</ul>
</li>
<li><a class="reference internal" href="#standard-modules">6.2. Standart modüller</a></li>
<li><a class="reference internal" href="#the-dir-function">6.3. <code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code> Fonksiyonu</a></li>
<li><a class="reference internal" href="#packages">6.4. Paketler</a><ul>
<li><a class="reference internal" href="#importing-from-a-package">6.4.1. Bir Paketten * İçe Aktarma</a></li>
<li><a class="reference internal" href="#intra-package-references">6.4.2. Paket İçi Referanslar</a></li>
<li><a class="reference internal" href="#packages-in-multiple-directories">6.4.3. Birden Çok Dizindeki Paketler</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Önceki konu</h4>
<p class="topless"><a href="datastructures.html"
title="önceki bölüm"><span class="section-number">5. </span>Veri Yapıları</a></p>
</div>
<div>
<h4>Sonraki konu</h4>
<p class="topless"><a href="inputoutput.html"
title="sonraki bölüm"><span class="section-number">7. </span>Girdi ve Çıktı</a></p>
</div>
<div role="note" aria-label="source link">
<h3>Bu Sayfa</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Hata Bildir</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.11/Doc/tutorial/modules.rst"
rel="nofollow">Kaynağı Göster
</a>
</li>
</ul>
</div>
</div>
<div id="sidebarbutton" title="Yan çubuğu daralt">
<span>«</span>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Gezinti</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="Genel Endeks"
>dizin</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Modül Dizini"
>modülleri</a> |</li>
<li class="right" >
<a href="inputoutput.html" title="7. Girdi ve Çıktı"
>sonraki</a> |</li>
<li class="right" >
<a href="datastructures.html" title="5. Veri Yapıları"
>önceki</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.11.5 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python Öğreticisi</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">6. </span>Modüller</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Hızlı Arama" aria-label="Hızlı Arama" type="search" name="q" id="search-box" />
<input type="submit" value="Git" />
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Telif Hakkı</a> 2001-2023, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
See <a href="/license.html">History and License</a> for more information.<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
Son güncelleme: Ara 01, 2023.
<a href="/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 6.2.1.
</div>
</body>
</html>