[Python-Dev] re: PEP 275: Switching on Multiple Values, Rev 1.2
Brian Slesinsky
bslesins@best.com
Mon, 26 Nov 2001 18:34:25 -0800 (PST)
> In languages with pattern matching, you find that they use it to
> emulate late binding: depending on the structure of a thing, you
> perform different code. In Python, this is more easily done using
> methods of the object, which naturally dispatch based on the type of
> the object.
Method dispatch is a great technique and I was once in the "all case
statements are insufficiently object-oriented" camp. But sometimes you
need to work with base types or other people's classes without subclassing
them, so I find myself writing long if-then-elif statements (most recently
for a memory profiler), and case statements would make this code clearer.
(Though not nice enough to switch languages, in particular to Prolog.)
re: "Classes don't participate that easily in pattern matching"
True for many classes and I'll admit my proposal was quarter-baked. Even
matching on base types like tuple, list, and dictionary would be plenty
interesting. (Especially since they're subclassable.)
- Brian