Repository navigation
Expand file tree
/
Copy path__main__.po
More file actions
447 lines (375 loc) · 30.1 KB
/
Copy path__main__.po
File metadata and controls
447 lines (375 loc) · 30.1 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
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001 Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# Revisto <theRevisto@gmail.com>, 2025
# Sepehr Rasouli <sepehrrasouli06@gmail.com>, 2026
# invincible627 <fatemehzahra810@gmail.com>, 2026
#
msgid ""
msgstr ""
"Project-Id-Version: Python 3.14\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-08-26 03:59+0000\n"
"PO-Revision-Date: 2021-06-28 00:54+0000\n"
"Last-Translator: Sepehr Rasouli <sepehrrasouli06@gmail.com>, 2026\n"
"Language-Team: Persian (https://github.com/python/python-docs-fa/)\n"
"Language: fa\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid ":mod:`!__main__` --- Top-level code environment"
msgstr ":mod:`!__main__` --- محیط سطحبالای کد"
msgid "In Python, the special name ``__main__`` is used for two important constructs:"
msgstr "در پایتون، از نام ویژهی ``__main__`` برای دو ساختار مهم استفاده میشود:"
msgid "the name of the top-level environment of the program, which can be checked using the ``__name__ == '__main__'`` expression; and"
msgstr "نام محیط سطح بالای برنامه، که میتوان آن را با استفاده از عبارت ``__name__ == '__main__'`` بررسی کرد؛ و"
msgid "the ``__main__.py`` file in Python packages."
msgstr "پرونده ``__main__.py`` در بستههای پایتون."
msgid "Both of these mechanisms are related to Python modules; how users interact with them and how they interact with each other. They are explained in detail below. If you're new to Python modules, see the tutorial section :ref:`tut-modules` for an introduction."
msgstr "هر دوی این مکانیزمها به ماژولهای پایتون مربوط میشوند: نحوهی تعامل کاربران با آنها و نحوهی تعامل آنها با یکدیگر. این موارد در زیر بهتفصیل توضیح داده شدهاند. اگر با ماژولهای پایتون تازهکار هستید، برای آشنایی به بخش آموزش :ref:`tut-modules` مراجعه کنید."
msgid "``__name__ == '__main__'``"
msgstr "``__name__ == '__main__'``"
msgid "When a Python module or package is imported, ``__name__`` is set to the module's name. Usually, this is the name of the Python file itself without the ``.py`` extension::"
msgstr "هنگامی که یک ماژول یا بستهی پایتون ایمپورت میشود، ``__name__`` به نام ماژول تنظیم میشود. معمولاً این همان نام پرونده پایتون است بدون پسوند ``.py``::"
msgid ""
">>> import configparser\n"
">>> configparser.__name__\n"
"'configparser'"
msgstr ""
">>> import configparser\n"
">>> configparser.__name__\n"
"'configparser'"
msgid "If the file is part of a package, ``__name__`` will also include the parent package's path::"
msgstr "اگر پرونده بخشی از یک بسته باشد، ``__name__`` همچنین مسیر بستهی والد را شامل خواهد شد::"
msgid ""
">>> from concurrent.futures import process\n"
">>> process.__name__\n"
"'concurrent.futures.process'"
msgstr ""
">>> from concurrent.futures import process\n"
">>> process.__name__\n"
"'concurrent.futures.process'"
msgid "However, if the module is executed in the top-level code environment, its ``__name__`` is set to the string ``'__main__'``."
msgstr "با این حال، اگر ماژول در محیط کد سطح بالا اجرا شود، ``__name__`` آن به رشتهی ``'__main__'`` تنظیم میشود."
msgid "What is the \"top-level code environment\"?"
msgstr "«محیط کد سطحبالا» چیست؟"
msgid "``__main__`` is the name of the environment where top-level code is run. \"Top-level code\" is the first user-specified Python module that starts running. It's \"top-level\" because it imports all other modules that the program needs. Sometimes \"top-level code\" is called an *entry point* to the application."
msgstr "``__main__`` نام محیطی است که کد سطحبالا در آن اجرا میشود. \"کد سطحبالا\" اولین ماژول پایتونی است که توسط کاربر مشخص میشود و شروع به اجرا میکند. به آن \"سطحبالا\" گفته میشود زیرا همهی ماژولهای دیگری را که برنامه به آنها نیاز دارد، ایمپورت میکند. گاهی به \"کد سطحبالا\" *نقطهی ورود* برنامه نیز گفته میشود."
msgid "The top-level code environment can be:"
msgstr "محیط سطحبالای کد میتواند باشد:"
msgid "the scope of an interactive prompt::"
msgstr "محدودهی یک دستور تعاملی::"
msgid ""
">>> __name__\n"
"'__main__'"
msgstr ""
">>> __name__\n"
"'__main__'"
msgid "the Python module passed to the Python interpreter as a file argument:"
msgstr "ماژول پایتونی که بهعنوان آرگومان فایل به مفسر پایتون پاس داده میشود:"
msgid ""
"$ python helloworld.py\n"
"Hello, world!"
msgstr ""
"$ python helloworld.py\n"
"Hello, world!"
msgid "the Python module or package passed to the Python interpreter with the :option:`-m` argument:"
msgstr "ماژول یا بستهی پایتونی که با آرگومان :option:`-m` به مفسر پایتون پاس داده میشود:"
msgid ""
"$ python -m tarfile\n"
"usage: tarfile.py [-h] [-v] (...)"
msgstr ""
"$ python -m tarfile\n"
"usage: tarfile.py [-h] [-v] (...)"
msgid "Python code read by the Python interpreter from standard input:"
msgstr "کد پایتون توسط مفسر پایتون از ورودی استاندارد خوانده میشود:"
msgid ""
"$ echo \"import this\" | python\n"
"The Zen of Python, by Tim Peters\n"
"\n"
"Beautiful is better than ugly.\n"
"Explicit is better than implicit.\n"
"..."
msgstr ""
"$ echo \"import this\" | python\n"
"The Zen of Python, by Tim Peters\n"
"\n"
"Beautiful is better than ugly.\n"
"Explicit is better than implicit.\n"
"..."
msgid "Python code passed to the Python interpreter with the :option:`-c` argument:"
msgstr "کد پایتون با آرگومان :option:`-c` به مفسر پایتون پاس داده میشود:"
msgid ""
"$ python -c \"import this\"\n"
"The Zen of Python, by Tim Peters\n"
"\n"
"Beautiful is better than ugly.\n"
"Explicit is better than implicit.\n"
"..."
msgstr ""
"$ python -c \"import this\"\n"
"The Zen of Python, by Tim Peters\n"
"\n"
"Beautiful is better than ugly.\n"
"Explicit is better than implicit.\n"
"..."
msgid "In each of these situations, the top-level module's ``__name__`` is set to ``'__main__'``."
msgstr "در هر کدام از این موقعیتها، ``__name__`` ماژول سطحبالا به ``'__main__'`` تنظیم میشود."
msgid "As a result, a module can discover whether or not it is running in the top-level environment by checking its own ``__name__``, which allows a common idiom for conditionally executing code when the module is not initialized from an import statement::"
msgstr "در نتیجه، یک ماژول میتواند با بررسی ``__name__`` خود متوجه شود که آیا در محیط سطحبالا اجرا میشود یا نه، که این امکان یک الگوی رایج برای اجرای شرطی کد را فراهم میکند زمانی که ماژول از یک دستور ایمپورت مقداردهی اولیه نشده است::"
msgid ""
"if __name__ == '__main__':\n"
" # Execute when the module is not initialized from an import statement.\n"
" ..."
msgstr ""
"if __name__ == '__main__':\n"
" # وقتی ماژول با دستور ایمپورت راهاندازی نشده باشد اجرا میشود.\n"
" ..."
msgid "For a more detailed look at how ``__name__`` is set in all situations, see the tutorial section :ref:`tut-modules`."
msgstr "برای نگاهی دقیقتر به نحوهی تنظیم ``__name__`` در همهی موقعیتها، به بخش آموزش :ref:`tut-modules` مراجعه کنید."
msgid "Idiomatic Usage"
msgstr "استفادهی اصطلاحی"
msgid "Some modules contain code that is intended for script use only, like parsing command-line arguments or fetching data from standard input. If a module like this was imported from a different module, for example to unit test it, the script code would unintentionally execute as well."
msgstr "بعضی ماژولها دارای کدی هستند که فقط برای استفادهی اسکریپتی در نظر گرفته شده است، مانند تجزیهی آرگومانهای خط فرمان یا دریافت داده از ورودی استاندارد. اگر چنین ماژولی از ماژول دیگری ایمپورت شود، مثلاً برای یونیتتست آن، کد اسکریپتی نیز بهطور ناخواسته اجرا خواهد شد."
msgid "This is where using the ``if __name__ == '__main__'`` code block comes in handy. Code within this block won't run unless the module is executed in the top-level environment."
msgstr "اینجاست که استفاده از بلوک کد ``if __name__ == '__main__'`` به کار میآید. کد داخل این بلوک اجرا نمیشود مگر اینکه ماژول در محیط سطحبالا اجرا شود."
msgid "Putting as few statements as possible in the block below ``if __name__ == '__main__'`` can improve code clarity and correctness. Most often, a function named ``main`` encapsulates the program's primary behavior::"
msgstr "قرار دادن کمترین تعداد دستور ممکن در بلوک زیر ``if __name__ == '__main__'`` میتواند وضوح و صحت کد را بهبود بخشد. اغلب، یک تابع به نام ``main`` رفتار اصلی برنامه را در خود محصور میکند::"
msgid ""
"# echo.py\n"
"\n"
"import shlex\n"
"import sys\n"
"\n"
"def echo(phrase: str) -> None:\n"
" \"\"\"A dummy wrapper around print.\"\"\"\n"
" # for demonstration purposes, you can imagine that there is some\n"
" # valuable and reusable logic inside this function\n"
" print(phrase)\n"
"\n"
"def main() -> int:\n"
" \"\"\"Echo the input arguments to standard output\"\"\"\n"
" phrase = shlex.join(sys.argv)\n"
" echo(phrase)\n"
" return 0\n"
"\n"
"if __name__ == '__main__':\n"
" sys.exit(main()) # next section explains the use of sys.exit"
msgstr ""
"# echo.py\n"
"\n"
"import shlex\n"
"import sys\n"
"\n"
"def echo(phrase: str) -> None:\n"
" \"\"\"یک پوشش ساختگی برای print.\"\"\"\n"
" # برای اهداف نمایشی، میتوانید تصور کنید که منطق ارزشمندی\n"
" # و قابل استفادهی مجددی درون این تابع وجود دارد\n"
" print(phrase)\n"
"\n"
"def main() -> int:\n"
" \"\"\"آرگومانهای ورودی را به خروجی استاندارد بازتاب میدهد\"\"\"\n"
" phrase = shlex.join(sys.argv)\n"
" echo(phrase)\n"
" return 0\n"
"\n"
"if __name__ == '__main__':\n"
" sys.exit(main()) # بخش بعدی استفاده از sys.exit را توضیح میدهد"
msgid "Note that if the module didn't encapsulate code inside the ``main`` function but instead put it directly within the ``if __name__ == '__main__'`` block, the ``phrase`` variable would be global to the entire module. This is error-prone as other functions within the module could be unintentionally using the global variable instead of a local name. A ``main`` function solves this problem."
msgstr "توجه داشته باشید که اگر ماژول کد را درون تابع ``main`` محصور نمیکرد و به جای آن مستقیماً آن را درون بلوک ``if __name__ == '__main__'`` قرار میداد، متغیر ``phrase`` برای کل ماژول سراسری میشد. این امر مستعد خطا است، زیرا ممکن است سایر توابع درون ماژول بهطور ناخواسته از متغیر سراسری به جای یک نام محلی استفاده کنند. تابع ``main`` این مشکل را حل میکند."
msgid "Using a ``main`` function has the added benefit of the ``echo`` function itself being isolated and importable elsewhere. When ``echo.py`` is imported, the ``echo`` and ``main`` functions will be defined, but neither of them will be called, because ``__name__ != '__main__'``."
msgstr "استفاده از تابع ``main`` این مزیت اضافی را دارد که خود تابع ``echo`` ایزوله و قابل ایمپورت در جای دیگر است. وقتی ``echo.py`` ایمپورت میشود، توابع ``echo`` و ``main`` تعریف میشوند، اما هیچکدام از آنها فراخوانی نمیشوند، زیرا ``__name__ != '__main__'`` است."
msgid "Packaging Considerations"
msgstr "ملاحظات بستهبندی"
msgid "``main`` functions are often used to create command-line tools by specifying them as entry points for console scripts. When this is done, `pip <https://pip.pypa.io/>`_ inserts the function call into a template script, where the return value of ``main`` is passed into :func:`sys.exit`. For example::"
msgstr "توابع ``main`` اغلب برای ساختن ابزارهای خط فرمان با مشخص کردن آنها بهعنوان نقاط ورودی برای اسکریپتهای کنسولی استفاده میشوند. وقتی این کار انجام میشود، `pip <https://pip.pypa.io/>`_ فراخوانی تابع را در یک اسکریپت الگو قرار میدهد، جایی که مقدار بازگشتی ``main`` به :func:`sys.exit` پاس داده میشود. برای مثال::"
msgid "sys.exit(main())"
msgstr "sys.exit(main())"
msgid "Since the call to ``main`` is wrapped in :func:`sys.exit`, the expectation is that your function will return some value acceptable as an input to :func:`sys.exit`; typically, an integer or ``None`` (which is implicitly returned if your function does not have a return statement)."
msgstr "از آنجایی که فراخوانی ``main`` در :func:`sys.exit` پیچیده شده است، انتظار میرود که تابع شما مقداری را برگرداند که بهعنوان ورودی برای :func:`sys.exit` قابلقبول باشد؛ معمولاً یک عدد صحیح یا ``None`` (که اگر تابع شما دستور return نداشته باشد، بهصورت ضمنی برگردانده میشود)."
msgid "By proactively following this convention ourselves, our module will have the same behavior when run directly (i.e. ``python echo.py``) as it will have if we later package it as a console script entry-point in a pip-installable package."
msgstr "با پیروی فعالانهی خودمان از این قرارداد، ماژول ما هنگام اجرای مستقیم (یعنی ``python echo.py``) همان رفتاری را خواهد داشت که در صورت بستهبندی بعدی آن بهعنوان یک نقطهی ورودی اسکریپت کنسول در یک بستهی قابل نصب با pip خواهد داشت."
msgid "In particular, be careful about returning strings from your ``main`` function. :func:`sys.exit` will interpret a string argument as a failure message, so your program will have an exit code of ``1``, indicating failure, and the string will be written to :data:`sys.stderr`. The ``echo.py`` example from earlier exemplifies using the ``sys.exit(main())`` convention."
msgstr "بهویژه، در مورد بازگرداندن رشتهها از تابع ``main`` خود دقت کنید. :func:`sys.exit` یک آرگومان رشتهای را بهعنوان پیام شکست تفسیر میکند، بنابراین برنامهی شما کد خروجی ``1`` را خواهد داشت که نشاندهندهی شکست است و رشته به :data:`sys.stderr` نوشته میشود. مثال ``echo.py`` از قبل، استفاده از قرارداد ``sys.exit(main())`` را بهخوبی نشان میدهد."
msgid "`Python Packaging User Guide <https://packaging.python.org/>`_ contains a collection of tutorials and references on how to distribute and install Python packages with modern tools."
msgstr "`Python Packaging User Guide <https://packaging.python.org/>`_ شامل مجموعهای از آموزشها و مراجع دربارهی نحوهی توزیع و نصب بستههای پایتون با ابزارهای مدرن است."
msgid "``__main__.py`` in Python Packages"
msgstr "``__main__.py`` در بستههای پایتون"
msgid "If you are not familiar with Python packages, see section :ref:`tut-packages` of the tutorial. Most commonly, the ``__main__.py`` file is used to provide a command-line interface for a package. Consider the following hypothetical package, \"bandclass\":"
msgstr "اگر با بستههای پایتون آشنایی ندارید، به بخش :ref:`tut-packages` از آموزش مراجعه کنید. معمولاً، از فایل ``__main__.py`` برای ارائهی یک رابط خط فرمان برای یک بسته استفاده میشود. بستهی فرضی زیر را در نظر بگیرید، \"bandclass\":"
msgid ""
"bandclass\n"
" ├── __init__.py\n"
" ├── __main__.py\n"
" └── student.py"
msgstr ""
"bandclass\n"
" ├── __init__.py\n"
" ├── __main__.py\n"
" └── student.py"
msgid "``__main__.py`` will be executed when the package itself is invoked directly from the command line using the :option:`-m` flag. For example:"
msgstr "``__main__.py`` زمانی اجرا میشود که خود بسته مستقیماً از خط فرمان با استفاده از پرچم :option:`-m` فراخوانی شود. برای مثال:"
msgid "$ python -m bandclass"
msgstr "$ python -m bandclass"
msgid "This command will cause ``__main__.py`` to run. How you utilize this mechanism will depend on the nature of the package you are writing, but in this hypothetical case, it might make sense to allow the teacher to search for students::"
msgstr "این دستور باعث اجرای ``__main__.py`` میشود. نحوهی استفادهی شما از این مکانیزم به ماهیت بستهای که مینویسید بستگی دارد، اما در این حالت فرضی، ممکن است منطقی باشد که به معلم اجازه دهید دانشآموزان را جستجو کند::"
msgid ""
"# bandclass/__main__.py\n"
"\n"
"import sys\n"
"from .student import search_students\n"
"\n"
"student_name = sys.argv[1] if len(sys.argv) >= 2 else ''\n"
"print(f'Found student: {search_students(student_name)}')"
msgstr ""
"# bandclass/__main__.py\n"
"\n"
"import sys\n"
"from .student import search_students\n"
"\n"
"student_name = sys.argv[1] if len(sys.argv) >= 2 else ''\n"
"print(f'Found student: {search_students(student_name)}')"
msgid "Note that ``from .student import search_students`` is an example of a relative import. This import style can be used when referencing modules within a package. For more details, see :ref:`intra-package-references` in the :ref:`tut-modules` section of the tutorial."
msgstr "توجه داشته باشید که ``from .student import search_students`` مثالی از یک ایمپورت نسبی است. این سبک ایمپورت را میتوان هنگام ارجاع به ماژولهای درون یک بسته استفاده کرد. برای اطلاعات بیشتر، به :ref:`intra-package-references` در بخش :ref:`tut-modules` از آموزش مراجعه کنید."
msgid "The content of ``__main__.py`` typically isn't fenced with an ``if __name__ == '__main__'`` block. Instead, those files are kept short and import functions to execute from other modules. Those other modules can then be easily unit-tested and are properly reusable."
msgstr "محتوای ``__main__.py`` معمولاً با یک بلوک ``if __name__ == '__main__'`` محصور نمیشود. در عوض، آن فایلها کوتاه نگه داشته میشوند و توابعی را برای اجرا از ماژولهای دیگر ایمپورت میکنند. سپس آن ماژولهای دیگر را میتوان بهراحتی یونیتتست کرد و بهدرستی قابل استفادهی مجدد هستند."
msgid "If used, an ``if __name__ == '__main__'`` block will still work as expected for a ``__main__.py`` file within a package, because its ``__name__`` attribute will include the package's path if imported::"
msgstr "اگر استفاده شود، یک بلوک ``if __name__ == '__main__'`` همچنان برای یک فایل ``__main__.py`` درون یک بسته بهطور مورد انتظار کار خواهد کرد، زیرا ویژگی ``__name__`` آن در صورت ایمپورت، مسیر بسته را شامل خواهد شد::"
msgid ""
">>> import asyncio.__main__\n"
">>> asyncio.__main__.__name__\n"
"'asyncio.__main__'"
msgstr ""
">>> import asyncio.__main__\n"
">>> asyncio.__main__.__name__\n"
"'asyncio.__main__'"
msgid "This won't work for ``__main__.py`` files in the root directory of a ``.zip`` file though. Hence, for consistency, a minimal ``__main__.py`` without a ``__name__`` check is preferred."
msgstr "با این حال، این برای فایلهای ``__main__.py`` در پوشهی ریشهی یک فایل ``.zip`` کار نخواهد کرد. بنابراین، برای یکدستی، یک ``__main__.py`` حداقلی بدون بررسی ``__name__`` ترجیح داده میشود."
msgid "See :mod:`venv` for an example of a package with a minimal ``__main__.py`` in the standard library. It doesn't contain a ``if __name__ == '__main__'`` block. You can invoke it with ``python -m venv [directory]``."
msgstr "برای مثال یک بسته با یک ``__main__.py`` حداقلی در کتابخانهی استاندارد، به :mod:`venv` مراجعه کنید. این بسته شامل یک بلوک ``if __name__ == '__main__'`` نمیشود. میتوانید آن را با ``python -m venv [directory]`` فراخوانی کنید."
msgid "See :mod:`runpy` for more details on the :option:`-m` flag to the interpreter executable."
msgstr "برای جزئیات بیشتر دربارهی پرچم :option:`-m` به مفسر اجرایی، به :mod:`runpy` مراجعه کنید."
msgid "See :mod:`zipapp` for how to run applications packaged as *.zip* files. In this case Python looks for a ``__main__.py`` file in the root directory of the archive."
msgstr "برای نحوهی اجرای برنامههای بستهبندیشده بهعنوان فایلهای *.zip*، به :mod:`zipapp` مراجعه کنید. در این حالت، پایتون به دنبال یک فایل ``__main__.py`` در پوشهی ریشهی بایگانی میگردد."
msgid "``import __main__``"
msgstr "``import __main__``"
msgid "Regardless of which module a Python program was started with, other modules running within that same program can import the top-level environment's scope (:term:`namespace`) by importing the ``__main__`` module. This doesn't import a ``__main__.py`` file but rather whichever module that received the special name ``'__main__'``."
msgstr "صرفنظر از اینکه یک برنامهی پایتون با کدام ماژول شروع شده است، سایر ماژولهایی که در همان برنامه اجرا میشوند میتوانند با ایمپورت کردن ماژول ``__main__``، به محدودهی محیط سطحبالا (:term:`namespace`) دسترسی پیدا کنند. این کار یک فایل ``__main__.py`` را ایمپورت نمیکند، بلکه هر ماژولی که نام ویژهی ``'__main__'`` را دریافت کرده است، ایمپورت میشود."
msgid "Here is an example module that consumes the ``__main__`` namespace::"
msgstr "در اینجا یک مثال از ماژول آورده شده است که از فضای نام ``__main__`` استفاده میکند::"
msgid ""
"# namely.py\n"
"\n"
"import __main__\n"
"\n"
"def did_user_define_their_name():\n"
" return 'my_name' in dir(__main__)\n"
"\n"
"def print_user_name():\n"
" if not did_user_define_their_name():\n"
" raise ValueError('Define the variable `my_name`!')\n"
"\n"
" print(__main__.my_name)"
msgstr ""
"# namely.py\n"
"\n"
"import __main__\n"
"\n"
"def did_user_define_their_name():\n"
" return 'my_name' in dir(__main__)\n"
"\n"
"def print_user_name():\n"
" if not did_user_define_their_name():\n"
" raise ValueError('Define the variable `my_name`!')\n"
"\n"
" print(__main__.my_name)"
msgid "Example usage of this module could be as follows::"
msgstr "یک نمونه استفاده از این ماژول میتواند به صورت زیر باشد::"
msgid ""
"# start.py\n"
"\n"
"import sys\n"
"\n"
"from namely import print_user_name\n"
"\n"
"# my_name = \"Dinsdale\"\n"
"\n"
"def main():\n"
" try:\n"
" print_user_name()\n"
" except ValueError as ve:\n"
" return str(ve)\n"
"\n"
"if __name__ == \"__main__\":\n"
" sys.exit(main())"
msgstr ""
"# start.py\n"
"\n"
"import sys\n"
"\n"
"from namely import print_user_name\n"
"\n"
"# my_name = \"Dinsdale\"\n"
"\n"
"def main():\n"
" try:\n"
" print_user_name()\n"
" except ValueError as ve:\n"
" return str(ve)\n"
"\n"
"if __name__ == \"__main__\":\n"
" sys.exit(main())"
msgid "Now, if we started our program, the result would look like this:"
msgstr "اکنون، اگر ما برنامهمان را شروع کنیم، نتیجه مثل زیر میشود::"
msgid ""
"$ python start.py\n"
"Define the variable `my_name`!"
msgstr ""
"$ python start.py\n"
"Define the variable `my_name`!"
msgid "The exit code of the program would be 1, indicating an error. Uncommenting the line with ``my_name = \"Dinsdale\"`` fixes the program and now it exits with status code 0, indicating success:"
msgstr "کد خروجی برنامه ۱ خواهد بود که نشاندهندهی خطا است. خارج کردن خط ``my_name = \"Dinsdale\"`` از حالت توضیح (کامنت)، برنامه را اصلاح میکند و اکنون با کد وضعیت ۰ خارج میشود که نشاندهندهی موفقیت است:"
msgid ""
"$ python start.py\n"
"Dinsdale"
msgstr ""
"$ python start.py\n"
"Dinsdale"
msgid "Note that importing ``__main__`` doesn't cause any issues with unintentionally running top-level code meant for script use which is put in the ``if __name__ == \"__main__\"`` block of the ``start`` module. Why does this work?"
msgstr "توجه داشته باشید که ایمپورت کردن ``__main__`` هیچ مشکلی در اجرای ناخواستهی کد سطحبالا که برای استفادهی اسکریپتی در نظر گرفته شده و در بلوک ``if __name__ == \"__main__\"`` ماژول ``start`` قرار دارد، ایجاد نمیکند. چرا این کار میکند؟"
msgid "Python inserts an empty ``__main__`` module in :data:`sys.modules` at interpreter startup, and populates it by running top-level code. In our example this is the ``start`` module which runs line by line and imports ``namely``. In turn, ``namely`` imports ``__main__`` (which is really ``start``). That's an import cycle! Fortunately, since the partially populated ``__main__`` module is present in :data:`sys.modules`, Python passes that to ``namely``. See :ref:`Special considerations for __main__ <import-dunder-main>` in the import system's reference for details on how this works."
msgstr "پایتون در زمان راهاندازی مفسر، یک ماژول خالی ``__main__`` را در :data:`sys.modules` قرار میدهد و آن را با اجرای کد سطحبالا پر میکند. در مثال ما، این ماژول ``start`` است که خط به خط اجرا میشود و ``namely`` را ایمپورت میکند. در ادامه، ``namely`` ماژول ``__main__`` (که در واقع همان ``start`` است) را ایمپورت میکند. این یک چرخهی ایمپورت است! خوشبختانه، از آنجایی که ماژول ``__main__`` تا حدی پر شده در :data:`sys.modules` وجود دارد، پایتون آن را به ``namely`` میدهد. برای جزئیات بیشتر دربارهی نحوهی کار این مکانیزم، به :ref:`Special considerations for __main__ <import-dunder-main>` در مرجع سیستم ایمپورت مراجعه کنید."
msgid "The Python REPL is another example of a \"top-level environment\", so anything defined in the REPL becomes part of the ``__main__`` scope::"
msgstr "REPL پایتون مثال دیگری از یک «محیط سطحبالا» است، بنابراین هر چیزی که در REPL تعریف شود، بخشی از محدودهی ``__main__`` میشود::"
msgid ""
">>> import namely\n"
">>> namely.did_user_define_their_name()\n"
"False\n"
">>> namely.print_user_name()\n"
"Traceback (most recent call last):\n"
"...\n"
"ValueError: Define the variable `my_name`!\n"
">>> my_name = 'Jabberwocky'\n"
">>> namely.did_user_define_their_name()\n"
"True\n"
">>> namely.print_user_name()\n"
"Jabberwocky"
msgstr ""
">>> import namely\n"
">>> namely.did_user_define_their_name()\n"
"False\n"
">>> namely.print_user_name()\n"
"Traceback (most recent call last):\n"
"...\n"
"ValueError: Define the variable `my_name`!\n"
">>> my_name = 'Jabberwocky'\n"
">>> namely.did_user_define_their_name()\n"
"True\n"
">>> namely.print_user_name()\n"
"Jabberwocky"
msgid "The ``__main__`` scope is used in the implementation of :mod:`pdb` and :mod:`rlcompleter`."
msgstr "محدودهی ``__main__`` در پیادهسازی :mod:`pdb` و :mod:`rlcompleter` استفاده میشود."