On Sat, 10 Oct 2026 00:25:54 -0000 (UTC), I wrote:
For comparison, here is an example of Python?s match-statement. Like
all the good (i.e. non-C-based) designs, the designers saw no point in
having fall-through.
This is a very simple example, involving straight matching of values
against instances of an enum. There are also more complex
pattern-matching options available.
Here?s an example that matches the type of the match-expression (not
its value) against various event subclasses:
events = iter(self.ws.events())
while True :
event = next(events, None)
if event == None :
break
match event :
case wsevents.AcceptConnection() :
self.logger.debug("connection accepted")
#end case
case wsevents.RejectConnection() :
raise RuntimeError \
(
"WebSockets connection rejected: code %d" % event.status_code
)
#end case
case wsevents.CloseConnection() :
self.logger.debug("connection closing")
if not self.closing :
self.closing = True
if ASYNC :
await self.sock.sendall(self.sock, self.ws.send(event.response()))
else :
self.sock.sendall(self.sock, self.ws.send(event.response()))
#end if
#end if
#end case
case wsevents.Ping() :
if ASYNC :
await self.sock.sendall(self.ws.send(event.response()))
else :
self.sock.sendall(self.ws.send(event.response()))
#end if
#end case
case wsevents.TextMessage() :
self.partial += event.data
if not event.message_finished :
break
if self.partial != "" :
result = json.loads(self.partial)
self.partial = ""
else :
result = None
#end if
yield result
#end case
case _ :
raise RuntimeError \
(
"unexpected WebSocket event %s -- %s"
%
(type(event).__name__, repr(event))
)
#end case
#end match
#end while
It is permissible to mix all the different kinds of matches in
different case-clauses within the same match-statement.
I?m still getting used to the match-statement, and I?m currently
thinking of dropping the ?#end case? line, since each case-clause is
implicitly terminated by the following one (or the end of the
match-statement) anyway. The ?#end match? will stay, of course.
--- PyGate Linux v1.5.19
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)