[Tutor] Why are expressions not allowed as parameters in function definition statements?

Danny Yoo dyoo at hashcollision.org
Sun Jun 19 00:04:10 EDT 2016


> You know Steve, as I was typing the beginning of a reply responding to
> a similar question you asked earlier in your response, I suddenly
> realized how ridiculous having a parameter of 'col/2' is!  I'll just
> have either eat crow or attribute this to a brain fart.  You pick!


Just to play devil's advocate: it's not crazy.  Essentially, what
you're asking for is called "pattern matching", and it  is done in a
class of many programming languages.  One of the more well known of
these is Prolog.  https://en.wikipedia.org/wiki/Prolog.  Other forms
of pattern matching show up in ML, and it *is* used in industry.
(e.g. Microsoft's F#.)

It's a bit out of scope to talk about this much here, but I just
wanted to chime in here to say that you are not ridiculous.  :P  But
Python does not have a robust pattern matching facility; the closest
it has is a limited form of tuple matching:

######################
> def f((x,y), z):
...      print x, y, z
...
>   f([1, 2], 3)
1 2 3
######################


with very limited applicability and rarely used.



I hope everyone is well!


More information about the Tutor mailing list