You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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."
Copy file name to clipboardExpand all lines: library/__main__.po
+29-7Lines changed: 29 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -146,28 +146,31 @@ msgid "In each of these situations, the top-level module's ``__name__`` is set t
146
146
msgstr"در هر کدام از این موقعیتها، ``__name__`` ماژول سطحبالا به ``'__main__'`` تنظیم میشود."
147
147
148
148
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::"
149
-
msgstr""
149
+
msgstr"در نتیجه، یک ماژول میتواند با بررسی ``__name__`` خود متوجه شود که آیا در محیط سطحبالا اجرا میشود یا نه، که این امکان یک الگوی رایج برای اجرای شرطی کد را فراهم میکند زمانی که ماژول از یک دستور ایمپورت مقداردهی اولیه نشده است::"
150
150
151
151
msgid""
152
152
"if __name__ == '__main__':\n"
153
153
" # Execute when the module is not initialized from an import statement.\n"
154
154
" ..."
155
155
msgstr""
156
+
"if __name__ == '__main__':\n"
157
+
" # وقتی ماژول با دستور ایمپورت راهاندازی نشده باشد اجرا میشود.\n"
158
+
" ..."
156
159
157
160
msgid"For a more detailed look at how ``__name__`` is set in all situations, see the tutorial section :ref:`tut-modules`."
158
-
msgstr""
161
+
msgstr"برای نگاهی دقیقتر به نحوهی تنظیم ``__name__`` در همهی موقعیتها، به بخش آموزش :ref:`tut-modules` مراجعه کنید."
159
162
160
163
msgid"Idiomatic Usage"
161
-
msgstr""
164
+
msgstr"استفادهی اصطلاحی"
162
165
163
166
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."
164
-
msgstr""
167
+
msgstr"بعضی ماژولها دارای کدی هستند که فقط برای استفادهی اسکریپتی در نظر گرفته شده است، مانند تجزیهی آرگومانهای خط فرمان یا دریافت داده از ورودی استاندارد. اگر چنین ماژولی از ماژول دیگری ایمپورت شود، مثلاً برای یونیتتست آن، کد اسکریپتی نیز بهطور ناخواسته اجرا خواهد شد."
165
168
166
169
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."
167
-
msgstr""
170
+
msgstr"اینجاست که استفاده از بلوک کد ``if __name__ == '__main__'`` به کار میآید. کد داخل این بلوک اجرا نمیشود مگر اینکه ماژول در محیط سطحبالا اجرا شود."
168
171
169
172
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::"
170
-
msgstr""
173
+
msgstr"قرار دادن کمترین تعداد دستور ممکن در بلوک زیر ``if __name__ == '__main__'`` میتواند وضوح و صحت کد را بهبود بخشد. اغلب، یک تابع به نام ``main`` رفتار اصلی برنامه را در خود محصور میکند::"
171
174
172
175
msgid""
173
176
"# echo.py\n"
@@ -190,9 +193,28 @@ msgid ""
190
193
"if __name__ == '__main__':\n"
191
194
" sys.exit(main()) # next section explains the use of sys.exit"
192
195
msgstr""
196
+
"# echo.py\n"
197
+
"\n"
198
+
"import shlex\n"
199
+
"import sys\n"
200
+
"\n"
201
+
"def echo(phrase: str) -> None:\n"
202
+
" \"\"\"یک پوشش ساختگی برای print.\"\"\"\n"
203
+
" # برای اهداف نمایشی، میتوانید تصور کنید که منطق ارزشمندی\n"
204
+
" # و قابل استفادهی مجددی درون این تابع وجود دارد\n"
205
+
" print(phrase)\n"
206
+
"\n"
207
+
"def main() -> int:\n"
208
+
" \"\"\"آرگومانهای ورودی را به خروجی استاندارد بازتاب میدهد\"\"\"\n"
209
+
" phrase = shlex.join(sys.argv)\n"
210
+
" echo(phrase)\n"
211
+
" return 0\n"
212
+
"\n"
213
+
"if __name__ == '__main__':\n"
214
+
" sys.exit(main()) # بخش بعدی استفاده از sys.exit را توضیح میدهد"
193
215
194
216
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."
195
-
msgstr""
217
+
msgstr"توجه داشته باشید که اگر ماژول کد را درون تابع ``main`` محصور نمیکرد و به جای آن مستقیماً آن را درون بلوک ``if __name__ == '__main__'`` قرار میداد، متغیر ``phrase`` برای کل ماژول سراسری میشد. این امر مستعد خطا است، زیرا ممکن است سایر توابع درون ماژول بهطور ناخواسته از متغیر سراسری به جای یک نام محلی استفاده کنند. تابع ``main`` این مشکل را حل میکند."
196
218
197
219
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__'``."
0 commit comments