<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Tristan Brugère</title><link>https://tristan.bruge.re/</link><description>Tristan Brugère’s personal website</description><atom:link href="https://tristan.bruge.re/fr/rss.xml" rel="self" type="application/rss+xml"></atom:link><language>fr</language><copyright>Contents © 2026 &lt;a href="mailto:tbrugere(at)ucsd.edu"&gt;Tristan Brugère&lt;/a&gt; </copyright><lastBuildDate>Fri, 20 Nov 2026 15:16:03 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Hacking your way through pattern matching on sets</title><link>https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/</link><dc:creator>Tristan Brugère</dc:creator><description>&lt;p&gt;A while ago, while writing some code to automate my machine learning pipelines,
I ended up writing a function with a definition that looks a bit like this&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_5ffb9699ee384145abc1becf80b3cd30-1" name="rest_code_5ffb9699ee384145abc1becf80b3cd30-1" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_5ffb9699ee384145abc1becf80b3cd30-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;check_arguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provided_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;required_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
&lt;a id="rest_code_5ffb9699ee384145abc1becf80b3cd30-2" name="rest_code_5ffb9699ee384145abc1becf80b3cd30-2" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_5ffb9699ee384145abc1becf80b3cd30-2"&gt;&lt;/a&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;"""Checks that the provided set of arguments matches the set of required arguments"""&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The pipeline uses this function to check that the hyperparameters it is reading from the
config file (or database, etc.) are correct for the model it is loading. But that's beside the point.&lt;/p&gt;
&lt;p&gt;This is a pretty common piece of boilerplate one might find anywhere.
It simply needs to check that the keys of provided_arguments and the members required arguments contain the same values.
Not a very complicated problem, and like all problems in CS,
it is trivialised by using the right data structure: &lt;em&gt;the unordered set&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="https://xkcd.com/353/"&gt;Python being Python&lt;/a&gt;, it is right there in the language's basic data structures&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_f975320429584943b5b5db10022771eb-1" name="rest_code_f975320429584943b5b5db10022771eb-1" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;check_arguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provided_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;required_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-2" name="rest_code_f975320429584943b5b5db10022771eb-2" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-2"&gt;&lt;/a&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;"""Checks that the provided set of arguments matches the set of require arguments"""&lt;/span&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-3" name="rest_code_f975320429584943b5b5db10022771eb-3" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-3"&gt;&lt;/a&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-4" name="rest_code_f975320429584943b5b5db10022771eb-4" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-4"&gt;&lt;/a&gt;    &lt;span class="n"&gt;provided_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provided_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-5" name="rest_code_f975320429584943b5b5db10022771eb-5" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-5"&gt;&lt;/a&gt;    &lt;span class="n"&gt;required_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;required_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-6" name="rest_code_f975320429584943b5b5db10022771eb-6" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-6"&gt;&lt;/a&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-7" name="rest_code_f975320429584943b5b5db10022771eb-7" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-7"&gt;&lt;/a&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;provided_set&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;required_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-8" name="rest_code_f975320429584943b5b5db10022771eb-8" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-8"&gt;&lt;/a&gt;    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"arguments required are &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;required_set&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;,"&lt;/span&gt;
&lt;a id="rest_code_f975320429584943b5b5db10022771eb-9" name="rest_code_f975320429584943b5b5db10022771eb-9" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_f975320429584943b5b5db10022771eb-9"&gt;&lt;/a&gt;    &lt;span class="s2"&gt;"but arguments provided are &lt;/span&gt;&lt;span class="si"&gt;{provided_set}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is already pretty nice and simple, but the error is not very readable.
We would like to show the user which arguments were required, but not provided,
and which ones were provided, but not required.&lt;/p&gt;
&lt;p&gt;Being a functional programmer at heart, I immediately remembered that a few years ago, Python had added pattern matching syntax into the language. So I whipped up a quick solution that ressemled what I would have written in OCaml.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-1" name="rest_code_53d777eb752a4648ab59075eb09923c0-1" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;check_arguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provided_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;required_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-2" name="rest_code_53d777eb752a4648ab59075eb09923c0-2" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-2"&gt;&lt;/a&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;"""Checks that the provided set of arguments matches the set of require arguments"""&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-3" name="rest_code_53d777eb752a4648ab59075eb09923c0-3" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-3"&gt;&lt;/a&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-4" name="rest_code_53d777eb752a4648ab59075eb09923c0-4" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-4"&gt;&lt;/a&gt;    &lt;span class="n"&gt;provided_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provided_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-5" name="rest_code_53d777eb752a4648ab59075eb09923c0-5" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-5"&gt;&lt;/a&gt;    &lt;span class="n"&gt;required_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;required_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-6" name="rest_code_53d777eb752a4648ab59075eb09923c0-6" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-6"&gt;&lt;/a&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-7" name="rest_code_53d777eb752a4648ab59075eb09923c0-7" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-7"&gt;&lt;/a&gt;    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;required_set&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;provided_set&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;provided_set&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;required_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-8" name="rest_code_53d777eb752a4648ab59075eb09923c0-8" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-8"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="p"&gt;{}:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-9" name="rest_code_53d777eb752a4648ab59075eb09923c0-9" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-9"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{}:&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-10" name="rest_code_53d777eb752a4648ab59075eb09923c0-10" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-10"&gt;&lt;/a&gt;            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"MISSING ARGUMENTS: arguments &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; were required, but not provided"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-11" name="rest_code_53d777eb752a4648ab59075eb09923c0-11" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-11"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="n"&gt;extra&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-12" name="rest_code_53d777eb752a4648ab59075eb09923c0-12" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-12"&gt;&lt;/a&gt;            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"EXTRA ARGUMENTS: arguments &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;extra&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; were provided, but not required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-13" name="rest_code_53d777eb752a4648ab59075eb09923c0-13" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-13"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extra&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-14" name="rest_code_53d777eb752a4648ab59075eb09923c0-14" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-14"&gt;&lt;/a&gt;        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"WRONG ARGUMENTS: arguments &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; were required, but not provided, "&lt;/span&gt;
&lt;a id="rest_code_53d777eb752a4648ab59075eb09923c0-15" name="rest_code_53d777eb752a4648ab59075eb09923c0-15" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_53d777eb752a4648ab59075eb09923c0-15"&gt;&lt;/a&gt;        &lt;span class="s2"&gt;"and arguments &lt;/span&gt;&lt;span class="si"&gt;{extra}&lt;/span&gt;&lt;span class="s2"&gt; were provided, but not required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The error should be much more readable now, and the code is also very clear and explicit. Surely there is no error with this, right?&lt;/p&gt;
&lt;section id="the-problem"&gt;
&lt;h2&gt;The problem&lt;/h2&gt;
&lt;p&gt;By now, anyone used to working with sets in Python should be banging their head against the keyboard…&lt;/p&gt;
&lt;p&gt;You see, Python has a nice syntax for sets: &lt;code class="docutils literal"&gt;{a1 [, a2 … ]}&lt;/code&gt;.
It also has a nice syntax for dictionaries: &lt;code class="docutils literal"&gt;{[k1: v1, &lt;span class="pre"&gt;...]}&lt;/span&gt;&lt;/code&gt;.
But they intersect in a very unfortunate way: &lt;code class="docutils literal"&gt;{}&lt;/code&gt; represents an empty dictionary.&lt;/p&gt;
&lt;p&gt;Not an empty set. There is no literal syntax for an empty set in Python.&lt;/p&gt;
&lt;p&gt;And —very consistently— the pattern matching syntax &lt;code class="docutils literal"&gt;{}&lt;/code&gt; corresponds to the empty &lt;a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#mapping-patterns"&gt;mapping pattern&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So never is it going to match the empty set —or any set, for that matter.&lt;/p&gt;
&lt;p&gt;At this point I could do the obvious thing: write it into a succession of &lt;code class="docutils literal"&gt;&lt;span class="pre"&gt;if-elif-else&lt;/span&gt;&lt;/code&gt; blocks. But it would be —in my opinion— slightly less readable. &lt;em&gt;Also, where would be the fun in that&lt;/em&gt; &lt;a class="brackets" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#realreason" id="footnote-reference-1" role="doc-noteref"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;1&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/a&gt; .&lt;/p&gt;
&lt;p&gt;So I set for the way to the syntax for pattern matching on sets, only to find out… there is none. At first I thought "Sets are iterables right?
And they have a &lt;code class="docutils literal"&gt;__len__&lt;/code&gt;. So maybe
&lt;a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#sequence-patterns"&gt;sequence patterns&lt;/a&gt;  will work?"&lt;/p&gt;
&lt;p&gt;Except, as their name indicates, sequence patterns only match sequences (objects that provide &lt;code class="docutils literal"&gt;__len__&lt;/code&gt; and &lt;code class="docutils literal"&gt;__getitem__&lt;/code&gt;), and sets are not sequences (since they are not ordered).&lt;/p&gt;
&lt;p&gt;I had to face it: there is actually &lt;a class="reference external" href="https://www.youtube.com/watch?v=WWaLxFIVX1s"&gt;no literal pattern to check sets&lt;/a&gt;.&lt;/p&gt;
&lt;/section&gt;
&lt;section id="time-to-hack"&gt;
&lt;h2&gt;Time to hack&lt;/h2&gt;
&lt;p&gt;That is when I remembered a blog posts I had read a while ago TODO LINK&lt;/p&gt;
&lt;p&gt;Python has several ways to hijack instance checking.
The easiest and most direct is to create a &lt;em&gt;metaclass&lt;/em&gt; that defines a &lt;code class="docutils literal"&gt;__instancecheck__&lt;/code&gt; method. All classes that are instances of that &lt;em&gt;metaclass&lt;/em&gt; will use that method for instance checking.&lt;/p&gt;
&lt;p&gt;Exploiting that, one can change the behaviour of &lt;a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#class-patterns"&gt;class patterns&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Basically we simply have to create a class &lt;code class="docutils literal"&gt;EmptySet&lt;/code&gt; so that &lt;code class="docutils literal"&gt;isinstance(x, EmptySet)&lt;/code&gt; returns &lt;code class="docutils literal"&gt;True&lt;/code&gt; iff &lt;code class="docutils literal"&gt;x&lt;/code&gt; is an empty &lt;code class="docutils literal"&gt;set&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Anyway, in a dozen lines, I had a working empty set checker:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-1" name="rest_code_55870ab21f234c46979d695aae93f14a-1" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;CheckerMeta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-2" name="rest_code_55870ab21f234c46979d695aae93f14a-2" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-2"&gt;&lt;/a&gt;    &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Callable&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-3" name="rest_code_55870ab21f234c46979d695aae93f14a-3" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-3"&gt;&lt;/a&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-4" name="rest_code_55870ab21f234c46979d695aae93f14a-4" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-4"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;__call__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-5" name="rest_code_55870ab21f234c46979d695aae93f14a-5" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-5"&gt;&lt;/a&gt;        &lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kwargs&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-6" name="rest_code_55870ab21f234c46979d695aae93f14a-6" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-6"&gt;&lt;/a&gt;        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Class with metaclass CheckerMeta shouldn't be instantiated"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-7" name="rest_code_55870ab21f234c46979d695aae93f14a-7" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-7"&gt;&lt;/a&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-8" name="rest_code_55870ab21f234c46979d695aae93f14a-8" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-8"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;__instancecheck__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-9" name="rest_code_55870ab21f234c46979d695aae93f14a-9" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-9"&gt;&lt;/a&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-10" name="rest_code_55870ab21f234c46979d695aae93f14a-10" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-10"&gt;&lt;/a&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-11" name="rest_code_55870ab21f234c46979d695aae93f14a-11" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-11"&gt;&lt;/a&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;Checker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metaclass&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;CheckerMeta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-12" name="rest_code_55870ab21f234c46979d695aae93f14a-12" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-12"&gt;&lt;/a&gt;    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-13" name="rest_code_55870ab21f234c46979d695aae93f14a-13" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-13"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-14" name="rest_code_55870ab21f234c46979d695aae93f14a-14" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-14"&gt;&lt;/a&gt;        &lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-15" name="rest_code_55870ab21f234c46979d695aae93f14a-15" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-15"&gt;&lt;/a&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-16" name="rest_code_55870ab21f234c46979d695aae93f14a-16" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-16"&gt;&lt;/a&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-17" name="rest_code_55870ab21f234c46979d695aae93f14a-17" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-17"&gt;&lt;/a&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;EmptySet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Checker&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-18" name="rest_code_55870ab21f234c46979d695aae93f14a-18" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-18"&gt;&lt;/a&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;"""``isinstance(x, EmptySet)`` checks if x is an empty ``set()`` instance."""&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-19" name="rest_code_55870ab21f234c46979d695aae93f14a-19" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-19"&gt;&lt;/a&gt;    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-20" name="rest_code_55870ab21f234c46979d695aae93f14a-20" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-20"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_55870ab21f234c46979d695aae93f14a-21" name="rest_code_55870ab21f234c46979d695aae93f14a-21" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_55870ab21f234c46979d695aae93f14a-21"&gt;&lt;/a&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now I could write&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-1" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-1" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;check_arguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provided_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;required_arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-2" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-2" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-2"&gt;&lt;/a&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="sd"&gt;"""Checks that the provided set of arguments matches the set of require arguments"""&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-3" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-3" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-3"&gt;&lt;/a&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-4" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-4" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-4"&gt;&lt;/a&gt;    &lt;span class="n"&gt;provided_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provided_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-5" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-5" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-5"&gt;&lt;/a&gt;    &lt;span class="n"&gt;required_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;required_arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-6" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-6" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-6"&gt;&lt;/a&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-7" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-7" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-7"&gt;&lt;/a&gt;    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;required_set&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;provided_set&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;provided_set&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;required_set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-8" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-8" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-8"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EmptySet&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;EmptySet&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-9" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-9" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-9"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EmptySet&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-10" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-10" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-10"&gt;&lt;/a&gt;            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"MISSING ARGUMENTS: arguments &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; were required, but not provided"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-11" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-11" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-11"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EmptySet&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;extra&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-12" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-12" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-12"&gt;&lt;/a&gt;            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"EXTRA ARGUMENTS: arguments &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;extra&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; were provided, but not required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-13" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-13" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-13"&gt;&lt;/a&gt;        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extra&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-14" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-14" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-14"&gt;&lt;/a&gt;        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"WRONG ARGUMENTS: arguments &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; were required, but not provided,"&lt;/span&gt;
&lt;a id="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-15" name="rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-15" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#rest_code_ed02ef911bd04bc4b8d14cbbd46c97aa-15"&gt;&lt;/a&gt;        &lt;span class="s2"&gt;" and arguments &lt;/span&gt;&lt;span class="si"&gt;{extra}&lt;/span&gt;&lt;span class="s2"&gt; were provided, but not required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Problem solved! Pretty readable (though sadly not as much as the version that didn't work).&lt;/p&gt;
&lt;/section&gt;
&lt;section id="how-bad-is-it-doctor"&gt;
&lt;h2&gt;How bad is it, doctor?&lt;/h2&gt;
&lt;p&gt;Now this is kind of hackish. I'm honestly not sure how I would feel about putting this in code that people other than me may actually use.&lt;/p&gt;
&lt;p&gt;On one hand, any code that employs a metaclass raises an alarm in my head. They are a pretty nice abstraction, but I am pretty sure at least 50% of python developers out there have no idea how they work.&lt;/p&gt;
&lt;p&gt;On the other hand, this is a pretty short piece of code. It is also pretty clear what it does from the documentation. And it should be pretty easy to understand for someone who actually knows the language features in use here.&lt;/p&gt;
&lt;/section&gt;
&lt;section id="now-what-about"&gt;
&lt;h2&gt;Now what about...&lt;/h2&gt;
&lt;p&gt;A "BlankString" class ? Or classes for strings that match a certain regex ? for entertainment purpose only though, don't do this at home.&lt;/p&gt;
&lt;aside class="footnote-list brackets"&gt;
&lt;aside class="footnote brackets" id="realreason" role="doc-footnote"&gt;
&lt;span class="label"&gt;&lt;span class="fn-bracket"&gt;[&lt;/span&gt;&lt;a role="doc-backlink" href="https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/#footnote-reference-1"&gt;1&lt;/a&gt;&lt;span class="fn-bracket"&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;That's the real reason.&lt;/p&gt;
&lt;/aside&gt;
&lt;/aside&gt;
&lt;/section&gt;</description><guid>https://tristan.bruge.re/fr/posts/hacking-your-way-through-pattern-matching-on-sets/</guid><pubDate>Tue, 14 May 2024 22:00:07 GMT</pubDate></item></channel></rss>