	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=MIC-06-4+Protocols+in+Python&amp;rft.aulast=Yee&amp;rft.aufirst=Raymond&amp;rft.subject=Uncategorized&amp;rft.source=Hypotyposis+on+a+Good+Day&amp;rft.date=2023-10-26&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=https://hypotyposis.net/blog/2023/10/26/mic-06-4-protocols-in-python/&amp;rft.language=English"></span>
	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=MIC-06-4+Protocols+in+Python&amp;rft.aulast=Yee&amp;rft.aufirst=Raymond&amp;rft.subject=Uncategorized&amp;rft.source=Hypotyposis+on+a+Good+Day&amp;rft.date=2023-10-26&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=https://hypotyposis.net/blog/2023/10/26/mic-06-4-protocols-in-python/&amp;rft.language=English"></span>
{"id":1603,"date":"2023-10-26T21:55:26","date_gmt":"2023-10-27T04:55:26","guid":{"rendered":"https:\/\/hypotyposis.net\/blog\/?p=1603"},"modified":"2023-10-26T21:55:26","modified_gmt":"2023-10-27T04:55:26","slug":"mic-06-4-protocols-in-python","status":"publish","type":"post","link":"https:\/\/hypotyposis.net\/blog\/2023\/10\/26\/mic-06-4-protocols-in-python\/","title":{"rendered":"MIC-06-4 Protocols in Python"},"content":{"rendered":"<p>In response to my prompt<\/p>\n<blockquote>\n<p>There are various &quot;protocols&quot; in Python -- interfaces involving the implementation of certain methods to fulfill a protocol. For example the iterator protocol is implemented via __iter<strong> and __next<\/strong>. Confirm what I just wrote and also tell me about other protocols. I understand there is a sequence protocol and a container protocol. True? What other ones?<\/p>\n<\/blockquote>\n<p>I got the <a href=\"https:\/\/www.perplexity.ai\/search\/1a88b02c-c8ce-4619-99e3-4a0ba22bd4ac?s=u\">answer from perplexity.ai<\/a>:<\/p>\n<blockquote>\n<p>Yes, you\\'re correct about the iterator protocol in Python. It is<br \/>\nimplemented via the\u00a0<code>__iter__<\/code>\u00a0and\u00a0<code>__next__<\/code>\u00a0methods. An object is considered an iterator in Python if it implements these two<br \/>\nmethods<a href=\"https:\/\/www.w3schools.com\/python\/python_iterators.asp\"><\/a><\/p>\n<p>In addition to the iterator protocol, Python also has other protocols<br \/>\nsuch as the sequence protocol and the container protocol.The sequence protocol is implemented by defining methods<br \/>\nlike\u00a0<code>__getitem__<\/code>\u00a0and\u00a0<code>__len__<\/code>. The\u00a0<code>__getitem__<\/code>\u00a0method is used to get an element at a specific index, and the\u00a0<code>__len__<\/code>\u00a0method is used to get the number of elements in the sequence. Other methods that can be part of the sequence protocol<br \/>\ninclude\u00a0<code>__contains__<\/code>,\u00a0<code>__iter__<\/code>,\u00a0<code>__reversed__<\/code>, and<br \/>\nmore<a href=\"https:\/\/docs.python.org\/3\/c-api\/sequence.html\"><\/a><\/p>\n<p>The container protocol is less clearly defined in the Python<br \/>\ndocumentation, but it generally refers to methods that allow an object to store other objects and provide ways to access and manipulate them. This can include methods like\u00a0<code>__contains__<\/code>,\u00a0<code>__iter__<\/code>, and\u00a0<code>__len__<\/code>, similar to the sequence protocol<a href=\"https:\/\/docs.python.org\/3\/library\/collections.html\"><\/a><\/p>\n<p>There are also other protocols in Python. For example, the async<br \/>\niterator protocol, which is similar to the iterator protocol but used<br \/>\nfor asynchronous iteration. It is implemented via the\u00a0<code>__aiter__<\/code>\u00a0and\u00a0<code>__anext__<\/code>\u00a0methods<a href=\"https:\/\/docs.python.org\/3\/c-api\/iter.html\"><\/a><\/p>\n<p>Another example is the protocol for numerical types, which includes<br \/>\nmethods<br \/>\nlike\u00a0<code>__add__<\/code>,\u00a0<code>__sub__<\/code>,\u00a0<code>__mul__<\/code>,\u00a0<code>__truediv__<\/code>,\u00a0<code>__floordiv__<\/code>,\u00a0<code>__mod__<\/code>,\u00a0<code>__divmod__<\/code>,\u00a0<code>__pow__<\/code>,\u00a0<code>__neg__<\/code>,\u00a0<code>__pos__<\/code>,\u00a0<code>__abs__<\/code>,\u00a0<code>__invert__<\/code>,\u00a0<code>__complex__<\/code>,\u00a0<code>__int__<\/code>,\u00a0<code>__float__<\/code>,\u00a0<code>__round__<\/code>,\u00a0<code>__trunc__<\/code>,\u00a0<code>__floor__<\/code>,\u00a0<code>__ceil__<\/code>, and more. These methods allow an object to behave like a numerical value and interact with other numerical values in a meaningful way.There are many more protocols in Python, and they are a powerful tool for creating objects that interact with built-in Python constructs in a natural way. However, there doesn\\'t seem to be a comprehensive list of all protocols in the Python documentation<a href=\"https:\/\/stackoverflow.com\/questions\/6087731\/comprehensive-list-of-python-protocols-interfaces\"><\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>In response to my prompt There are various &quot;protocols&quot; in Python &#8212; interfaces involving the implementation of certain methods to fulfill a protocol. For example the iterator protocol is implemented via __iter and __next. Confirm what I just wrote and &hellip; <a href=\"https:\/\/hypotyposis.net\/blog\/2023\/10\/26\/mic-06-4-protocols-in-python\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1603","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7I6qs-pR","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts\/1603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/comments?post=1603"}],"version-history":[{"count":1,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts\/1603\/revisions"}],"predecessor-version":[{"id":1604,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts\/1603\/revisions\/1604"}],"wp:attachment":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/media?parent=1603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/categories?post=1603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/tags?post=1603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}