{"id":2054,"date":"2025-04-01T01:29:05","date_gmt":"2025-03-31T21:59:05","guid":{"rendered":"https:\/\/mshaeri.com\/blog\/?p=2054"},"modified":"2025-07-14T09:49:00","modified_gmt":"2025-07-14T06:19:00","slug":"pythons-protocol-vs-golangs-interface-are-they-siblings","status":"publish","type":"post","link":"https:\/\/mshaeri.com\/blog\/pythons-protocol-vs-golangs-interface-are-they-siblings\/","title":{"rendered":"Python&#8217;s Protocol vs. Golang&#8217;s interface: Are They Siblings?"},"content":{"rendered":"\n<p>Python introduced <code>Protocol<\/code> in <strong>PEP 544<\/strong> (Python Enhancement Proposal) and officially added it in <strong>Python 3.8<\/strong>. If you&#8217;re coming from a <strong>Golang<\/strong> background and diving into Python, you might stumble upon Python&#8217;s <code>Protocol<\/code> and think, <em>&#8220;Wait a minute&#8230; isn&#8217;t this just Golang&#8217;s <code>interface{}<\/code>?&#8221;<\/em> And honestly? You&#8217;re not entirely wrong! But let&#8217;s break it down and see if they&#8217;re really long-lost brothers or just distant cousins.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is <code>Protocol<\/code> in Python?<\/strong><\/h2>\n\n\n\n<p>The idea is simple: give Python a way to define interfaces without forcing inheritance.<\/p>\n\n\n\n<p>Here&#8217;s a simple example of how it works:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">from typing import Protocol\n\nclass Flyer(Protocol):\n    def fly(self) -&gt; None:\n        ...\n\nclass Bird:\n    def fly(self) -&gt; None:\n        print(\"Flapping wings!\")\n\nclass Airplane:\n    def fly(self) -&gt; None:\n        print(\"Engaging turbines!\")\n\ndef take_off(entity: Flyer):\n    entity.fly()\n\ntake_off(Bird())  # Flapping wings!\ntake_off(Airplane())  # Engaging turbines!\n<\/code><\/pre>\n\n\n\n<p><strong>Key takeaway:<\/strong> Any class with a <code>fly<\/code> method can be treated as a <code>Flyer<\/code>, even if it doesn\u2019t explicitly say, <em>\u201cHey, I implement Flyer!\u201d<\/em> That\u2019s <strong>duck typing<\/strong> at its finest!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Does This Compare to Golang&#8217;s <code>interface{}<\/code>?<\/strong><\/h2>\n\n\n\n<p>If you&#8217;ve ever coded in Go before, you know that <strong>interfaces define behavior, not structure<\/strong>. Just like Python&#8217;s <code>Protocol<\/code>, Go&#8217;s interfaces allow a type to be considered valid if it implements the expected methods.<\/p>\n\n\n\n<p>Here\u2019s a Go version of the same example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">type Flyer interface {\n    Fly()\n}\n\ntype Bird struct{}\n\nfunc (b Bird) Fly() {\n    fmt.Println(\"Flapping wings!\")\n}\n\ntype Airplane struct{}\n\nfunc (a Airplane) Fly() {\n    fmt.Println(\"Engaging turbines!\")\n}\n\nfunc TakeOff(f Flyer) {\n    f.Fly()\n}\n\nTakeOff(Bird{})      \/\/ Flapping wings!\nTakeOff(Airplane{})  \/\/ Engaging turbines!\n<\/code><\/pre>\n\n\n\n<p>Looks super similar, right? In both cases:<\/p>\n\n\n\n<ul>\n<li>There&#8217;s no need for explicit inheritance or <code>implements<\/code> keywords.<\/li>\n\n\n\n<li>If an object has the right methods, it qualifies as the interface\/protocol.<\/li>\n\n\n\n<li>This promotes <strong>duck typing<\/strong>: &#8220;If it quacks like a duck, it\u2019s a duck.&#8221;<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Who Inspired Whom?<\/strong> \ud83e\udd14<\/h2>\n\n\n\n<p>At first glance, it might seem like Python copied Golang. After all, Go has had interfaces <strong>since day one<\/strong> (back in 2009), whereas Python only got <code>Protocol<\/code> in <strong>2019<\/strong>. But hold on! <strong>Duck typing has been a core concept in Python since forever.<\/strong> What <code>Protocol<\/code> really did was give duck typing a more structured, type-safe way of working with tools like <code>mypy<\/code>. So, while Go\u2019s interface <strong>might<\/strong> have influenced Python\u2019s <code>Protocol<\/code>, Python\u2019s dynamic nature already had the foundation for this idea.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>My Take?<\/strong><\/h3>\n\n\n\n<p>Neither is copying the other. Instead, I think they both solve the same problem in a way that makes sense for their language:<\/p>\n\n\n\n<ul>\n<li><strong>Go\u2019s <code>interface{}<\/code><\/strong> is a fundamental part of the language, making it possible to write generic, loosely coupled code in a statically typed world.<\/li>\n\n\n\n<li><strong>Python\u2019s <code>Protocol<\/code><\/strong> is more of an <strong>optional tool<\/strong> for better static type checking while keeping the language\u2019s flexible, dynamic nature.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>When Should\/Shouldn&#8217;t We Use <code>Protocol<\/code> in Python?<\/strong><\/h2>\n\n\n\n<p>Alright, so when does <code>Protocol<\/code> actually make sense in Python?<\/p>\n\n\n\n<p>\u2705 <strong>When you want static type safety<\/strong> \u2013 If you&#8217;re using <code>mypy<\/code>, <code>Protocol<\/code> helps ensure that objects conform to the expected behavior before runtime.<\/p>\n\n\n\n<p>\u2705 <strong>When you\u2019re writing libraries<\/strong> \u2013 If you&#8217;re designing an API that others will use, <code>Protocol<\/code> lets you define expected behaviors without forcing users to subclass.<\/p>\n\n\n\n<p>\u2705 <strong>When working with multiple implementations<\/strong> \u2013 Just like in the Flyer example, if you have multiple objects with the same behavior but different implementations, <code>Protocol<\/code> keeps things clean.<\/p>\n\n\n\n<p>\u274c <strong>When working with simple, dynamic Python code<\/strong> \u2013 If your project doesn&#8217;t need static type checks, regular duck typing (just using <code>.fly()<\/code>) is more Pythonic and readable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>So, are Python&#8217;s <code>Protocol<\/code> and Golang&#8217;s <code>interface{}<\/code> sibling? They&#8217;re definitely <strong>from the same family of ideas<\/strong>. They both encourage <strong>behavior-based programming<\/strong>, for making code more flexible and modular.<\/p>\n\n\n\n<p>But while Go was designed with interfaces as a core feature from the start, Python\u2019s <code>Protocol<\/code> is more of a <strong>nice-to-have<\/strong> for those who need type safety while still enjoying Python\u2019s dynamic nature.<\/p>\n\n\n\n<p>What do you think? Have you used both <code>Protocol<\/code> and Go\u2019s <code>interface{}<\/code>? Let\u2019s discuss in the comments! <\/p>\n\n\n\n<p>Refrences: <\/p>\n\n\n\n<ol>\n<li>Fluent Python 2nd edition, Chapter 13: Interfaces, Protocols, and ABCs<\/li>\n\n\n\n<li>The Go Programming Language, Chapter 7: Interfaces<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Python introduced Protocol in PEP 544 (Python Enhancement Proposal) and officially added it in Python 3.8. If you&#8217;re coming from a Golang background and diving &hellip; <\/p>\n","protected":false},"author":1,"featured_media":2062,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,139,35,41],"tags":[288,127,18,289,287,39],"_links":{"self":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/2054"}],"collection":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/comments?post=2054"}],"version-history":[{"count":5,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/2054\/revisions"}],"predecessor-version":[{"id":2075,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/2054\/revisions\/2075"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/media\/2062"}],"wp:attachment":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/media?parent=2054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/categories?post=2054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/tags?post=2054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}