LWN: Comments on ""Structural pattern matching" for Python, part 2" https://lwn.net/Articles/828486/ This is a special feed containing comments posted to the individual LWN article titled ""Structural pattern matching" for Python, part 2". en-us Sun, 19 Oct 2025 12:54:34 +0000 Sun, 19 Oct 2025 12:54:34 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831806/ https://lwn.net/Articles/831806/ udifuchs <div class="FormattedComment"> I know that a computer parser could easily understand this syntax. Still, as a human, I find it confusing. Most keywords in python are followed by an expression. The case keyword is followed by a pattern that has a syntax similar to an expression. This is confusing.<br> <p> The PEP mentions that assignment targets in python also look like expressions, but this is true in a very narrow way. It is a syntax error to write &#x27;int(x) = 3&#x27;, while &#x27;case int(x):&#x27; is valid.<br> <p> More specifically, while reading the is_tuple() example in the PEP, it took me a while to realize that LParen() is initialized in the original code but not in the new code. If LParen.__init__() has some side effects, the two versions of the code that look very similar could be very different. <br> <p> </div> Fri, 18 Sep 2020 04:23:15 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831663/ https://lwn.net/Articles/831663/ HelloWorld <div class="FormattedComment"> <font class="QuotedText">&gt; Dispatching on types is just not a good way to write code in a dynamically typed language. There is no - and can be no - exhaustiveness checking for example, to give just one example of why it&#x27;s a problem.</font><br> <p> That problem has nothing to do with pattern matching and everything to do with dynamic typing, which is quite simply an inherently bad idea.<br> </div> Wed, 16 Sep 2020 16:02:20 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831659/ https://lwn.net/Articles/831659/ HelloWorld <div class="FormattedComment"> <font class="QuotedText">&gt; case Point2D(x, y):</font><br> <font class="QuotedText">&gt; </font><br> <font class="QuotedText">&gt; It looks like the class is being initiated.</font><br> <p> No, it doesn&#x27;t, because there is a case keyword right before it, it&#x27;s pretty much impossible to miss. If the reader doesn&#x27;t know what that means and starts making wild guesses, the problem isn&#x27;t with the language but with the reader.<br> </div> Wed, 16 Sep 2020 15:59:52 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831552/ https://lwn.net/Articles/831552/ udifuchs <div class="FormattedComment"> The syntax that I find confusing is the class pattern matching:<br> <p> match pt:<br> case Point2D(x, y):<br> <p> It looks like the class is being initiated. <br> I was wondering if using square brackets would make more sense,<br> as it is similar to the syntax used for type annotations.<br> For example:<br> <p> match pt:<br> case Point2D[x, y]:<br> <p> One issue with this idea is that the square bracket use might be confused with<br> item access. In enum, for example, this is a valid syntax:<br> <p> <font class="QuotedText">&gt;&gt;&gt; Sides[&quot;SPAM&quot;] == Sides.SPAM == Sides(&quot;Spam&quot;)</font><br> True<br> <p> Resolving this conflict would probably require not allowing such item access<br> inside a case statement.<br> </div> Tue, 15 Sep 2020 13:28:42 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831397/ https://lwn.net/Articles/831397/ plugwash <div class="FormattedComment"> <font class="QuotedText">&gt; You are correct that a PyObject is a tagged union.</font><br> <p> I would argue it&#x27;s more akin to a base class than to a tagged union with the &quot;ob_type&quot; field serving a role similiar to a vptr.<br> <p> To me at least &quot;tagged union&quot; implies that the list of subtypes is determined in advance and sufficient memory is allocated to store any of the subtypes. <br> <p> <p> </div> Sun, 13 Sep 2020 11:11:35 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831325/ https://lwn.net/Articles/831325/ milesrout <div class="FormattedComment"> <font class="QuotedText">&gt;Is there any indication that something better than `isinstance` if-trees was explicitly rejected earlier in the language&#x27;s design process?</font><br> <p> Yes. Switch statement suggestions have been rejected over and over again. Pattern matching proposals have been rejected before. &#x27;if isinstance&#x27; isn&#x27;t considered bad Python because it&#x27;s *ugly* but because it&#x27;s *bad design*. Dispatching on types is just not a good way to write code in a dynamically typed language. There is no - and can be no - exhaustiveness checking for example, to give just one example of why it&#x27;s a problem.<br> <p> <font class="QuotedText">&gt;but with types now becoming more and more of a thing in general (even in Python), this seems like a natural thing to be considering around this time (to me at least).</font><br> <p> Adding type annotations to Python is yet another example of the continual application of bad ideas to clutter up Python, done by people trying to roleplay as Haskell programmers. Pattern matching is the same thing. It&#x27;s blatant cargo culting.<br> </div> Sat, 12 Sep 2020 04:30:26 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831254/ https://lwn.net/Articles/831254/ mathstuf <div class="FormattedComment"> <font class="QuotedText">&gt; It&#x27;s not supported by the language *for good reason*.</font><br> <p> This feels like one of those &quot;cannot derive ought from is&quot; statements. Is there any indication that something better than `isinstance` if-trees was explicitly rejected earlier in the language&#x27;s design process? I&#x27;d expect the previous approach was &quot;just treat it like a duck and if it quacks well enough, what do you care?&quot;, but with types now becoming more and more of a thing in general (even in Python), this seems like a natural thing to be considering around this time (to me at least).<br> </div> Fri, 11 Sep 2020 13:27:14 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831245/ https://lwn.net/Articles/831245/ milesrout <div class="FormattedComment"> And what I am saying, quite clearly I think, is that I do not think that that is the right thing to be doing in Python code. It&#x27;s not supported by the language *for good reason*.<br> </div> Fri, 11 Sep 2020 12:53:54 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831243/ https://lwn.net/Articles/831243/ johill <div class="FormattedComment"> Well, that&#x27;s a question of your architecture/design, but a large part of the whole point of the match statement was to be able to match different types in the same place.<br> </div> Fri, 11 Sep 2020 06:29:32 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831162/ https://lwn.net/Articles/831162/ mathstuf <div class="FormattedComment"> Ah, I see now what you&#x27;re doing. Yes, that&#x27;s a nifty trick (assuming you can guarantee that you&#x27;re only using rebound variables rather than holding a window into a variable someone else eventually mutates). But it&#x27;s Python and I consider that kind of thinking just necessary for making robust software in that language.<br> </div> Thu, 10 Sep 2020 13:21:58 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831119/ https://lwn.net/Articles/831119/ milesrout That's quite intentional. If you are passing the wrong type of value to a function then your code should crash. The code will also crash if I pass a string to a function that expects an integer. Code should not be full of defensive checks for programmer error: they're code paths that are hard if not impossible to test and by design are intended to never be run (nobody makes programmer errors on purpose). Type checks are for dispatching based on type in Python, and most of the time 'isinstance' checks are the wrong way of doing things. "if isinstance elif isinstance elif isinstance"-style code is considered unPythonic, and I don't think that papering over that code with a language construct makes it a good idea. <p>Now there are possibly some cases where you want to be able to pass different sizes of tuples to a single function. However I think in most of those cases you probably shouldn't be using tuples, you should be using something like a named tuple, a dictionary, a custom class, a dataclass with an optional field, or just separate arguments to a single function, some of which are optional. <p>Or possibly you should actually be making a decision 'am I working with 2-tuples or 3-tuples?' at a higher level and switch. If you had some code that is meant to work with 2D or 3D or 4D vectors for example, it would be better to have separate functions for the length of 2D, 3D and 4D vectors than a single function that does <pre><code> def veclen(v): match v: case (x,y): return sqrt(x*x + y*y) case (x,y,z): return sqrt(x*x + y*y + z*z) case (x,y,z,w): return sqrt(x*x + y*y + z*z + w*w) </code></pre> <p>Then you should make the determination as to whether you are working with 2D, 3D or 4D vectors at a high level and write all the functions dealing with 2D, 3D or 4D vectors separately. <p>For example if you really want to write 'length-agnostic' code with tuples up to some maximum length, then you should encapsulate that variation in size in Vector2D, Vector3D and Vector4D etc. classes with a common Vector superclass such that Vector's methods can be those that are parametric in the dimension. <p>And if it's really truly length-agnostic tuple code then you should be writing loops over lists, not matching on tuples. Thu, 10 Sep 2020 02:12:08 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831117/ https://lwn.net/Articles/831117/ milesrout <div class="FormattedComment"> That&#x27;s conflating two separate things: capturing names and capturing names that refer to mutable values.<br> <p> <font class="QuotedText">&gt;&gt;&gt; a = 0</font><br> <font class="QuotedText">&gt;&gt;&gt; b = []</font><br> <font class="QuotedText">&gt;&gt;&gt; f = lambda: print(a)</font><br> <font class="QuotedText">&gt;&gt;&gt; g = lambda a=a: print(a)</font><br> <font class="QuotedText">&gt;&gt;&gt; h = lambda: print(b)</font><br> <font class="QuotedText">&gt;&gt;&gt; i = lambda b=b: print(b)</font><br> <font class="QuotedText">&gt;&gt;&gt; (f(), g(), h(), i())</font><br> 0<br> 0<br> []<br> []<br> <font class="QuotedText">&gt;&gt;&gt; a = 1; c.append(3)</font><br> <font class="QuotedText">&gt;&gt;&gt; (f(), g(), h(), i())</font><br> 1<br> 0<br> [3]<br> [3]<br> <p> f captures the *name* &#x27;a&#x27;, which refers to the immutable value 0. g has an optional parameter, the default value of which is the value of a i.e. the immutable value 0. Like any Python function, g will store internally references to the default values for its parameters. It can&#x27;t &#x27;capture&#x27; the variable a because you could put *any* expression as the default value for g&#x27;s parameter, and that expression is evaluated *when the function is defined*, with the resulting value stored inside the closure object somewhere. <br> <p> When f is called, it prints a i.e. the value referred to by the name &#x27;a&#x27; that f captured. If &#x27;a&#x27; has changed then what f prints will change. When g is called, it prints the value of its parameter or if it isn&#x27;t given one, the default value, which is zero. The &#x27;a&#x27; in f refers not to the value that &#x27;a&#x27; had when f was created, but to the name &#x27;a&#x27; itself in the definition environment of f. <br> <p> When a is rebound to 1, it doesn&#x27;t change the value that a was originally bound to. &#x27;a = 1&#x27; is an operation on the name &#x27;a&#x27;, not an operation on the value pointed to by a. We reassign &#x27;a&#x27; so that it refers to a different value, but we don&#x27;t modify the value that &#x27;a&#x27; previously pointed to. So when f() is called a second time, it prints 1 (the new value pointed to by a) while when g() is called a second time it prints 0 (the value you can think of as being pointed to by some &#x27;g.__internal_stuff__.parameters[0].default_value&#x27; attribute). <br> <p> h and i actually work exactly the same way, but with one crucial difference: the value that the name &#x27;b&#x27; refers to is a list and is thus mutable. So when we do the &#x27;b.append(3)&#x27; operation we are not changing the name &#x27;b&#x27;! We&#x27;re modifying the value *pointed to by b*. When we defined i, we evaluated the expression &quot;b&quot; and pointed some internal &quot;this is the default value of my parameter&quot; field of the closure object at the result of that evaluation. Evaluating the expression &quot;b&quot; results in that list object, the list object pointed to by the name &#x27;b&#x27;. <br> <p> So when we call h(), it prints the value pointed to by the name &#x27;b&#x27;. When we call i(), it prints the value of parameter, if it exists, or the value pointed to by some internal &#x27;default&#x27; reference for that parameter. That default reference hasn&#x27;t been changed, it still points to the same object. However that object itself has been changed by the call to append. We only ever created *one* list object, and there are two references to it. <br> <p> The &#x27;mutable default value&#x27; issue for Python functions basically has nothing to do with variable capture at all. It&#x27;s present even for normal function definitions:<br> <p> def foo(x=[]):<br> x.append(1)<br> print(x)<br> <p> <font class="QuotedText">&gt;&gt;&gt; foo()</font><br> [1]<br> <font class="QuotedText">&gt;&gt;&gt; foo()</font><br> [1, 1]<br> <p> The main problem that the variable capture issue causes happens even with immutable values, and in fact is primarily an immutable value issue. The first time I encountered it was with a GUI library. I was doing something like this:<br> <p> <font class="QuotedText">&gt;&gt;&gt; for i in range(5):</font><br> <font class="QuotedText">&gt;&gt;&gt; buttons[i].on_click(lambda: buttons[i].colour = RED)</font><br> <p> but of course the problem with this is that all the buttons will make the last button red when clicked. That&#x27;s not because of a mutable value: the value pointed to by i doesn&#x27;t change. The problem here is that a for loop assigns to the iteration variable. That code is the same as writing<br> <p> <font class="QuotedText">&gt;&gt;&gt; i = 0</font><br> <font class="QuotedText">&gt;&gt;&gt; buttons[i].on_click(lambda: buttons[i].colour = RED)</font><br> <font class="QuotedText">&gt;&gt;&gt; i = 1</font><br> <font class="QuotedText">&gt;&gt;&gt; buttons[i].on_click(lambda: buttons[i].colour = RED)</font><br> etc.<br> <p> And there it&#x27;s more clear what&#x27;s happening vis a vis name capture. The solution is of course to instead write buttons[i].on_click(lambda i=i: buttons[i].colour = RED).<br> <p> </div> Thu, 10 Sep 2020 02:11:40 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831111/ https://lwn.net/Articles/831111/ johill <div class="FormattedComment"> Your version crashes if you pass it a 3-tuple or something else.<br> <p> The match/case version can handle such things without you having to put another &quot;is a 2-tuple&quot; if outside of it.<br> </div> Wed, 09 Sep 2020 22:16:11 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831043/ https://lwn.net/Articles/831043/ mathstuf <div class="FormattedComment"> Hmm. That doesn&#x27;t seem fool-proof:<br> <p> <font class="QuotedText">&gt;&gt;&gt; d = []</font><br> <font class="QuotedText">&gt;&gt;&gt; f = lambda x=d: print(x)</font><br> <font class="QuotedText">&gt;&gt;&gt; f()</font><br> []<br> <font class="QuotedText">&gt;&gt;&gt; d.append(3)</font><br> <font class="QuotedText">&gt;&gt;&gt; f()</font><br> [3]<br> <p> Unless I&#x27;m missing what you&#x27;re trying to capture here?<br> </div> Wed, 09 Sep 2020 14:37:40 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831000/ https://lwn.net/Articles/831000/ milesrout <div class="FormattedComment"> Python used to have simple and pleasant syntax. With async/await, := assignment and now this, the syntax seems to be getting significantly more complex with every new version of the language for very little benefit. <br> </div> Wed, 09 Sep 2020 13:37:46 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830998/ https://lwn.net/Articles/830998/ milesrout At that point, how is this better than <pre><code> (x, y) = size if x == y: circle() else: ellipse() </code></pre> I would contend that it is not. It is an extra level of indentation and a whole new complicated language construct. Wed, 09 Sep 2020 13:37:36 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830997/ https://lwn.net/Articles/830997/ milesrout And of course there is in also a very easy solution: <code>lambda i=i: i</code> captures <code>i</code> &ldquo;properly&rdquo;. Perhaps not very intuitive for beginners, but once you've learnt it you've learnt it. Wed, 09 Sep 2020 13:37:25 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/831001/ https://lwn.net/Articles/831001/ johill <div class="FormattedComment"> <font class="QuotedText">&gt; As long as CapturedVar suitably &#x27;ducks&#x27; its type (e.g. for ints, implementing + and **, like you suggested), Point.__init__ should be entirely happy with it unless it&#x27;s trying to do things that depend on the actual values.</font><br> <p> I don&#x27;t think that&#x27;ll work. It has to implement the algebraic operations to build a parse tree to be able to do algebraic matching, but you still can&#x27;t really call Point.__init__ with it I&#x27;d think, it might check the type, or other random things in there. Hence the match protocol though!<br> <p> <font class="QuotedText">&gt; Sure, it&#x27;s not something that will work for every class, but nor is the rest of the &quot;Match Protocol&quot; in PEP 622. And if the class has a __match_args__, then you could potentially have a matching.Duck that uses that to &quot;play the rôle of&quot; the class in a match expression: [...]</font><br> <p> But yeah, you&#x27;re right, the PEP also cannot do magic wrt. class matching, it has __match_args__ for that, and indeed with something like the &quot;Duck()&quot; you propose - or what I wrote as &quot;m := matches_instance(Point, capture.x, capture.y)&quot; - you can indeed do the same thing.<br> <p> I neglected to think enough about it I suppose, but (fairly obviously) the only magic in the PEP can be the syntax, making &quot;Point(x, y)&quot; not be an instantiation call but some other kind of expression. Without new syntax we have to open that up manually by doing &quot;Duck(Point)(capture.x, capture.y)&quot; or my &quot;m := matches_instance(Point, capture.x, capture.y)&quot;.<br> <p> The rest ... yeah can probably be done, now that I think about it. In mostly the same ways.<br> <p> And that shows to some extent where and how the algebraic matching breaks down and probably why they punted on it, though I&#x27;m not convinced it&#x27;s that bad. Obviously, you don&#x27;t want to get into the territory of writing &quot;Point(x**2, x**3)&quot; and suddenly needing an arbitrary solver to do the match, but I think you could do it if you restrict to having to mention each variable &quot;naked&quot; at least once. I.e. &quot;Point(x, x**3)&quot; would be OK and &quot;Point3d(x, y, x+y)&quot; would also be OK - all of this because once you have values for x/y it&#x27;s easy to check the remaining expressions. But something like &quot;Point(x+y, x-y)&quot; would not be allowed.<br> </div> Wed, 09 Sep 2020 06:22:09 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830992/ https://lwn.net/Articles/830992/ ecree <blockquote>because you quite probably cannot instantiate a Point() object with two CapturedVar instances.</blockquote> <p>But can't you, though?</p> <p>As long as <code>CapturedVar</code> suitably 'ducks' its type (e.g. for ints, implementing + and **, like you suggested), <code>Point.__init__</code> should be entirely happy with it unless it's trying to do things that depend on the actual values.</p> <p>Sure, it's not something that will work for <i>every</i> class, but nor is the rest of the <a href="https://www.python.org/dev/peps/pep-0622/#the-match-protocol">"Match Protocol"</a> in PEP 622. And if the class has a <code>__match_args__</code>, then you could potentially have a <code>matching.Duck</code> that uses that to "play the rôle of" the class in a match expression:</p> <p><pre><code>from matching import match, capture, Duck with match(p) as matches: if m := matches(Duck(Point)(capture.x(int), capture.x(int))): print("Diagonal at %d" % (m.x, )) elif m := matches(capture.p(Point)(capture.x(int), capture.y(int))): # the second call on capture.p implies a Duck # if the match succeeds, it is replaced with the concrete Point print("Other point at %s % (m.p, ))</code></pre></p> <p>So you'd use plain <code>Point()</code> for dataclasses and anything else that's not too fussy, <code>Duck(Point)()</code> otherwise.</p> <p>Still not <i>quite</i> as pretty as the PEP 622 syntax, but it still seems like a good way to get experience with "how this feature would be used in practice" before baking it into the language. Which to my mind is reason enough to do it.</p> Tue, 08 Sep 2020 23:39:39 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830973/ https://lwn.net/Articles/830973/ johill <div class="FormattedComment"> But actually...<br> <p> while all of that works nicely with tuples because you can build some knowledge of them into the code, it really doesn&#x27;t work at all with what the PEP calls &quot;Class Patterns&quot;.<br> <p> You can&#x27;t write &quot;m := matches(Point(capture.x, capture.y))&quot; because you quite probably cannot instantiate a Point() object with two CapturedVar instances.<br> <p> so you&#x27;d have to rewrite that as something like &quot;m := matches_instance(Point, capture.x, capture.y)&quot; at which point (pun intended) new syntax seems to make sense...<br> </div> Tue, 08 Sep 2020 18:33:48 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830970/ https://lwn.net/Articles/830970/ johill I also just realized that I sort of sketched the basics of "algebraic matching", where the original PEP completely bypassed that issue and (x, x) doesn't even do what seems obvious? Or am I reading that wrong? <p> Of course some more complex expressions would be ... hard. Probably not impossible, but hard. You'd have to make the CapturedVar() be able to track all kinds of things, if you wanted to be able to write <pre> ... if m := matches((capture.x(int), capture.x(int)**2)): print("Integer point on the parabola at %d" % m.x) ... </pre> It still seems possible, but ... tricky to say the least. Effectively, "CapturedVar" would have to be able to be operated on in a symbolic fashion, always returning a new CapturedVar object that encapsulates the expression, and can later evaluate it. So given an instance <pre> x_square := CapturedVar('x') ** 2 </pre> you'd have to be able to calculate <pre>x_square(7)</pre> so you can compare to <pre>capture.x(7)</pre>. <p> But you can see the ambiguity here! I previously allowed <pre>capture.x(int)</pre> to indicate you wanted a specific type ... <p> This would be better if the types were to rely on type annotations, but I don't think you can do that in this fashion. <p> I suppose you could detect if the argument was a type or an instance or something? But tricky to differentiate ... <p> Oh, I have an idea. The <pre>x_square(7)</pre> calculation is purely internal, so we can do it as <pre>x_square(value=y)</pre> (say the signature is "__call__(self, type=None, value=None)" so you can differentiate more easily. The value passing syntax is only needed internally to evaluate when you have a given thing in hand. <p> Well, basically ... it get complex, but I haven't hit anything I couldn't do. <p> Now I'm tempted to actually implement it :-) Tue, 08 Sep 2020 17:39:44 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830958/ https://lwn.net/Articles/830958/ ecree <div class="FormattedComment"> <font class="QuotedText">&gt; But I believe it can be done.</font><br> <p> I agree. And that&#x27;s a clever idea of yours to use a context manager to get the &#x27;only mention p once&#x27; semantics.<br> </div> Tue, 08 Sep 2020 16:15:12 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830901/ https://lwn.net/Articles/830901/ johill Sorry, obviously in both cases the first if was meant to be <pre> if m := matches((capture.x(int), capture.x(int))): print("Diagonal at %d" % (m.x, )) </pre> And that of course shows that the matches() call needs to be fairly smart about disentangling instances of CapturedVar() etc. But I believe it can be done. Tue, 08 Sep 2020 07:34:43 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830900/ https://lwn.net/Articles/830900/ johill And maybe CapturedVar() can be callable again, returning another CapturedVar() instance with a type attached, so you can write <pre> from matching import match, capture with match(p) as matches: if m := matches((capture.x(int), capture.y(int))): print("Diagonal at %d" % (m.x, )) elif m := matches((0, capture.y(int))): print("X-axis at %d" % (m.y, )) elif m := matches((capture.x(int), capture.y(int))): print("Point at (%d, %d)" % (m.x, m.y)) elif m := matches((capture.x, capture.y)): raise ValueError("It's a tuple, but not with integers") else: raise ValueError("Not a 2-tuple") return Point(*p) </pre> Tue, 08 Sep 2020 07:29:40 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830899/ https://lwn.net/Articles/830899/ johill You can probably use some more "magic" to make that shorter, something like <pre> from matching import match, capture with match(p) as matches: if m := matches((capture.x, capture.y)): print("Diagonal at %d" % (m.x, )) elif m := matches((0, capture.y)): print("X-axis at %d" % (m.y, )) elif m := matches((capture.x, capture.y)): print("Point at (%d, %d)" % (m.x, m.y)) else: raise ValueError("Not a 2-tuple") return Point(*p) </pre> You need something like <pre> class CapturedVar: def __init__(self, name): self.name = name class capture: def __getattr__(self, name): return CapturedVar(name) </pre> and "match(...)" returns an object that is callable; the called method knows about CapturedVar instances and bumps those into the returned MatchedVars object ("m"). Tue, 08 Sep 2020 07:21:17 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830861/ https://lwn.net/Articles/830861/ ecree <p>On thinking more about this, I have a fairly fundamental question, which I didn't see raised anywhere:</p> <p>Why can't this be done as a library? Why is a language feature needed?</p> <p>Something like <pre><code> import matching # ... if m := matching.match(p, (matching.capture('x'), matching.capture('x')): print("Diagonal at %d" % (m.x,)) elif m := matching.match(p, (0, matching.capture('y')): print("X-axis as %d" % (m.y,)) elif m := matching.match(p, (matching.capture('x'), matching.capture('y')): print("Point at (%d,%d)" % (m.x, m.y)) else: raise ValueError("Not a 2-tuple") return Point(*p)</code></pre> Yes, it's more verbose than the 'match' statement, but mainly because of the length of names, and still much more concise and declarative than doing everything with isinstance() and len(). And it makes the difference between constants and capture variables <em>totally</em> explicit!</p> Mon, 07 Sep 2020 16:45:37 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830796/ https://lwn.net/Articles/830796/ smitty_one_each <div class="FormattedComment"> I hadn&#x27;t heard of &quot;Fear Of Missing Out&quot; before. Thanks.<br> <p> It does seem that, with the newer release cycle, Python is going down the Java &quot;featuritis&quot; road.<br> <p> On the other hand, this seems to take Python more in a Haskell-ish direction.<br> <p> Good news? Bad news? Who can say?<br> <p> On the other hand, the languages that fail to add new features seem to be the ones nobody&#x27;s using any longer. It&#x27;s more of an economic than technical arms race, apparently.<br> </div> Sun, 06 Sep 2020 21:18:19 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830782/ https://lwn.net/Articles/830782/ kleptog An alternative to algebraic pattern matching is allowing extra conditional, like in elixir (which also offers algebraic matching). Then it could look like: <pre> match size: case (x,y) if x == y: circle() case (x,y): ellipse() </pre> The whole power of such a match statement is that you can match entire nested structures, which this PEP does offer. An optional extra if clause for the more fancy cases can help here. Sun, 06 Sep 2020 11:35:20 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830657/ https://lwn.net/Articles/830657/ mathstuf I was worried about the same thing too, but the walrus operator saves you in this related case as well: <pre> match t := some_expr(): case ...: case _: # use t </pre> Though I don't like the locality mismatch here. For a large (probably considered un-Pythonic) match, your variable assignment is far away when it could be right next to it. You also have to consider that `t` is now always assigned to rather than just in the catch-all case. Fri, 04 Sep 2020 16:04:23 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830655/ https://lwn.net/Articles/830655/ ecree <p>Apparently the PEP authors decided to punt on what they call <a href="https://www.python.org/dev/peps/pep-0622/#algebraic-matching-of-repeated-names">"algebraic matching"</a> of repeated names.</p> <p>IMNSHO, if this PEP isn't doing algebraic matching then it's really far from clear what it's <i>for</i>, and I'm tempted to side with those who say this creature is being feeped purely for the sake of feeping.</p> Fri, 04 Sep 2020 15:54:00 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830604/ https://lwn.net/Articles/830604/ gray_-_wolf <div class="FormattedComment"> Given that the same variable in the match multiple times is illegal, how is one supposed to match things like Point with x == y?<br> <p> I would have assume that<br> <p> match p:<br> case (x, x):<br> <p> would match points where p.x == p.y. However that does not seem to be the case. How can I express such a match?<br> <p> Second question, if constants must be namespaced, is there no way to express something like:<br> <p> x = 1<br> match p:<br> case (x, y):<br> <p> For points that are `(1, .*)`? Is there something like `local` namespaces for local variables in python?<br> <p> I&#x27;m not a python programmer, so I might be lacking some parts of the proper mindset, but it seems... limiting.<br> </div> Fri, 04 Sep 2020 11:31:54 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830559/ https://lwn.net/Articles/830559/ NYKevin <div class="FormattedComment"> lambda does create a new lexical scope already, and it doesn&#x27;t help with that problem. In order for your example to work &quot;correctly,&quot; you would need to establish a new lexical scope for each iteration (not just one for the whole loop, because it would still have i=4 at the end of the loop). I would find that very hard to reason about, because that&#x27;s not what I think of when I hear the word &quot;lexical.&quot;<br> <p> What (I think) you really want is capture-by-value semantics instead of capture-by-name, which has nothing to do with whether or not the loop has a separate scope.<br> </div> Thu, 03 Sep 2020 22:48:04 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830533/ https://lwn.net/Articles/830533/ jcpunk I agree. I've yet to be convinced this is actually any better than <pre> if XXX: print(x) elseif YYY: print(y) elseif ZZZ: print(z) else: raise ValueError() </pre> Thu, 03 Sep 2020 16:38:43 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830500/ https://lwn.net/Articles/830500/ sbaugh <div class="FormattedComment"> <font class="QuotedText">&gt;Moreover, I&#x27;ve never run into a situation where it actually causes a problem of some sort. </font><br> <p> Isn&#x27;t the fact that Python functions aren&#x27;t true closures a direct consequence of its lack of proper lexical scope? I&#x27;d say that causes a *lot* of problems:<br> <p> &gt;&gt;&gt; fs = [lambda: i for i in range(5)]<br> &gt;&gt;&gt; [f() for f in fs]<br> [4, 4, 4, 4, 4]<br> <p> </div> Thu, 03 Sep 2020 14:17:32 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830472/ https://lwn.net/Articles/830472/ hartb I guess you still have the object-to-be-matched handy: <pre> match t: ... case _: print("error: ", str(t)) </pre> Thu, 03 Sep 2020 13:22:42 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830463/ https://lwn.net/Articles/830463/ NYKevin <div class="FormattedComment"> <font class="QuotedText">&gt; Yes, I anticipated that. I think it would be worth it, since the only way to get reasonable scopes in current Python involves nested functions or lambdas, but in the end that&#x27;s not for me to decide.</font><br> <p> How exactly would you define &quot;reasonable?&quot;<br> <p> IMHO, the Python rule is very straightforward and easy to remember. Moreover, I&#x27;ve never run into a situation where it actually causes a problem of some sort. Sure, variables hang around for a little longer than they would otherwise, but so what? If you really care, you can explicitly write del x, which is probably a Good Idea anyway, because if you care, then you should call attention to the fact that you care (not-caring is the default state of affairs, so x must be special in some way).<br> <p> <font class="QuotedText">&gt; Making it harder to mess around with frame objects seems like a feature, not a bug. Making them completely inaccessible would be even better.</font><br> <p> And while we&#x27;re at it, let&#x27;s put the e in creat(2), where it should&#x27;ve been all along.<br> <p> You can&#x27;t break backcompat on a feature just because you personally dislike that feature. It exists. It needs to be supported. Unless we want to do *another* 3.x-style flag day, frame objects are here to stay.<br> <p> <font class="QuotedText">&gt; I think that could be avoided by simply defaulting to function scope as current Python does when assigning to an unknown variable.</font><br> <p> As I mentioned in the comments the last time we discussed this idea:<br> <p> - There is no such thing as an &quot;unknown&quot; variable, except at the global scope.<br> - It is always possible to determine at compile time which variables belong to which scopes, with the exception of the global scope. The bytecode compiler will then emit bytecode that only consults the single scope where the variable actually lives, or consults the global scope if the variable is not recognized.<br> - The global scope is not known at compile time, for a number of reasons, most obviously the fact that from foo import * is legal at the global scope (it&#x27;s a SyntaxError anywhere else). This doesn&#x27;t matter, because the &quot;unknown = global&quot; algorithm is good enough for the current design.<br> - Because the global scope is not known at compile time, it is not possible to determine at compile time whether an unrecognized variable in a match expression is a global or a nonexistent variable that should be created and bound to the case&#x27;s scope.<br> - Therefore, allowing match expressions to create a new scope does not actually solve this problem, and would introduce a significant amount of complexity for no adequately justified reason.<br> </div> Thu, 03 Sep 2020 07:06:51 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830451/ https://lwn.net/Articles/830451/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; Changing variable scope to be anything other than the parent function/class/module declaration would be a massive change.</font><br> <p> Yes, I anticipated that. I think it would be worth it, since the only way to get reasonable scopes in current Python involves nested functions or lambdas, but in the end that&#x27;s not for me to decide.<br> <p> <font class="QuotedText">&gt; Anything digging around in frame objects would need to learn to poke wherever these new scopes end up being declared.</font><br> <p> Making it harder to mess around with frame objects seems like a feature, not a bug. Making them completely inaccessible would be even better. (And, apart from debugging, how would you even use such a thing without creating a very tight coupling with the function&#x27;s internal implementation? I&#x27;m not seeing any realistic scenario where this code would not need to be updated anyway.)<br> <p> <font class="QuotedText">&gt; You&#x27;d basically need to add variable declarations too…</font><br> <p> I think that could be avoided by simply defaulting to function scope as current Python does when assigning to an unknown variable. Nested block scopes would be introduced by specific statements where binding makes sense such as loop variables in a &quot;for&quot; statements, &quot;as&quot; clauses in &quot;with&quot; statements (as per the original version of PEP 343[1]), or placeholders in &quot;match&quot; statements. A &quot;local&quot; keyword could be added to explicitly introduce scoped variables into the current block, similar to the &quot;global&quot; and &quot;nonlocal&quot; keywords, if there is a demand for it.<br> <p> [1] <a href="https://docs.python.org/2.5/whatsnew/pep-343.html">https://docs.python.org/2.5/whatsnew/pep-343.html</a><br> </div> Wed, 02 Sep 2020 23:24:59 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830442/ https://lwn.net/Articles/830442/ mathstuf Changing variable scope to be anything other than the parent function/class/module declaration would be a massive change. Anything digging around in frame objects would need to learn to poke wherever these new scopes end up being declared. You'd basically need to add variable declarations too since this fairly common pattern would be broken: <pre> if foo is not None: local_foo = foo else: local_foo = DefaultValue() </pre> (Sure, here `foo` could probably be reused, but code like this still exists.) Wed, 02 Sep 2020 21:32:47 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830441/ https://lwn.net/Articles/830441/ nybble41 <div class="FormattedComment"> Maybe I just lack a properly Pythonic mindset, but the most bizarre part of this proposal to me is the fact that the names are *assigned* rather than *bound* within the lexical scope of the match arm. Every other language I know of with pattern matching creates lexical bindings for match variables rather than allowing the match itself to have side effects on existing variables. If the match placeholders were bound lexically then it would make perfect sense that they can&#x27;t be qualified. As implicit assignments, however, the restriction does seem a bit arbitrary. At the moment Python appears to lack a concept of lexical block scope. Perhaps that deficiency should be rectified before moving on more advanced features like pattern matching.<br> </div> Wed, 02 Sep 2020 21:25:14 +0000 "Structural pattern matching" for Python, part 2 https://lwn.net/Articles/830437/ https://lwn.net/Articles/830437/ mb <i>we chose a simpler rule: named constants are only recognized when referenced via some namespace, such as `mod.var` or `Color.RED`</i> <br/><br/> Oh, come on... <br/><br/> That is even more awful than using cryptic special characters like dots.<br/> That means whether a variable is read from or written to depends on how I "fetch" a variable name. <br/><br/> The equal character would IMO be the obvious and intuitive character to mark that something is going to be assigned to. <pre> match t: case (USE_RECT, real=, imag=): return complex(real, imag) case (USE_POLAR, r=, phi=): return complex(r * cos(phi), r * sin(phi)) </pre> Wed, 02 Sep 2020 19:25:01 +0000