May 24, 2016
3:42 p.m.
On Tue, May 24, 2016 at 5:59 PM, Michel Desmoulin <desmoulinmichel@gmail.com> wrote: > What about making it a function ? > > Pattern matching is complex, it can be like a mini language inside the > language, just like regex. > > To match text we do: > > import re > re.match('pattern', 'string') > > We could do the same for matching structures: > > from inspect import match > > def do(stuff): > m = match(stuff): # m implements __call__ > if m('whatever mini language you like'): > return foo > if m('again'): > return m.thing_your_extracted > Or with methods: m = match(stuff) if m.by_type(SomeType): # handle SomeType elif m.by_attrs('x', 'y'): # do things with stuff.x and stuff.y elif m.by_len(3): x,y,z = stuff # do things with x, y, z > Pros: > > - no need for a new syntax > - very explicit > - use well know constructs > - we can easily start experimenting with the matching language in a lib > in Pypy + can be extended easily > > Cons: > > - much more verbose; > - debugging your mini language and handling error is not as good; + probably slower + need to separately get the desired attributes/elements after matching. -- Koos