"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"
"X-Generator: Poedit 3.6\n"
msgid"Functional Programming HOWTO"
msgstr"راهنمای عملی برنامهنویسی تابعی"
msgid"In this document, we'll take a tour of Python's features suitable for implementing programs in a functional style. After an introduction to the concepts of functional programming, we'll look at language features such as :term:`iterator`\\s and :term:`generator`\\s and relevant library modules such as :mod:`itertools` and :mod:`functools`."
msgstr"در این سند، مروری بر ویژگیهای پایتون مناسب برای پیادهسازی برنامهها به سبک تابعی خواهیم داشت. پس از مقدمهای بر مفاهیم برنامهنویسی تابعی، به ویژگیهای زبانی مانند :term:`iterator`\\s و :term:`تولیدگر <generator>`\\s و ماژولهای کتابخانهای مرتبط مانند :mod:`itertools` و :mod:`functools` میپردازیم."
msgid"Introduction"
msgstr"مقدمه"
msgid"This section explains the basic concept of functional programming; if you're just interested in learning about Python language features, skip to the next section on :ref:`functional-howto-iterators`."
msgstr"این بخش مفهوم پایهی برنامهنویسی تابعی را توضیح میدهد؛ اگر فقط به یادگیری ویژگیهای زبان پایتون علاقه دارید، به بخش بعدی دربارهی :ref:`functional-howto-iterators` بروید."
msgid"Programming languages support decomposing problems in several different ways:"
msgstr"زبانهای برنامهنویسی امکان تجزیه مسائل را به چندین روش مختلف فراهم میکنند:"
msgid"Most programming languages are **procedural**: programs are lists of instructions that tell the computer what to do with the program's input. C, Pascal, and even Unix shells are procedural languages."
msgstr"بیشتر زبانهای برنامهنویسی **رویهای** هستند: برنامهها فهرستهایی از دستورالعملها هستند که به رایانه میگویند با ورودی برنامه چه کاری انجام دهد. C، Pascal و حتی پوستههای یونیکس زبانهای رویهای هستند."
msgid"In **declarative** languages, you write a specification that describes the problem to be solved, and the language implementation figures out how to perform the computation efficiently. SQL is the declarative language you're most likely to be familiar with; a SQL query describes the data set you want to retrieve, and the SQL engine decides whether to scan tables or use indexes, which subclauses should be performed first, etc."
msgstr"در زبانهای **اعلانی**، شما مشخصاتی مینویسید که مسئلهی موردحل را توصیف میکند و پیادهسازی زبان تشخیص میدهد که چگونه محاسبات را بهصورت کارآمد انجام دهد. SQL زبان اعلانیای است که احتمالاً بیش از همه با آن آشنا هستید؛ یک پرسوجوی SQL مجموعهدادهای را که میخواهید بازیابی کنید توصیف میکند و موتور SQL تصمیم میگیرد که آیا جدولها را پویش کند یا از اندیسها استفاده کند، کدام زیربند باید ابتدا اجرا شود و غیره."
msgid"**Object-oriented** programs manipulate collections of objects. Objects have internal state and support methods that query or modify this internal state in some way. Smalltalk and Java are object-oriented languages. C++ and Python are languages that support object-oriented programming, but don't force the use of object-oriented features."
msgstr"برنامههای **شیءگرا** مجموعههایی از اشیاء را دستکاری میکنند. اشیاء دارای وضعیت داخلی هستند و از متدهایی پشتیبانی میکنند که این وضعیت داخلی را به شکلی پرسوجو یا تغییر میدهند. Smalltalk و Java زبانهای شیءگرا هستند. C++ و Python زبانهایی هستند که از برنامهنویسی شیءگرا پشتیبانی میکنند، اما استفاده از قابلیتهای شیءگرا را تحمیل نمیکنند."
msgid"**Functional** programming decomposes a problem into a set of functions. Ideally, functions only take inputs and produce outputs, and don't have any internal state that affects the output produced for a given input. Well-known functional languages include the ML family (Standard ML, OCaml, and other variants) and Haskell."
msgstr"برنامهنویسی تابعی یک مسئله را به مجموعهای از توابع تجزیه میکند. در حالت ایدهآل، توابع تنها ورودیها را دریافت میکنند و خروجیها را تولید میکنند و هیچ وضعیت داخلیای ندارند که بر خروجی تولیدشده برای یک ورودی مشخص تأثیر بگذارد. زبانهای تابعی شناختهشده شامل خانواده ML (شامل Standard ML، OCaml و سایر گونهها) و Haskell هستند."
msgid"The designers of some computer languages choose to emphasize one particular approach to programming. This often makes it difficult to write programs that use a different approach. Other languages are multi-paradigm languages that support several different approaches. Lisp, C++, and Python are multi-paradigm; you can write programs or libraries that are largely procedural, object-oriented, or functional in all of these languages. In a large program, different sections might be written using different approaches; the GUI might be object-oriented while the processing logic is procedural or functional, for example."
msgstr "طراحان برخی زبانهای رایانهای انتخاب میکنند که بر یک رویکرد خاص به برنامهنویسی تأکید کنند. این موضوع اغلب نوشتن برنامههایی را که از رویکرد متفاوتی استفاده میکنند دشوار میکند. سایر زبانها، زبانهای چندپارادایمی هستند که از چندین رویکرد متفاوت پشتیبانی میکنند. Lisp، C++ و Python چندپارادایمی هستند؛ شما میتوانید در همهی این زبانها برنامهها یا کتابخانههایی بنویسید که عمدتاً رویهای، شیءگرا یا تابعی هستند. در یک برنامهی بزرگ، ممکن است بخشهای مختلف با استفاده از رویکردهای متفاوتی نوشته شوند؛ برای مثال، ممکن است GUI شیءگرا باشد، در حالی که منطق پردازش رویهای یا تابعی است."
msgid"In a functional program, input flows through a set of functions. Each function operates on its input and produces some output. Functional style discourages functions with side effects that modify internal state or make other changes that aren't visible in the function's return value. Functions that have no side effects at all are called **purely functional**. Avoiding side effects means not using data structures that get updated as a program runs; every function's output must only depend on its input."
msgstr"در یک برنامه تابعی، ورودی از میان مجموعهای از توابع عبور میکند. هر تابع روی ورودی خود عمل میکند و خروجیای تولید میکند. سبک تابعی از توابع دارای عوارض جانبیای که وضعیت داخلی را تغییر میدهند یا تغییرات دیگری ایجاد میکنند که در مقدار بازگشتی تابع قابل مشاهده نیستند، پرهیز میکند. به توابعی که هیچگونه عارضه جانبی ندارند، **کاملاً تابعی** گفته میشود. اجتناب از عوارض جانبی به معنای عدم استفاده از ساختارهای دادهای است که در حین اجرای برنامه بهروزرسانی میشوند؛ خروجی هر تابع باید تنها به ورودی آن وابسته باشد."
msgid"Some languages are very strict about purity and don't even have assignment statements such as ``a=3`` or ``c = a + b``, but it's difficult to avoid all side effects, such as printing to the screen or writing to a disk file. Another example is a call to the :func:`print` or :func:`time.sleep` function, neither of which returns a useful value. Both are called only for their side effects of sending some text to the screen or pausing execution for a second."
msgstr"برخی زبانها در مورد خلوص بسیار سختگیر هستند و حتی دستور انتسابی مانند ``a=3`` یا ``c = a + b`` ندارند، اما اجتناب از تمام عوارض جانبی، مانند چاپ روی صفحهنمایش یا نوشتن در یک پرونده روی دیسک، دشوار است. مثالی دیگر، فراخوانی تابع :func:`print` یا :func:`time.sleep` است که هیچکدام مقدار مفیدی برنمیگرداند. هر دو فقط برای عوارض جانبیشان، یعنی ارسال مقداری متن به صفحهنمایش یا مکث در اجرا برای یک ثانیه، فراخوانی میشوند."
msgid"Python programs written in functional style usually won't go to the extreme of avoiding all I/O or all assignments; instead, they'll provide a functional-appearing interface but will use non-functional features internally. For example, the implementation of a function will still use assignments to local variables, but won't modify global variables or have other side effects."
msgstr"برنامههای پایتون نوشتهشده به سبک تابعی معمولاً تا سرحد اجتناب کامل از تمام ورودی/خروجیها یا تمام انتسابها پیش نمیروند؛ در عوض، رابطی با ظاهر تابعی ارائه میدهند، اما بهصورت درونی از قابلیتهای غیرتابعی استفاده میکنند. برای مثال، پیادهسازی یک تابع همچنان از انتسابها به متغیرهای محلی استفاده میکند، اما متغیرهای سراسری را تغییر نمیدهد و عوارض جانبی دیگری ندارد."
msgid"Functional programming can be considered the opposite of object-oriented programming. Objects are little capsules containing some internal state along with a collection of method calls that let you modify this state, and programs consist of making the right set of state changes. Functional programming wants to avoid state changes as much as possible and works with data flowing between functions. In Python you might combine the two approaches by writing functions that take and return instances representing objects in your application (e-mail messages, transactions, etc.)."
msgstr "برنامهنویسی تابعی میتواند متضاد برنامهنویسی شیءگرا در نظر گرفته شود. شیءها کپسولهای کوچکی هستند که حاوی وضعیتی داخلی به همراه مجموعهای از فراخوانیهای متد هستند که به شما اجازه میدهند این وضعیت را تغییر دهید، و برنامهها از ایجاد مجموعهای درست از تغییرات وضعیت تشکیل شدهاند. برنامهنویسی تابعی میخواهد تا حد امکان از تغییرات وضعیت اجتناب کند و با جریان داده بین توابع کار میکند. در پایتون ممکن است این دو رویکرد را با نوشتن توابعی ترکیب کنید که نمونههایی نشاندهندهی اشیایی در برنامه شما (پیامهای ایمیل، تراکنشها و غیره) را دریافت میکنند و برمیگردانند."
msgid"Functional design may seem like an odd constraint to work under. Why should you avoid objects and side effects? There are theoretical and practical advantages to the functional style:"
msgstr"طراحی تابعی ممکن است محدودیتی عجیب برای کار کردن تحت آن به نظر برسد. چرا باید از اشیاء و عوارض جانبی اجتناب کنید؟ سبک تابعی مزایای نظری و عملی دارد:"
msgid"Formal provability."
msgstr"اثباتپذیری صوری."
msgid"Modularity."
msgstr"پیمانهای بودن."
msgid"Composability."
msgstr"ترکیبپذیری."
msgid"Ease of debugging and testing."
msgstr"سهولت اشکالزدایی و آزمون."
msgid"Formal provability"
msgstr"اثباتپذیری صوری"
msgid"A theoretical benefit is that it's easier to construct a mathematical proof that a functional program is correct."
msgstr"یک مزیت نظری این است که ساختن اثبات ریاضی برای صحیح بودن یک برنامه تابعی آسانتر است."
msgid"For a long time researchers have been interested in finding ways to mathematically prove programs correct. This is different from testing a program on numerous inputs and concluding that its output is usually correct, or reading a program's source code and concluding that the code looks right; the goal is instead a rigorous proof that a program produces the right result for all possible inputs."
msgstr"برای مدت طولانی، پژوهشگران به یافتن راههایی برای اثبات ریاضیاتی صحت برنامهها علاقهمند بودهاند. این امر با آزمایش یک برنامه روی ورودیهای متعدد و نتیجهگیری اینکه خروجی آن معمولاً صحیح است، یا خواندن کد منبع یک برنامه و نتیجهگیری اینکه کد درست به نظر میرسد، متفاوت است؛ در عوض، هدف اثبات دقیقی است که نشان میدهد یک برنامه نتیجه درست را برای تمام ورودیهای ممکن تولید میکند."
msgid"The technique used to prove programs correct is to write down **invariants**, properties of the input data and of the program's variables that are always true. For each line of code, you then show that if invariants X and Y are true **before** the line is executed, the slightly different invariants X' and Y' are true **after** the line is executed. This continues until you reach the end of the program, at which point the invariants should match the desired conditions on the program's output."
msgstr"روشی که برای اثبات صحت برنامهها به کار میرود، نوشتن **ناورداها** است؛ یعنی ویژگیهایی از دادههای ورودی و متغیرهای برنامه که همیشه برقرار هستند. سپس برای هر خط کد، نشان میدهید که اگر ناورداهای X و Y **پیش از** اجرای آن خط برقرار باشند، ناورداهای اندکی متفاوت X' و Y' **پس از** اجرای آن خط برقرار خواهند بود. این کار تا رسیدن شما به پایان برنامه ادامه مییابد؛ در آن نقطه، ناورداها باید با شرایط مطلوب برای خروجی برنامه مطابقت کنند."
msgid"Functional programming's avoidance of assignments arose because assignments are difficult to handle with this technique; assignments can break invariants that were true before the assignment without producing any new invariants that can be propagated onward."
msgstr"پرهیز برنامهنویسی تابعی از انتسابها به این دلیل بهوجود آمد که مدیریت انتسابها با این روش دشوار است؛ انتسابها میتوانند ناورداهایی را که پیش از انتساب برقرار بودند نقض کنند، بدون آنکه ناوردای جدیدی ایجاد کنند که بتوان آن را به مراحل بعدی منتقل کرد."
msgid"Unfortunately, proving programs correct is largely impractical and not relevant to Python software. Even trivial programs require proofs that are several pages long; the proof of correctness for a moderately complicated program would be enormous, and few or none of the programs you use daily (the Python interpreter, your XML parser, your web browser) could be proven correct. Even if you wrote down or generated a proof, there would then be the question of verifying the proof; maybe there's an error in it, and you wrongly believe you've proved the program correct."
msgstr"متأسفانه، اثبات صحت برنامهها تا حد زیادی غیرعملی است و ارتباطی به نرمافزار پایتون ندارد. حتی برنامههای بدیهی نیز به اثباتهایی چندینصفحهای نیاز دارند؛ اثبات صحت یک برنامهی نسبتاً پیچیده بسیار بزرگ خواهد بود، و بسیار کم یا هیچکدام از برنامههایی که روزانه استفاده میکنید (مفسر پایتون، پارسرٔ XML شما، مرورگر وب شما) را نمیتوان اثبات صحت کرد. حتی اگر اثباتی را یادداشت یا تولید کنید، آنگاه مسئلهی تأیید آن اثبات مطرح میشود؛ شاید خطایی در آن وجود داشته باشد و شما بهاشتباه باور داشته باشید که صحت برنامه را اثبات کردهاید."
msgid"Modularity"
msgstr"ماژولار بودن"
msgid"A more practical benefit of functional programming is that it forces you to break apart your problem into small pieces. Programs are more modular as a result. It's easier to specify and write a small function that does one thing than a large function that performs a complicated transformation. Small functions are also easier to read and to check for errors."
msgstr"یک مزیت عملیتر برنامهنویسی تابعی این است که شما را وادار میکند مسئلهی خود را به بخشهای کوچک تقسیم کنید. در نتیجه، برنامهها ماژولارتر میشوند. مشخص کردن و نوشتن یک تابع کوچک که یک کار انجام میدهد، آسانتر از مشخص کردن و نوشتن یک تابع بزرگ است که یک تبدیل پیچیده را انجام میدهد. توابع کوچک نیز برای خواندن و بررسی خطاها آسانتر هستند."
msgid"Ease of debugging and testing"
msgstr"سهولت اشکالزدایی و آزمون"
msgid"Testing and debugging a functional-style program is easier."
msgstr"آزمون و اشکالزدایی یک برنامه به سبک تابعی آسانتر است."
msgid"Debugging is simplified because functions are generally small and clearly specified. When a program doesn't work, each function is an interface point where you can check that the data are correct. You can look at the intermediate inputs and outputs to quickly isolate the function that's responsible for a bug."
msgstr"اشکالزدایی سادهتر میشود، زیرا توابع عموماً کوچک و بهوضوح مشخصشدهاند. هنگامی که برنامهای کار نمیکند، هر تابع یک نقطهی رابط است که میتوانید در آن درست بودن دادهها را بررسی کنید. شما میتوانید با بررسی ورودیها و خروجیهای میانی، تابعی را که مسئول یک اشکال است، بهسرعت جدا کنید."
msgid"Testing is easier because each function is a potential subject for a unit test. Functions don't depend on system state that needs to be replicated before running a test; instead you only have to synthesize the right input and then check that the output matches expectations."
msgstr"آزمون آسانتر است، زیرا هر تابع موضوع بالقوهای برای یک آزمون واحد است. توابع به وضعیت سیستمی که باید پیش از اجرای یک آزمون بازتولید شود وابسته نیستند؛ در عوض فقط باید ورودی درست را بسازید و سپس بررسی کنید که خروجی با انتظارات مطابقت دارد."
msgid"Composability"
msgstr"ترکیبپذیری"
msgid"As you work on a functional-style program, you'll write a number of functions with varying inputs and outputs. Some of these functions will be unavoidably specialized to a particular application, but others will be useful in a wide variety of programs. For example, a function that takes a directory path and returns all the XML files in the directory, or a function that takes a filename and returns its contents, can be applied to many different situations."
msgstr"هنگامی که روی برنامهای به سبک تابعی کار میکنید، تعدادی تابع با ورودیها و خروجیهای متفاوت خواهید نوشت. برخی از این توابع ناگزیر برای یک کاربرد خاص تخصصی خواهند بود، اما برخی دیگر در طیف وسیعی از برنامهها مفید خواهند بود. برای مثال، تابعی که مسیر یک پوشه را دریافت میکند و همه پروندههای XML موجود در آن پوشه را برمیگرداند، یا تابعی که نام یک پرونده را دریافت میکند و محتویات آن را برمیگرداند، میتواند در موقعیتهای مختلف بسیاری کاربرد داشته باشد."
msgid"Over time you'll form a personal library of utilities. Often you'll assemble new programs by arranging existing functions in a new configuration and writing a few functions specialized for the current task."
msgstr"بهمرور زمان، شما کتابخانهای شخصی از ابزارهای کاربردی تشکیل خواهید داد. اغلب، برنامههای جدید را با چیدمان توابع موجود در یک پیکربندی جدید و نوشتن چند تابع مخصوص وظیفهی جاری خواهید ساخت."
msgid"Iterators"
msgstr"پیمایشگرها"
msgid"I'll start by looking at a Python language feature that's an important foundation for writing functional-style programs: iterators."
msgstr"با بررسی ویژگیای از زبان پایتون آغاز میکنم که پایهای مهم برای نوشتن برنامهها بهسبک تابعی است: پیمایشگرها ."
msgid"An iterator is an object representing a stream of data; this object returns the data one element at a time. A Python iterator must support a method called :meth:`~iterator.__next__` that takes no arguments and always returns the next element of the stream. If there are no more elements in the stream, :meth:`~iterator.__next__` must raise the :exc:`StopIteration` exception. Iterators don't have to be finite, though; it's perfectly reasonable to write an iterator that produces an infinite stream of data."
msgstr"پیمایشگر شیءای است که جریانی از داده را نشان میدهد؛ این شیء دادهها را هر بار یک عنصر برمیگرداند. یک پیمایشگر پایتون باید از متدی به نام :meth:`~iterator.__next__` پشتیبانی کند که هیچ آرگومانی نمیگیرد و همیشه عنصر بعدی جریان را برمیگرداند. اگر دیگر عنصری در جریان وجود نداشته باشد، :meth:`~iterator.__next__` باید استثنای :exc:`StopIteration` را پرتاب کند. البته پیمایشگرها لازم نیست متناهی باشند؛ نوشتن پیمایشگری که جریان نامتناهی از داده تولید میکند، کاملاً معقول است."
msgid"The built-in :func:`iter` function takes an arbitrary object and tries to return an iterator that will return the object's contents or elements, raising :exc:`TypeError` if the object doesn't support iteration. Several of Python's built-in data types support iteration, the most common being lists and dictionaries. An object is called :term:`iterable` if you can get an iterator for it."
msgstr"تابع توکار :func:`iter` یک شیء دلخواه را میگیرد و تلاش میکند پیمایشگری برگرداند که محتویات یا عناصر شیء را برمیگرداند، و اگر شیء از پیمایش پشتیبانی نکند، :exc:`TypeError` را پرتاب میکند. چندین نوع دادهی توکار پایتون از پیمایش پشتیبانی میکنند، که رایجترین آنها فهرستها و دیکشنریها هستند. یک شیء :term:`پیمایشپذیر <iterable>` (پیمایشپذیر) نامیده میشود اگر بتوانید پیمایشگری برای آن دریافت کنید."
msgid"You can experiment with the iteration interface manually:"
msgstr"میتوانید رابط پیمایش را بهصورت دستی آزمایش کنید:"
msgid"Python expects iterable objects in several different contexts, the most important being the :keyword:`for` statement. In the statement ``for X in Y``, Y must be an iterator or some object for which :func:`iter` can create an iterator. These two statements are equivalent::"
msgstr"پایتون در چندین زمینه مختلف، اشیاء پیمایشپذیر را انتظار دارد، مهمترین آنها دستور :keyword:`for` است. در دستور ``for X in Y``، Y باید یک پیمایشگر باشد یا شیءای که :func:`iter` بتواند برای آن یک پیمایشگر ایجاد کند. این دو دستور معادل هستند::"
msgid""
"for i in iter(obj):\n"
" print(i)\n"
"\n"
"for i in obj:\n"
" print(i)"
msgstr""
"for i in iter(obj):\n"
" print(i)\n"
"\n"
"for i in obj:\n"
" print(i)"
msgid"Iterators can be materialized as lists or tuples by using the :func:`list` or :func:`tuple` constructor functions:"
msgstr"پیمایشگرها را میتوان با استفاده از توابع سازندهی :func:`list` یا :func:`tuple` بهصورت فهرست یا تاپل درآورد:"
msgid"Sequence unpacking also supports iterators: if you know an iterator will return N elements, you can unpack them into an N-tuple:"
msgstr"واگشایی دنباله همچنین از پیمایشگرها پشتیبانی میکند: اگر میدانید یک پیمایشگر N عنصر برمیگرداند، میتوانید آنها را در یک N-تایی واگشایی کنید:"
msgid"Built-in functions such as :func:`max` and :func:`min` can take a single iterator argument and will return the largest or smallest element. The ``\"in\"`` and ``\"not in\"`` operators also support iterators: ``X in iterator`` is true if X is found in the stream returned by the iterator. You'll run into obvious problems if the iterator is infinite; :func:`max`, :func:`min` will never return, and if the element X never appears in the stream, the ``\"in\"`` and ``\"not in\"`` operators won't return either."
msgstr"توابع توکار مانند :func:`max` و :func:`min` میتوانند یک آرگومان پیمایشگر بگیرند و بزرگترین یا کوچکترین عنصر را برمیگردانند. عملگرهای ``\"in\"`` و ``\"not in\"`` نیز از پیمایشگرها پشتیبانی میکنند: ``X in iterator`` زمانی درست است که X در جریان برگرداندهشده توسط پیمایشگر پیدا شود. اگر پیمایشگر بیپایان باشد، با مشکلات آشکاری مواجه خواهید شد؛ :func:`max` و :func:`min` هرگز برنمیگردند، و اگر عنصر X هرگز در جریان ظاهر نشود، عملگرهای ``\"in\"`` و ``\"not in\"`` نیز برنمیگردند."
msgid"Note that you can only go forward in an iterator; there's no way to get the previous element, reset the iterator, or make a copy of it. Iterator objects can optionally provide these additional capabilities, but the iterator protocol only specifies the :meth:`~iterator.__next__` method. Functions may therefore consume all of the iterator's output, and if you need to do something different with the same stream, you'll have to create a new iterator."
msgstr"توجه داشته باشید که در یک پیمایشگر فقط میتوانید به جلو بروید؛ هیچ راهی برای دریافت عنصر قبلی، بازنشانی پیمایشگر، یا ایجاد یک کپی از آن وجود ندارد. اشیای پیمایشگر میتوانند بهصورت اختیاری این قابلیتهای اضافی را فراهم کنند، اما پروتکل پیمایشگر فقط متد :meth:`~iterator.__next__` را مشخص میکند. بنابراین توابع ممکن است تمام خروجی پیمایشگر را مصرف کنند، و اگر نیاز دارید کار متفاوتی با همان جریان انجام دهید، باید یک پیمایشگر جدید ایجاد کنید."
msgid"Data Types That Support Iterators"
msgstr"انواع دادهای که از پیمایشگرها پشتیبانی میکنند"
msgid"We've already seen how lists and tuples support iterators. In fact, any Python sequence type, such as strings, will automatically support creation of an iterator."
msgstr"پیشتر دیدهایم که فهرستها و تاپلها از پیمایشگرها پشتیبانی میکنند. در واقع، هر نوع دنبالهای در پایتون، مانند رشتهها، بهطور خودکار از ایجاد یک پیمایشگر پشتیبانی میکند."
msgid"Calling :func:`iter` on a dictionary returns an iterator that will loop over the dictionary's keys::"
msgstr"فراخوانی :func:`iter` روی یک دیکشنری، پیمایشگری برمیگرداند که روی کلیدهای دیکشنری حلقه میزند::"
msgid"Note that starting with Python 3.7, dictionary iteration order is guaranteed to be the same as the insertion order. In earlier versions, the behaviour was unspecified and could vary between implementations."
msgstr"توجه داشته باشید که از Python 3.7 به بعد، تضمین میشود که ترتیب پیمایش دیکشنری همان ترتیب درج باشد. در نسخههای پیشین، این رفتار مشخص نشده بود و میتوانست بین پیادهسازیها متفاوت باشد."
msgid"Applying :func:`iter` to a dictionary always loops over the keys, but dictionaries have methods that return other iterators. If you want to iterate over values or key/value pairs, you can explicitly call the :meth:`~dict.values` or :meth:`~dict.items` methods to get an appropriate iterator."
msgstr"اعمال :func:`iter` روی یک دیکشنری همیشه روی کلیدها حلقه میزند، اما دیکشنریها متدهایی دارند که پیمایشگرهای دیگری برمیگردانند. اگر بخواهید روی مقادیر یا جفتهای کلید/مقدار پیمایش کنید، میتوانید بهصراحت متدهای :meth:`~dict.values` یا :meth:`~dict.items` را فراخوانی کنید تا پیمایشگر مناسبی به دست آورید."
msgid"The :func:`dict` constructor can accept an iterator that returns a finite stream of ``(key, value)`` tuples:"
msgstr"سازندهی :func:`dict` میتواند پیمایشگری را بپذیرد که جریان متناهی از تاپلهای ``(key, value)`` را برمیگرداند:"
msgid"Files also support iteration by calling the :meth:`~io.TextIOBase.readline` method until there are no more lines in the file. This means you can read each line of a file like this::"
msgstr"پروندهها همچنین از پیمایش از طریق فراخوانی متد :meth:`~io.TextIOBase.readline` تا زمانی که دیگر سطری در پرونده وجود نداشته باشد، پشتیبانی میکنند. این بدان معناست که میتوانید هر خط از یک پرونده را به این صورت بخوانید::"
msgid""
"for line in file:\n"
" # do something for each line\n"
" ..."
msgstr""
"for line in file:\n"
" # do something for each line\n"
" ..."
msgid"Sets can take their contents from an iterable and let you iterate over the set's elements::"
msgstr"مجموعهها میتوانند محتوای خود را از یک پیمایشپذیر بگیرند و به شما اجازه دهند عناصر مجموعه را پیمایش کنید::"
msgid""
">>> S = {2, 3, 5, 7, 11, 13}\n"
">>> for i in S:\n"
"... print(i)\n"
"2\n"
"3\n"
"5\n"
"7\n"
"11\n"
"13"
msgstr""
">>> S = {2, 3, 5, 7, 11, 13}\n"
">>> for i in S:\n"
"... print(i)\n"
"2\n"
"3\n"
"5\n"
"7\n"
"11\n"
"13"
msgid"Generator expressions and list comprehensions"
msgstr"عبارتهای تولیدگر و درکهای فهرستی"
msgid"Two common operations on an iterator's output are 1) performing some operation for every element, 2) selecting a subset of elements that meet some condition. For example, given a list of strings, you might want to strip off trailing whitespace from each line or extract all the strings containing a given substring."
msgstr"دو عملیات رایج روی خروجی یک پیمایشگر عبارتاند از ۱) انجام عملیاتی برای هر عنصر، ۲) انتخاب زیرمجموعهای از عناصر که شرطی را برآورده میکنند. برای مثال، با داشتن فهرستی از رشتهها، ممکن است بخواهید فضای سفید انتهایی هر خط را حذف کنید یا تمام رشتههایی که حاوی یک زیررشتهی مشخص هستند را استخراج کنید."
msgid"List comprehensions and generator expressions (short form: \"listcomps\" and \"genexps\") are a concise notation for such operations, borrowed from the functional programming language Haskell (https://www.haskell.org/). You can strip all the whitespace from a stream of strings with the following code::"
msgstr"درکهای فهرستی و عبارات تولیدگر (شکل کوتاه: «listcomps» و «genexps») نمادگذاری مختصری برای چنین عملیاتی هستند که از زبان برنامهنویسی تابعی Haskell (https://www.haskell.org/) وام گرفته شدهاند. میتوانید با کد زیر تمام فضای سفید را از یک جریان از رشتهها حذف کنید::"
">>> stripped_iter = (line.strip() for line in line_list)\n"
"\n"
">>> # List comprehension -- returns list\n"
">>> stripped_list = [line.strip() for line in line_list]"
msgid"You can select only certain elements by adding an ``\"if\"`` condition::"
msgstr"شما میتوانید با افزودن یک شرط ``\"if\"``، فقط عناصر خاصی را انتخاب کنید::"
msgid""
">>> stripped_list = [line.strip() for line in line_list\n"
"... if line != \"\"]"
msgstr""
">>> stripped_list = [line.strip() for line in line_list\n"
"... if line != \"\"]"
msgid"With a list comprehension, you get back a Python list; ``stripped_list`` is a list containing the resulting lines, not an iterator. Generator expressions return an iterator that computes the values as necessary, not needing to materialize all the values at once. This means that list comprehensions aren't useful if you're working with iterators that return an infinite stream or a very large amount of data. Generator expressions are preferable in these situations."
msgstr"با یک درک فهرستی، شما یک فهرست پایتون دریافت میکنید؛ ``stripped_list`` فهرستی شامل ردیفهای حاصل است، نه یک پیمایشگر. عبارتهای تولیدگر یک پیمایشگر برمیگردانند که مقادیر را در حد نیاز محاسبه میکند و نیازی به ایجاد همه مقادیر بهصورت یکجا ندارد. این بدان معناست که اگر با پیمایشگرهایی کار میکنید که یک جریان بینهایت یا مقدار بسیار زیادی از داده را برمیگردانند، درکهای فهرستی مفید نیستند. در این موقعیتها، عبارتهای تولیدگر ترجیح داده میشوند."
msgid"Generator expressions are surrounded by parentheses (\"()\") and list comprehensions are surrounded by square brackets (\"[]\"). Generator expressions have the form::"
msgstr"عبارتهای تولیدگر در میان پرانتز (\"()\") و درکهای فهرستی در میان کروشه (\"[]\") قرار دارند. عبارتهای تولیدگر به این شکل هستند::"
msgid""
"( expression for expr in sequence1\n"
" if condition1\n"
" for expr2 in sequence2\n"
" if condition2\n"
" for expr3 in sequence3\n"
" if condition3\n"
" ...\n"
" for exprN in sequenceN\n"
" if conditionN )"
msgstr""
"( expression for expr in sequence1\n"
" if condition1\n"
" for expr2 in sequence2\n"
" if condition2\n"
" for expr3 in sequence3\n"
" if condition3\n"
" ...\n"
" for exprN in sequenceN\n"
" if conditionN )"
msgid"Again, for a list comprehension only the outside brackets are different (square brackets instead of parentheses)."
msgstr"باز هم، برای درک فهرستی تنها کروشههای بیرونی متفاوت هستند (کروشههای مربع بهجای پرانتزها)."
msgid"The elements of the generated output will be the successive values of ``expression``. The ``if`` clauses are all optional; if present, ``expression`` is only evaluated and added to the result when ``condition`` is true."
msgstr"عناصر خروجی تولیدشده، مقادیر متوالی ``expression`` خواهند بود. بندهای ``if`` همگی اختیاری هستند؛ در صورت وجود، ``expression`` تنها زمانی ارزیابی و به نتیجه اضافه میشود که ``condition`` درست باشد."
msgid"Generator expressions always have to be written inside parentheses, but the parentheses signalling a function call also count. If you want to create an iterator that will be immediately passed to a function you can write::"
msgstr"عبارتهای تولیدگر همیشه باید داخل پرانتز نوشته شوند، اما پرانتزهای نشاندهندهی فراخوانی تابع نیز بهحساب میآیند. اگر بخواهید پیمایشگری بسازید که بلافاصله به یک تابع پاس داده شود، میتوانید بنویسید::"
msgid"obj_total = sum(obj.count for obj in list_all_objects())"
msgstr"obj_total = sum(obj.count for obj in list_all_objects())"
msgid"The ``for...in`` clauses contain the sequences to be iterated over. The sequences do not have to be the same length, because they are iterated over from left to right, **not** in parallel. For each element in ``sequence1``, ``sequence2`` is looped over from the beginning. ``sequence3`` is then looped over for each resulting pair of elements from ``sequence1`` and ``sequence2``."
msgstr"بندهای ``for...in`` شامل دنبالههایی هستند که باید پیمایش شوند. دنبالهها لازم نیست همطول باشند، زیرا از چپ به راست پیمایش میشوند، **نه** بهصورت موازی. برای هر عنصر در ``sequence1``، روی ``sequence2`` از ابتدا حلقه زده میشود. سپس ``sequence3`` برای هر جفت عنصر حاصل از ``sequence1`` و ``sequence2`` حلقه زده میشود."
msgid"To put it another way, a list comprehension or generator expression is equivalent to the following Python code::"
msgstr"بهبیان دیگر، یک درک فهرستی یا عبارت تولیدگر معادل کد پایتون زیر است::"
msgid""
"for expr1 in sequence1:\n"
" if not (condition1):\n"
" continue # Skip this element\n"
" for expr2 in sequence2:\n"
" if not (condition2):\n"
" continue # Skip this element\n"
" ...\n"
" for exprN in sequenceN:\n"
" if not (conditionN):\n"
" continue # Skip this element\n"
"\n"
" # Output the value of\n"
" # the expression."
msgstr""
"for expr1 in sequence1:\n"
" if not (condition1):\n"
" continue # Skip this element\n"
" for expr2 in sequence2:\n"
" if not (condition2):\n"
" continue # Skip this element\n"
" ...\n"
" for exprN in sequenceN:\n"
" if not (conditionN):\n"
" continue # Skip this element\n"
"\n"
" # Output the value of\n"
" # the expression."
msgid"This means that when there are multiple ``for...in`` clauses but no ``if`` clauses, the length of the resulting output will be equal to the product of the lengths of all the sequences. If you have two lists of length 3, the output list is 9 elements long:"
msgstr"این بدان معناست که وقتی چندین بند ``for...in`` وجود داشته باشد اما هیچ بند ``if`` وجود نداشته باشد، طول خروجی حاصل برابر با حاصلضرب طول همه دنبالهها خواهد بود. اگر دو فهرست به طول ۳ داشته باشید، فهرست خروجی ۹ عنصر طول دارد:"
msgid"To avoid introducing an ambiguity into Python's grammar, if ``expression`` is creating a tuple, it must be surrounded with parentheses. The first list comprehension below is a syntax error, while the second one is correct::"
msgstr"برای جلوگیری از ایجاد ابهام در دستور زبان پایتون، اگر ``expression`` یک تاپل ایجاد کند، باید درون پرانتز قرار گیرد. نخستین درک فهرستی زیر یک خطای سینتکسی است، در حالی که دومی صحیح است::"
msgid""
"# Syntax error\n"
"[x, y for x in seq1 for y in seq2]\n"
"# Correct\n"
"[(x, y) for x in seq1 for y in seq2]"
msgstr""
"# Syntax error\n"
"[x, y for x in seq1 for y in seq2]\n"
"# Correct\n"
"[(x, y) for x in seq1 for y in seq2]"
msgid"Generators"
msgstr"تولیدگرها"
msgid"Generators are a special class of functions that simplify the task of writing iterators. Regular functions compute a value and return it, but generators return an iterator that returns a stream of values."
msgstr"تولیدگرها کلاسی خاص از توابع هستند که کار نوشتن پیمایشگرها را ساده میکنند. توابع معمولی یک مقدار را محاسبه میکنند و آن را برمیگردانند، اما تولیدگرها پیمایشگری را برمیگردانند که جریانی از مقادیر را برمیگرداند."
msgid"You're doubtless familiar with how regular function calls work in Python or C. When you call a function, it gets a private namespace where its local variables are created. When the function reaches a ``return`` statement, the local variables are destroyed and the value is returned to the caller. A later call to the same function creates a new private namespace and a fresh set of local variables. But, what if the local variables weren't thrown away on exiting a function? What if you could later resume the function where it left off? This is what generators provide; they can be thought of as resumable functions."
msgstr "شما بدون شک با چگونگی کار فراخوانیهای معمول تابع در پایتون یا C آشنا هستید. هنگامی که تابعی را فراخوانی میکنید، یک فضای نام خصوصی برای آن ایجاد میشود که متغیرهای محلیاش در آن ساخته میشوند. وقتی تابع به یک دستور ``return`` میرسد، متغیرهای محلی از بین میروند و مقدار به فراخواننده بازگردانده میشود. فراخوانی بعدی همان تابع، یک فضای نام خصوصی جدید و مجموعهای تازه از متغیرهای محلی ایجاد میکند. اما چه میشد اگر متغیرهای محلی هنگام خروج از یک تابع دور انداخته نمیشدند؟ چه میشد اگر میتوانستید بعداً تابع را از همانجایی که متوقف شده بود از سر بگیرید؟ این همان چیزی است که تولیدگرها فراهم میکنند؛ میتوان آنها را توابعی قابل از سرگیری دانست."
msgid"Here's the simplest example of a generator function:"
msgstr"در اینجا سادهترین مثال از یک تابع تولیدگر آمده است:"
msgid"Any function containing a :keyword:`yield` keyword is a generator function; this is detected by Python's :term:`bytecode` compiler which compiles the function specially as a result."
msgstr"هر تابعی که شامل کلیدواژهی :keyword:`yield` باشد، یک تابع تولیدگر است؛ این موضوع را کامپایلر :term:`bytecode` پایتون تشخیص میدهد و در نتیجه، تابع را بهصورت ویژه کامپایل میکند."
msgid"When you call a generator function, it doesn't return a single value; instead it returns a generator object that supports the iterator protocol. On executing the ``yield`` expression, the generator outputs the value of ``i``, similar to a ``return`` statement. The big difference between ``yield`` and a ``return`` statement is that on reaching a ``yield`` the generator's state of execution is suspended and local variables are preserved. On the next call to the generator's :meth:`~generator.__next__` method, the function will resume executing."
msgstr"وقتی یک تابع تولیدگر را فراخوانی میکنید، مقدار واحدی را برنمیگرداند؛ بلکه یک شیء تولیدگر برمیگرداند که از پروتکل پیمایشگر پشتیبانی میکند. با اجرای عبارت ``yield``، تولیدگر مقدار ``i`` را خروجی میدهد، مشابه یک دستور ``return``. تفاوت بزرگ میان ``yield`` و یک دستور ``return`` این است که با رسیدن به ``yield``، وضعیت اجرای تولیدگر به حالت تعلیق درمیآید و متغیرهای محلی حفظ میشوند. در فراخوانی بعدی متد :meth:`~generator.__next__` تولیدگر، تابع اجرای خود را از سر میگیرد."
msgid"Here's a sample usage of the ``generate_ints()`` generator:"
msgstr"در اینجا یک نمونه استفاده از تولیدگر ``generate_ints()`` آمده است:"
msgid"You could equally write ``for i in generate_ints(5)``, or ``a, b, c = generate_ints(3)``."
msgstr"همچنین میتوانید ``for i in generate_ints(5)`` یا ``a, b, c = generate_ints(3)`` را بنویسید."
msgid"Inside a generator function, ``return value`` causes ``StopIteration(value)`` to be raised from the :meth:`~generator.__next__` method. Once this happens, or the bottom of the function is reached, the procession of values ends and the generator cannot yield any further values."
msgstr"درون یک تابع تولیدگر، ``return value`` باعث پرتاب ``StopIteration(value)`` از متد :meth:`~generator.__next__` میشود. پس از وقوع این حالت، یا پس از رسیدن به انتهای تابع، دنبالهی مقادیر پایان مییابد و تولیدگر نمیتواند مقادیر بیشتری تولید کند."
msgid"You could achieve the effect of generators manually by writing your own class and storing all the local variables of the generator as instance variables. For example, returning a list of integers could be done by setting ``self.count`` to 0, and having the :meth:`~iterator.__next__` method increment ``self.count`` and return it. However, for a moderately complicated generator, writing a corresponding class can be much messier."
msgstr"میتوانید اثر تولیدگرها را بهصورت دستی با نوشتن کلاس خودتان و ذخیره کردن تمام متغیرهای محلی تولیدگر بهعنوان متغیرهای نمونه به دست آورید. برای مثال، برگرداندن فهرستی از اعداد صحیح میتواند با تنظیم ``self.count`` روی ۰ و افزایش ``self.count`` در متد :meth:`~iterator.__next__` و برگرداندن آن انجام شود. با این حال، برای یک تولیدگر نسبتاً پیچیده، نوشتن کلاس متناظر میتواند بسیار آشفتهتر باشد."
msgid"The test suite included with Python's library, :source:`Lib/test/test_generators.py`, contains a number of more interesting examples. Here's one generator that implements an in-order traversal of a tree using generators recursively. ::"
msgstr"بدنهی آزمون موجود در کتابخانهی پایتون، :source:`Lib/test/test_generators.py`، شامل چندین مثال جالبتر است. در اینجا یک تولیدگر آمده است که پیمایش میانترتیب یک درخت را با استفادهی بازگشتی از تولیدگرها پیادهسازی میکند. ::"
msgid""
"# A recursive generator that generates Tree leaves in in-order.\n"
"def inorder(t):\n"
" if t:\n"
" for x in inorder(t.left):\n"
" yield x\n"
"\n"
" yield t.label\n"
"\n"
" for x in inorder(t.right):\n"
" yield x"
msgstr""
"# A recursive generator that generates Tree leaves in in-order.\n"
"def inorder(t):\n"
" if t:\n"
" for x in inorder(t.left):\n"
" yield x\n"
"\n"
" yield t.label\n"
"\n"
" for x in inorder(t.right):\n"
" yield x"
msgid"Two other examples in ``test_generators.py`` produce solutions for the N-Queens problem (placing N queens on an NxN chess board so that no queen threatens another) and the Knight's Tour (finding a route that takes a knight to every square of an NxN chessboard without visiting any square twice)."
msgstr"دو مثال دیگر در ``test_generators.py`` راهحلهایی برای مسئلهی N وزیر (N-Queens، قرار دادن N وزیر روی یک صفحهی شطرنج NxN بهگونهای که هیچ وزیری وزیر دیگری را تهدید نکند) و گشت اسب (Knight's Tour، یافتن مسیری که یک اسب را به همهی خانههای یک صفحهی شطرنج NxN ببرد، بدون آنکه از هیچ خانهای دو بار بازدید شود) تولید میکنند."
msgid"Passing values into a generator"
msgstr"ارسال مقادیر به یک تولیدگر"
msgid"In Python 2.4 and earlier, generators only produced output. Once a generator's code was invoked to create an iterator, there was no way to pass any new information into the function when its execution is resumed. You could hack together this ability by making the generator look at a global variable or by passing in some mutable object that callers then modify, but these approaches are messy."
msgstr"در پایتون 2.4 و نسخههای پیش از آن، تولیدگرها تنها خروجی تولید میکردند. پس از فراخوانی کد یک تولیدگر برای ایجاد یک پیمایشگر، هیچ راهی برای ارسال اطلاعات جدید به تابع در زمان از سرگیری اجرای آن وجود نداشت. شما میتوانستید این قابلیت را بهصورت دستوپاشکسته با وادار کردن تولیدگر به بررسی یک متغیر سراسری یا با ارسال یک شیء تغییرپذیر که فراخوانکنندگان سپس آن را تغییر میدهند، پیادهسازی کنید، اما این روشها آشفته هستند."
msgid"In Python 2.5 there's a simple way to pass values into a generator. :keyword:`yield` became an expression, returning a value that can be assigned to a variable or otherwise operated on::"
msgstr"در پایتون 2.5 راه سادهای برای ارسال مقادیر به یک تولیدگر وجود دارد. :keyword:`yield` به یک عبارت تبدیل شد و مقداری را برمیگرداند که میتوان آن را به یک متغیر انتساب داد یا عملیات دیگری روی آن انجام داد::"
msgid"val = (yield i)"
msgstr"val = (yield i)"
msgid"I recommend that you **always** put parentheses around a ``yield`` expression when you're doing something with the returned value, as in the above example. The parentheses aren't always necessary, but it's easier to always add them instead of having to remember when they're needed."
msgstr"توصیه میکنم که **همیشه** اطراف عبارت ``yield`` پرانتز قرار دهید، هنگامی که با مقدار برگرداندهشده کاری انجام میدهید، همانگونه که در مثال بالا آمده است. پرانتزها همیشه ضروری نیستند، اما افزودن همیشگی آنها آسانتر است تا اینکه به یاد داشته باشید چه زمانی لازم هستند."
msgid"(:pep:`342` explains the exact rules, which are that a ``yield``-expression must always be parenthesized except when it occurs at the top-level expression on the right-hand side of an assignment. This means you can write ``val = yield i`` but have to use parentheses when there's an operation, as in ``val = (yield i) + 12``.)"
msgstr"(:pep:`342` قوانین دقیق را توضیح میدهد، که بر اساس آنها عبارت ``yield`` باید همیشه داخل پرانتز قرار گیرد، مگر وقتی که بهعنوان عبارت سطح بالا در سمت راست یک انتساب ظاهر شود. این بدان معناست که میتوانید ``val = yield i`` بنویسید، اما وقتی عملیاتی وجود دارد باید از پرانتز استفاده کنید، مانند ``val = (yield i) + 12``.)"
msgid"Values are sent into a generator by calling its :meth:`send(value) <generator.send>` method. This method resumes the generator's code and the ``yield`` expression returns the specified value. If the regular :meth:`~generator.__next__` method is called, the ``yield`` returns ``None``."
msgstr"مقادیر با فراخوانی متد :meth:`send(value) <generator.send>` یک تولیدگر، به آن ارسال میشوند. این متد کد تولیدگر را از سر میگیرد و عبارت ``yield`` مقدار مشخصشده را بازمیگرداند. اگر متد معمولی :meth:`~generator.__next__` فراخوانی شود، ``yield`` مقدار ``None`` را بازمیگرداند."
msgid"Here's a simple counter that increments by 1 and allows changing the value of the internal counter."
msgstr"در اینجا یک شمارنده ساده آمده است که به اندازه ۱ افزایش مییابد و امکان تغییر مقدار شمارنده داخلی را فراهم میکند."
msgid""
"def counter(maximum):\n"
" i = 0\n"
" while i < maximum:\n"
" val = (yield i)\n"
" # If value provided, change counter\n"
" if val is not None:\n"
" i = val\n"
" else:\n"
" i += 1"
msgstr""
"def counter(maximum):\n"
" i = 0\n"
" while i < maximum:\n"
" val = (yield i)\n"
" # If value provided, change counter\n"
" if val is not None:\n"
" i = val\n"
" else:\n"
" i += 1"
msgid"And here's an example of changing the counter:"
msgstr"و در اینجا مثالی از تغییر شمارنده آمده است:"
msgid"Because ``yield`` will often be returning ``None``, you should always check for this case. Don't just use its value in expressions unless you're sure that the :meth:`~generator.send` method will be the only method used to resume your generator function."
msgstr"از آنجا که ``yield`` اغلب مقدار ``None`` را برمیگرداند، باید همیشه این حالت را بررسی کنید. مقدار آن را بهسادگی در عبارتها به کار نبرید، مگر اینکه مطمئن باشید متد :meth:`~generator.send` تنها متدی است که برای از سرگیری تابع تولیدگر شما استفاده میشود."
msgid"In addition to :meth:`~generator.send`, there are two other methods on generators:"
msgstr"علاوه بر :meth:`~generator.send`، تولیدگرها دو متد دیگر نیز دارند:"
msgid":meth:`throw(value) <generator.throw>` is used to raise an exception inside the generator; the exception is raised by the ``yield`` expression where the generator's execution is paused."
msgstr":meth:`throw(value) <generator.throw>` برای پرتاب استثنا درون تولیدگر استفاده میشود؛ این استثنا توسط عبارت ``yield`` در نقطهای که اجرای تولیدگر مکث کرده است پرتاب میشود."
msgid":meth:`~generator.close` sends a :exc:`GeneratorExit` exception to the generator to terminate the iteration. On receiving this exception, the generator's code must either raise :exc:`GeneratorExit` or :exc:`StopIteration`; catching the exception and doing anything else is illegal and will trigger a :exc:`RuntimeError`. :meth:`~generator.close` will also be called by Python's garbage collector when the generator is garbage-collected."
msgstr":meth:`~generator.close` استثنای :exc:`GeneratorExit` را به تولیدگر میفرستد تا تکرار را پایان دهد. هنگام دریافت این استثنا، کد تولیدگر باید یا :exc:`GeneratorExit` یا :exc:`StopIteration` را پرتاب کند؛ گرفتن این استثنا و انجام هر کار دیگری غیرمجاز است و باعث پرتاب :exc:`RuntimeError` میشود. همچنین وقتی تولیدگر زبالهروبی میشود، :meth:`~generator.close` توسط زبالهروب پایتون فراخوانی میشود."
msgid"If you need to run cleanup code when a :exc:`GeneratorExit` occurs, I suggest using a ``try: ... finally:`` suite instead of catching :exc:`GeneratorExit`."
msgstr"اگر نیاز دارید هنگام رخ دادن :exc:`GeneratorExit` کد پاکسازی را اجرا کنید، پیشنهاد میکنم بهجای گرفتن :exc:`GeneratorExit` از یک بلوک ``try: ... finally:`` استفاده کنید."
msgid"The cumulative effect of these changes is to turn generators from one-way producers of information into both producers and consumers."
msgstr"اثر تجمعی این تغییرات این است که تولیدگرها را از تولیدکنندگان یکطرفهی اطلاعات به هر دو تولیدکننده و مصرفکننده تبدیل میکند."
msgid"Generators also become **coroutines**, a more generalized form of subroutines. Subroutines are entered at one point and exited at another point (the top of the function, and a ``return`` statement), but coroutines can be entered, exited, and resumed at many different points (the ``yield`` statements)."
msgstr"تولیدگرها همچنین به **همروالها** تبدیل میشوند، شکل تعمیمیافتهتری از زیربرنامهها. زیربرنامهها در یک نقطه وارد میشوند و از نقطهای دیگر خارج میشوند (ابتدای تابع و یک دستور ``return``)، اما همروالها میتوانند در نقاط مختلفی وارد شوند، خارج شوند و از سر گرفته شوند (دستورهای ``yield``)."
msgid"Built-in functions"
msgstr"توابع توکار"
msgid"Let's look in more detail at built-in functions often used with iterators."
msgstr"بگذارید توابع توکاری را که اغلب با پیمایشگرها استفاده میشوند، با جزئیات بیشتری بررسی کنیم."
msgid"Two of Python's built-in functions, :func:`map` and :func:`filter` duplicate the features of generator expressions:"
msgstr"دو مورد از توابع توکار پایتون، :func:`map` و :func:`filter`، قابلیتهای عبارات تولیدگر را تکرار میکنند:"
msgid":func:`map(f, iterA, iterB, ...) <map>` returns an iterator over the sequence"
msgstr":func:`map(f, iterA, iterB, ...) <map>` یک پیمایشگر بر روی دنباله برمیگرداند"
msgid"You can of course achieve the same effect with a list comprehension."
msgstr"البته میتوانید با استفاده از یک درک فهرستی نیز همان اثر را ایجاد کنید."
msgid":func:`filter(predicate, iter) <filter>` returns an iterator over all the sequence elements that meet a certain condition, and is similarly duplicated by list comprehensions. A **predicate** is a function that returns the truth value of some condition; for use with :func:`filter`, the predicate must take a single value."
msgstr":func:`filter(predicate, iter) <filter>` یک پیمایشگر بر روی تمام عناصر دنبالهای که شرط خاصی را برآورده میکنند، برمیگرداند و بهطور مشابه میتوان آن را با درک فهرستی پیادهسازی کرد. یک **تابع محمول** (predicate) تابعی است که مقدار درستی یک شرط را برمیگرداند؛ برای استفاده با :func:`filter`، تابع محمول باید یک مقدار واحد دریافت کند."
msgid"This can also be written as a list comprehension:"
msgstr"این را میتوان بهصورت یک درک فهرستی نیز نوشت:"
msgid":func:`enumerate(iter, start=0) <enumerate>` counts off the elements in the iterable returning 2-tuples containing the count (from *start*) and each element. ::"
msgstr":func:`enumerate(iter, start=0) <enumerate>` عناصر موجود در پیمایشپذیر را میشمارد و تاپلهای دوتایی (2-tuples) شامل شماره (از *start*) و هر عنصر را برمیگرداند. ::"
msgid""
">>> for item in enumerate(['subject', 'verb', 'object']):\n"
"... print(item)\n"
"(0, 'subject')\n"
"(1, 'verb')\n"
"(2, 'object')"
msgstr""
">>> for item in enumerate(['subject', 'verb', 'object']):\n"
"... print(item)\n"
"(0, 'subject')\n"
"(1, 'verb')\n"
"(2, 'object')"
msgid":func:`enumerate` is often used when looping through a list and recording the indexes at which certain conditions are met::"
msgstr":func:`enumerate` اغلب هنگام پیمایش یک فهرست با حلقه و ثبت اندیسهایی که شرایط خاصی در آنها برقرار است استفاده میشود::"
msgid""
"f = open('data.txt', 'r')\n"
"for i, line in enumerate(f):\n"
" if line.strip() == '':\n"
" print('Blank line at line #%i' % i)"
msgstr""
"f = open('data.txt', 'r')\n"
"for i, line in enumerate(f):\n"
" if line.strip() == '':\n"
" print('Blank line at line #%i' % i)"
msgid":func:`sorted(iterable, key=None, reverse=False) <sorted>` collects all the elements of the iterable into a list, sorts the list, and returns the sorted result. The *key* and *reverse* arguments are passed through to the constructed list's :meth:`~list.sort` method. ::"
msgstr":func:`sorted(iterable, key=None, reverse=False) <sorted>` تمام عناصر یک شیء پیمایشپذیر را در یک فهرست گردآوری میکند، فهرست را مرتب میکند و نتیجهی مرتبشده را برمیگرداند. آرگومانهای *key* و *reverse* به متد :meth:`~list.sort` فهرست ساختهشده منتقل میشوند. ::"
msgid""
">>> import random\n"
">>> # Generate 8 random numbers between [0, 10000)\n"
msgid"(For a more detailed discussion of sorting, see the :ref:`sortinghowto`.)"
msgstr"(برای بحثی با جزئیات بیشتر درباره مرتبسازی، :ref:`sortinghowto` را ببینید.)"
msgid"The :func:`any(iter) <any>` and :func:`all(iter) <all>` built-ins look at the truth values of an iterable's contents. :func:`any` returns ``True`` if any element in the iterable is a true value, and :func:`all` returns ``True`` if all of the elements are true values:"
msgstr"توابع توکار :func:`any(iter) <any>` و :func:`all(iter) <all>` مقادیر درستی محتوای یک پیمایشپذیر را بررسی میکنند. :func:`any` اگر یکی از المانهای پیمایشپذیر مقدار درست باشد، ``True`` را برمیگرداند، و :func:`all` اگر تمام المانها مقادیر درست باشند، ``True`` را برمیگرداند:"
msgid":func:`zip(iterA, iterB, ...) <zip>` takes one element from each iterable and returns them in a tuple::"
msgstr":func:`zip(iterA, iterB, ...) <zip>` یک عنصر از هر پیمایشپذیر میگیرد و آنها را در قالب یک تاپل برمیگرداند::"
msgid""
"zip(['a', 'b', 'c'], (1, 2, 3)) =>\n"
" ('a', 1), ('b', 2), ('c', 3)"
msgstr""
"zip(['a', 'b', 'c'], (1, 2, 3)) =>\n"
" ('a', 1), ('b', 2), ('c', 3)"
msgid"It doesn't construct an in-memory list and :term:`exhaust <exhausted>` all the input iterators before returning; instead tuples are constructed and returned only if they're requested. (The technical term for this behaviour is `lazy evaluation <https://en.wikipedia.org/wiki/Lazy_evaluation>`__.)"
msgstr"این تابع یک فهرست در حافظه نمیسازد و :term:`خالی کردن <exhausted>` تمام پیمایشگرهای ورودی را قبل از بازگشت انجام نمیدهد؛ در عوض، تاپلها تنها در صورت درخواست ساخته و بازگردانده میشوند. (اصطلاح فنی برای این رفتار `lazy evaluation <https://en.wikipedia.org/wiki/Lazy_evaluation>`__ است.)"
msgid"This iterator is intended to be used with iterables that are all of the same length. If the iterables are of different lengths, the resulting stream will be the same length as the shortest iterable. ::"
msgstr"این پیمایشگر برای استفاده با پیمایشپذیرهایی در نظر گرفته شده است که همگی طول یکسانی دارند. اگر پیمایشپذیرها طولهای متفاوتی داشته باشند، جریان حاصل همطول با کوتاهترین پیمایشپذیر خواهد بود. ::"
msgid""
"zip(['a', 'b'], (1, 2, 3)) =>\n"
" ('a', 1), ('b', 2)"
msgstr""
"zip(['a', 'b'], (1, 2, 3)) =>\n"
" ('a', 1), ('b', 2)"
msgid"You should avoid doing this, though, because an element may be taken from the longer iterators and discarded. This means you can't go on to use the iterators further because you risk skipping a discarded element."
msgstr"با این حال، باید از انجام این کار خودداری کنید، زیرا ممکن است عنصری از پیمایشگرهای طولانیتر گرفته و دور انداخته شود. این بدان معناست که نمیتوانید به استفاده بیشتر از پیمایشگرها ادامه دهید، زیرا ممکن است یک عنصر دور انداختهشده را از قلم بیندازید."
msgid"The itertools module"
msgstr"ماژول itertools"
msgid"The :mod:`itertools` module contains a number of commonly used iterators as well as functions for combining several iterators. This section will introduce the module's contents by showing small examples."
msgstr"ماژول :mod:`itertools` شامل تعدادی پیمایشگر پرکاربرد و همچنین توابعی برای ترکیب چندین پیمایشگر است. این بخش با نمایش مثالهای کوچک، محتوای این ماژول را معرفی میکند."
msgid"The module's functions fall into a few broad classes:"
msgstr"توابع این ماژول به چند کلاس کلی تقسیم میشوند:"
msgid"Functions that create a new iterator based on an existing iterator."
msgstr"توابعی که یک پیمایشگر جدید را بر اساس یک پیمایشگر موجود ایجاد میکنند."
msgid"Functions for treating an iterator's elements as function arguments."
msgstr"توابعی برای در نظر گرفتن عناصر یک پیمایشگر بهعنوان آرگومانهای تابع."
msgid"Functions for selecting portions of an iterator's output."
msgstr"توابعی برای انتخاب بخشهایی از خروجی یک پیمایشگر ."
msgid"A function for grouping an iterator's output."
msgstr"تابعی برای گروهبندی خروجی یک پیمایشگر."
msgid"Creating new iterators"
msgstr"ایجاد پیمایشگرهای جدید"
msgid":func:`itertools.count(start, step) <itertools.count>` returns an infinite stream of evenly spaced values. You can optionally supply the starting number, which defaults to 0, and the interval between numbers, which defaults to 1::"
msgstr":func:`itertools.count(start, step) <itertools.count>` یک جریان بیپایان از مقادیر با فاصلههای مساوی بازمیگرداند. بهصورت اختیاری میتوانید عدد شروع را که پیشفرض آن ۰ است و فاصله بین اعداد را که پیشفرض آن ۱ است، مشخص کنید::"
msgid""
"itertools.count() =>\n"
" 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\n"
"itertools.count(10) =>\n"
" 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...\n"
"itertools.count(10, 5) =>\n"
" 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, ..."
msgstr""
"itertools.count() =>\n"
" 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\n"
"itertools.count(10) =>\n"
" 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...\n"
"itertools.count(10, 5) =>\n"
" 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, ..."
msgid":func:`itertools.cycle(iter) <itertools.cycle>` saves a copy of the contents of a provided iterable and returns a new iterator that returns its elements from first to last. The new iterator will repeat these elements infinitely. ::"
msgstr":func:`itertools.cycle(iter) <itertools.cycle>` یک کپی از محتوای پیمایشپذیر ارائهشده ذخیره میکند و پیمایشگر جدیدی بازمیگرداند که عناصر آن را از اولین تا آخرین بازمیگرداند. این پیمایشگر جدید، این عناصر را بهطور بینهایت تکرار خواهد کرد. ::"
msgid""
"itertools.cycle([1, 2, 3, 4, 5]) =>\n"
" 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ..."
msgstr""
"itertools.cycle([1, 2, 3, 4, 5]) =>\n"
" 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ..."
msgid":func:`itertools.repeat(elem, [n]) <itertools.repeat>` returns the provided element *n* times, or returns the element endlessly if *n* is not provided. ::"
msgstr":func:`itertools.repeat(elem, [n]) <itertools.repeat>` المان ارائهشده را *n* بار برمیگرداند، یا اگر *n* ارائه نشده باشد، المان را بیپایان برمیگرداند. ::"
msgid":func:`itertools.chain(iterA, iterB, ...) <itertools.chain>` takes an arbitrary number of iterables as input, and returns all the elements of the first iterator, then all the elements of the second, and so on, until all of the iterables have been :term:`exhausted`. ::"
msgstr":func:`itertools.chain(iterA, iterB, ...) <itertools.chain>` تعداد دلخواهی از پیمایشپذیرها را به عنوان ورودی میپذیرد، و تمام المانهای اولین پیمایشگر را، سپس تمام المانهای دومی، و به همین ترتیب، تا زمانی که تمام پیمایشپذیرها :term:`خالی شده <exhausted>` شده باشند، برمیگرداند. ::"
msgid":func:`itertools.islice(iter, [start], stop, [step]) <itertools.islice>` returns a stream that's a slice of the iterator. With a single *stop* argument, it will return the first *stop* elements. If you supply a starting index, you'll get *stop-start* elements, and if you supply a value for *step*, elements will be skipped accordingly. Unlike Python's string and list slicing, you can't use negative values for *start*, *stop*, or *step*. ::"
msgstr":func:`itertools.islice(iter, [start], stop, [step]) <itertools.islice>` جریانی را برمیگرداند که اسلایسی از پیمایشگر است. با تنها یک آرگومان *stop*، نخستین *stop* عنصر را برمیگرداند. اگر یک اندیس آغاز ارائه دهید، *stop-start* عنصر دریافت خواهید کرد، و اگر مقداری برای *step* ارائه دهید، مطابق آن از روی عناصر پرش خواهد شد. برخلاف اسلایس رشته و فهرست در پایتون، نمیتوانید از مقادیر منفی برای *start*، *stop* یا *step* استفاده کنید. ::"
msgid""
"itertools.islice(range(10), 8) =>\n"
" 0, 1, 2, 3, 4, 5, 6, 7\n"
"itertools.islice(range(10), 2, 8) =>\n"
" 2, 3, 4, 5, 6, 7\n"
"itertools.islice(range(10), 2, 8, 2) =>\n"
" 2, 4, 6"
msgstr""
"itertools.islice(range(10), 8) =>\n"
" 0, 1, 2, 3, 4, 5, 6, 7\n"
"itertools.islice(range(10), 2, 8) =>\n"
" 2, 3, 4, 5, 6, 7\n"
"itertools.islice(range(10), 2, 8, 2) =>\n"
" 2, 4, 6"
msgid":func:`itertools.tee(iter, [n]) <itertools.tee>` replicates an iterator; it returns *n* independent iterators that will all return the contents of the source iterator. If you don't supply a value for *n*, the default is 2. Replicating iterators requires saving some of the contents of the source iterator, so this can consume significant memory if the iterator is large and one of the new iterators is consumed more than the others. ::"
msgstr":func:`itertools.tee(iter, [n]) <itertools.tee>` یک پیمایشگر را تکثیر میکند؛ این تابع *n* پیمایشگر مستقل برمیگرداند که همگی محتوای پیمایشگر منبع را برمیگردانند. اگر مقداری برای *n* ارائه ندهید، مقدار پیشفرض ۲ است. تکثیر پیمایشگرها نیازمند ذخیره کردن بخشی از محتوای پیمایشگر منبع است، بنابراین اگر پیمایشگر بزرگ باشد و یکی از پیمایشگرهای جدید بیشتر از بقیه مصرف شود، میتواند حافظه قابلتوجهی مصرف کند. ::"
msgid""
"itertools.tee( itertools.count() ) =>\n"
" iterA, iterB\n"
"\n"
"where iterA ->\n"
" 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\n"
"\n"
"and iterB ->\n"
" 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ..."
msgstr""
"itertools.tee( itertools.count() ) =>\n"
" iterA, iterB\n"
"\n"
"که در آن iterA ->\n"
" 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...\n"
"\n"
"و iterB ->\n"
" 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ..."
msgid"Calling functions on elements"
msgstr"فراخوانی توابع بر روی المانها"
msgid"The :mod:`operator` module contains a set of functions corresponding to Python's operators. Some examples are :func:`operator.add(a, b) <operator.add>` (adds two values), :func:`operator.ne(a, b) <operator.ne>` (same as ``a != b``), and :func:`operator.attrgetter('id') <operator.attrgetter>` (returns a callable that fetches the ``.id`` attribute)."
msgstr"ماژول :mod:`operator` شامل مجموعهای از توابع متناظر با عملگرهای پایتون است. برخی نمونهها عبارتاند از :func:`operator.add(a, b) <operator.add>` (دو مقدار را جمع میکند)، :func:`operator.ne(a, b) <operator.ne>` (معادل ``a != b`` است)، و :func:`operator.attrgetter('id') <operator.attrgetter>` (یک فراخوانیپذیر برمیگرداند که ویژگی ``.id`` را دریافت میکند)."
msgid":func:`itertools.starmap(func, iter) <itertools.starmap>` assumes that the iterable will return a stream of tuples, and calls *func* using these tuples as the arguments::"
msgstr":func:`itertools.starmap(func, iter) <itertools.starmap>` فرض میکند که پیمایشپذیر جریانی از تاپلها را برمیگرداند، و *func* را با استفاده از این تاپلها بهعنوان آرگومانها فراخوانی میکند::"
msgid"Another group of functions chooses a subset of an iterator's elements based on a predicate."
msgstr"گروه دیگری از توابع، زیرمجموعهای از عناصر یک پیمایشگر را بر اساس یک محک (predicate) انتخاب میکنند."
msgid":func:`itertools.filterfalse(predicate, iter) <itertools.filterfalse>` is the opposite of :func:`filter`, returning all elements for which the predicate returns false::"
msgstr":func:`itertools.filterfalse(predicate, iter) <itertools.filterfalse>` نقطه مقابل :func:`filter` است و همه عناصری را برمیگرداند که محمول برای آنها false برمیگرداند::"
msgid":func:`itertools.takewhile(predicate, iter) <itertools.takewhile>` returns elements for as long as the predicate returns true. Once the predicate returns false, the iterator will signal the end of its results. ::"
msgstr":func:`itertools.takewhile(predicate, iter) <itertools.takewhile>` عناصر را تا زمانی که شرط مقدار درست را برگرداند، برمیگرداند. هنگامی که محمول مقدار نادرست را برگرداند، پیمایشگر پایان نتایج خود را اعلام میکند. ::"
msgid":func:`itertools.dropwhile(predicate, iter) <itertools.dropwhile>` discards elements while the predicate returns true, and then returns the rest of the iterable's results. ::"
msgstr":func:`itertools.dropwhile(predicate, iter) <itertools.dropwhile>` عناصر را تا زمانی که محمول مقدار درست را برگرداند نادیده میگیرد، و سپس باقیماندهی نتایج تکرارپذیر را برمیگرداند. ::"
msgid":func:`itertools.compress(data, selectors) <itertools.compress>` takes two iterators and returns only those elements of *data* for which the corresponding element of *selectors* is true, stopping whenever either one is :term:`exhausted`::"
msgstr":func:`itertools.compress(data, selectors) <itertools.compress>` دو پیمایشگر را میپذیرد و تنها آن المانهای *data* را برمیگرداند که المان متناظر در *selectors* صحیح باشد، و متوقف میشود هر وقت که یکی از آنها :term:`خالی شده <exhausted>` شود::"
msgid"The :func:`itertools.combinations(iterable, r) <itertools.combinations>` returns an iterator giving all possible *r*-tuple combinations of the elements contained in *iterable*. ::"
msgstr":func:`itertools.combinations(iterable, r) <itertools.combinations>` یک پیمایشگر برمیگرداند که همهی ترکیبهای ممکن بهصورت تاپلهای *r* عضوی از عناصر موجود در *iterable* را میدهد. ::"
msgid"The elements within each tuple remain in the same order as *iterable* returned them. For example, the number 1 is always before 2, 3, 4, or 5 in the examples above. A similar function, :func:`itertools.permutations(iterable, r=None) <itertools.permutations>`, removes this constraint on the order, returning all possible arrangements of length *r*::"
msgstr"عناصر هر تاپل به همان ترتیبی که *iterable* آنها را برگردانده است باقی میمانند. برای مثال، عدد ۱ همیشه قبل از ۲، ۳، ۴ یا ۵ در مثالهای بالا قرار دارد. تابعی مشابه، :func:`itertools.permutations(iterable, r=None) <itertools.permutations>`، این محدودیت روی ترتیب را برمیدارد و همهی آرایشهای ممکن به طول *r* را برمیگرداند::"
msgid"The identical tuple ``('a', 'a', 'b')`` occurs twice, but the two 'a' strings came from different positions."
msgstr"تاپل یکسان ``('a', 'a', 'b')`` دو بار ظاهر میشود، اما دو رشتهی 'a' از موقعیتهای متفاوتی آمدهاند."
msgid"The :func:`itertools.combinations_with_replacement(iterable, r) <itertools.combinations_with_replacement>` function relaxes a different constraint: elements can be repeated within a single tuple. Conceptually an element is selected for the first position of each tuple and then is replaced before the second element is selected. ::"
msgstr"تابع :func:`itertools.combinations_with_replacement(iterable, r) <itertools.combinations_with_replacement>` محدودیت دیگری را برمیدارد: عناصر میتوانند در یک تاپل تکرار شوند. از نظر مفهومی، یک عنصر برای جایگاه نخست هر تاپل انتخاب میشود و سپس پیش از انتخاب عنصر دوم، بازگردانده میشود. ::"
msgid"The last function I'll discuss, :func:`itertools.groupby(iter, key_func=None) <itertools.groupby>`, is the most complicated. ``key_func(elem)`` is a function that can compute a key value for each element returned by the iterable. If you don't supply a key function, the key is simply each element itself."
msgstr"آخرین تابعی که بررسی خواهم کرد، :func:`itertools.groupby(iter, key_func=None) <itertools.groupby>`، پیچیدهترین است. ``key_func(elem)`` تابعی است که میتواند یک مقدار کلید برای هر المان بازگرداندهشده از پیمایشپذیر محاسبه کند. اگر تابع کلید را ارائه نکنید، کلید صرفاً خود هر المان است."
msgid":func:`~itertools.groupby` collects all the consecutive elements from the underlying iterable that have the same key value, and returns a stream of 2-tuples containing a key value and an iterator for the elements with that key."
msgstr":func:`~itertools.groupby` تمام عناصر متوالیِ پیمایشپذیر زیرین را که مقدار کلید یکسانی دارند، جمعآوری میکند و جریانی از تاپلهای دوتایی شامل یک مقدار کلید و یک پیمایشگر برای عناصر با آن کلید را برمیگرداند."
msgid":func:`~itertools.groupby` assumes that the underlying iterable's contents will already be sorted based on the key. Note that the returned iterators also use the underlying iterable, so you have to consume the results of iterator-1 before requesting iterator-2 and its corresponding key."
msgstr":func:`~itertools.groupby` فرض میکند که محتوای پیمایشپذیر زیربنایی از قبل بر اساس کلید مرتب شده باشد. توجه داشته باشید که پیمایشگرهای برگرداندهشده نیز از پیمایشپذیر زیربنایی استفاده میکنند، بنابراین باید نتایج پیمایشگر ۱ را پیش از درخواست پیمایشگر ۲ و کلید متناظر آن مصرف کنید."
msgid"The functools module"
msgstr"ماژول functools"
msgid"The :mod:`functools` module contains some higher-order functions. A **higher-order function** takes one or more functions as input and returns a new function. The most useful tool in this module is the :func:`functools.partial` function."
msgstr"ماژول :mod:`functools` شامل برخی توابع مرتبه بالاتر است. یک **تابع مرتبه بالاتر** یک یا چند تابع را بهعنوان ورودی میگیرد و یک تابع جدید را برمیگرداند. مفیدترین ابزار در این ماژول، تابع :func:`functools.partial` است."
msgid"For programs written in a functional style, you'll sometimes want to construct variants of existing functions that have some of the parameters filled in. Consider a Python function ``f(a, b, c)``; you may wish to create a new function ``g(b, c)`` that's equivalent to ``f(1, b, c)``; you're filling in a value for one of ``f()``'s parameters. This is called \"partial function application\"."
msgstr"برای برنامههایی که به سبک تابعی نوشته شدهاند، گاهی ممکن است بخواهید نسخههایی از توابع موجود بسازید که برخی از پارامترهای آنها از قبل پر شده باشند. یک تابع پایتون ``f(a, b, c)`` را در نظر بگیرید؛ ممکن است بخواهید یک تابع جدید ``g(b, c)`` بسازید که معادل ``f(1, b, c)`` باشد؛ شما مقداری را برای یکی از پارامترهای ``f()`` جایگذاری میکنید. به این کار «کاربرد جزئی تابع» (partial function application) گفته میشود."
msgid"The constructor for :func:`~functools.partial` takes the arguments ``(function, arg1, arg2, ..., kwarg1=value1, kwarg2=value2)``. The resulting object is callable, so you can just call it to invoke ``function`` with the filled-in arguments."
msgstr"سازندهی :func:`~functools.partial` آرگومانهای ``(function, arg1, arg2, ..., kwarg1=value1, kwarg2=value2)`` را دریافت میکند. شیء حاصل فراخوانیپذیر است، بنابراین کافی است آن را فراخوانی کنید تا ``function`` با آرگومانهای مقداردهیشده فراخوانی شود."
msgid"Here's a small but realistic example::"
msgstr"در اینجا مثالی کوچک اما واقعبینانه آمده است::"
msgid""
"import functools\n"
"\n"
"def log(message, subsystem):\n"
" \"\"\"Write the contents of 'message' to the specified subsystem.\"\"\"\n"