binding a reference to a variable
Christian Tanzer
tanzer at swing.co.at
Thu Apr 11 03:01:39 EDT 2002
Andrew Koenig <ark at research.att.com> wrote:
> The point of this example is the . operator, which takes a pattern
> (by value) as its left argument and a variable (by reference) as its
> right argument. The result is a pattern that matches the same strings
> as the left argument, but if the match is successful, it assigns
> the substring matched to the variable that is the right argument.
>
> As it happens, I can implement much the same idea in C++, though
> the exact details of operator overloading make it look somewhat
> different:
>
> string id, domain;
> match(name, pos(0) + (break("@") >> id) + "@" + (rpos(0) >> domain));
>
> and I can also implement a similar idea in ML, though I don't
> think it's worth going into details here.
>
> So I was thinking about how I would do something similar in Python.
>
> Focusing just on the (break("@") . id) subexpression, how would
> one implement it? That's what led me on this path.
How about using a class to implement the matcher and assigning the
matched substrings to a namespace provided by the matcher instance in
question?
Using `* args, ** kw` trickery, you might be able to trade readability
of the matcher code for ease of the matcher clients. For instance:
m(name, m.pos(0) + m.break(id="@") + "@" + m.rpos(domain=0))
YMMV,
--
Christian Tanzer tanzer at swing.co.at
Glasauergasse 32 Tel: +43 1 876 62 36
A-1130 Vienna, Austria Fax: +43 1 877 66 92
More information about the Python-list
mailing list